forked from singer-io/singer-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.py
More file actions
229 lines (166 loc) · 6.5 KB
/
Copy pathmessages.py
File metadata and controls
229 lines (166 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import sys
import simplejson as json
class Message(object):
'''Base class for messages.'''
def asdict(self): # pylint: disable=no-self-use
raise Exception('Not implemented')
def __eq__(self, other):
return isinstance(other, Message) and self.asdict() == other.asdict()
def __repr__(self):
pairs = ["{}={}".format(k, v) for k, v in self.asdict().items()]
attrstr = ", ".join(pairs)
return "{}({})".format(self.__class__.__name__, attrstr)
def __str__(self):
return str(self.asdict())
class RecordMessage(Message):
'''RECORD message.
The RECORD message has these fields:
* stream (string) - The name of the stream the record belongs to.
* record (dict) - The raw data for the record
* version (optional, int) - For versioned streams, the version
number. Note that this feature is experimental and most Taps and
Targets should not need to use versioned streams.
>>> msg = singer.RecordMessage(
>>> stream='users',
>>> record={'id': 1, 'name': 'Mary'})
'''
def __init__(self, stream, record, version=None):
self.stream = stream
self.record = record
self.version = version
def asdict(self):
result = {
'type': 'RECORD',
'stream': self.stream,
'record': self.record,
}
if self.version is not None:
result['version'] = self.version
return result
def __str__(self):
return str(self.asdict())
class SchemaMessage(Message):
'''SCHEMA message.
The SCHEMA message has these fields:
* stream (string) - The name of the stream this schema describes.
* schema (dict) - The JSON schema.
* key_properties (list of strings) - List of primary key properties.
>>> msg = singer.SchemaMessage(
>>> stream='users',
>>> schema={'type': 'object',
>>> 'properties': {
>>> 'id': {'type': 'integer'},
>>> 'name': {'type': 'string'}
>>> }
>>> },
>>> key_properties=['id'])
'''
def __init__(self, stream, schema, key_properties):
self.stream = stream
self.schema = schema
self.key_properties = key_properties
def asdict(self):
return {
'type': 'SCHEMA',
'stream': self.stream,
'schema': self.schema,
'key_properties': self.key_properties
}
class StateMessage(Message):
'''STATE message.
The STATE message has one field:
* value (dict) - The value of the state.
>>> msg = singer.StateMessage(
>>> value={'users': '2017-06-19T00:00:00'})
'''
def __init__(self, value):
self.value = value
def asdict(self):
return {
'type': 'STATE',
'value': self.value
}
class ActivateVersionMessage(Message):
'''ACTIVATE_VERSION message (EXPERIMENTAL).
The ACTIVATE_VERSION messages has these fields:
* stream - The name of the stream.
* version - The version number to activate.
This is a signal to the Target that it should delete all previously
seen data and replace it with all the RECORDs it has seen where the
record's version matches this version number.
Note that this feature is experimental. Most Taps and Targets should
not need to use the "version" field of "RECORD" messages or the
"ACTIVATE_VERSION" message at all.
>>> msg = singer.ActivateVersionMessage(
>>> stream='users',
>>> version=2)
'''
def __init__(self, stream, version):
self.stream = stream
self.version = version
def asdict(self):
return {
'type': 'ACTIVATE_VERSION',
'stream': self.stream,
'version': self.version
}
def _required_key(msg, k):
if k not in msg:
raise Exception("Message is missing required key '{}': {}".format(k, msg))
return msg[k]
def parse_message(msg):
"""Parse a message string into a Message object."""
obj = json.loads(msg)
msg_type = _required_key(obj, 'type')
if msg_type == 'RECORD':
return RecordMessage(stream=_required_key(obj, 'stream'),
record=_required_key(obj, 'record'),
version=obj.get('version'))
elif msg_type == 'SCHEMA':
return SchemaMessage(stream=_required_key(obj, 'stream'),
schema=_required_key(obj, 'schema'),
key_properties=_required_key(obj, 'key_properties'))
elif msg_type == 'STATE':
return StateMessage(value=_required_key(obj, 'value'))
elif msg_type == 'ACTIVATE_VERSION':
return ActivateVersionMessage(stream=_required_key(obj, 'stream'),
version=_required_key(obj, 'version'))
def format_message(message):
return json.dumps(message.asdict(), use_decimal=True)
def write_message(message):
sys.stdout.write(format_message(message) + '\n')
sys.stdout.flush()
def write_record(stream_name, record, stream_alias=None):
"""Write a single record for the given stream.
>>> write_record("users", {"id": 2, "email": "mike@stitchdata.com"})
"""
write_message(RecordMessage(stream=(stream_alias or stream_name), record=record))
def write_records(stream_name, records):
"""Write a list of records for the given stream.
>>> chris = {"id": 1, "email": "chris@stitchdata.com"}
>>> mike = {"id": 2, "email": "mike@stitchdata.com"}
>>> write_records("users", [chris, mike])
"""
for record in records:
write_record(stream_name, record)
def write_schema(stream_name, schema, key_properties, stream_alias=None):
"""Write a schema message.
>>> stream = 'test'
>>> schema = {'properties': {'id': {'type': 'integer'}, 'email': {'type': 'string'}}} # nopep8
>>> key_properties = ['id']
>>> write_schema(stream, schema, key_properties)
"""
if isinstance(key_properties, (str, bytes)):
key_properties = [key_properties]
if not isinstance(key_properties, list):
raise Exception("key_properties must be a string or list of strings")
write_message(
SchemaMessage(
stream=(stream_alias or stream_name),
schema=schema,
key_properties=key_properties))
def write_state(value):
"""Write a state message.
>>> write_state({'last_updated_at': '2017-02-14T09:21:00'})
"""
write_message(StateMessage(value=value))