forked from singer-io/target-csv
-
Notifications
You must be signed in to change notification settings - Fork 8
/
target_csv.py
executable file
·193 lines (157 loc) · 6.68 KB
/
target_csv.py
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
#!/usr/bin/env python3
import argparse
import io
import os
import sys
import json
import simplejson
import csv
import threading
import http.client
import urllib
from datetime import datetime
import collections
import pkg_resources
import pathlib
from jsonschema.validators import Draft4Validator
import singer
logger = singer.get_logger()
def emit_state(state):
if state is not None:
line = json.dumps(state)
logger.debug('Emitting state {}'.format(line))
sys.stdout.write("{}\n".format(line))
sys.stdout.flush()
def flatten(d, parent_key='', sep='__'):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, collections.MutableMapping):
items.extend(flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, str(v) if type(v) is list else v))
return dict(items)
def persist_messages(delimiter, quotechar, messages, destination_path, fixed_headers, validate):
state = None
schemas = {}
key_properties = {}
headers = {}
validators = {}
now = datetime.now().strftime('%Y%m%dT%H%M%S')
for message in messages:
try:
o = singer.parse_message(message).asdict()
except json.decoder.JSONDecodeError:
logger.error("Unable to parse:\n{}".format(message))
raise
message_type = o['type']
if message_type == 'RECORD':
if o['stream'] not in schemas:
raise Exception("A record for stream {}"
"was encountered before a corresponding schema".format(o['stream']))
if validate:
validators[o['stream']].validate(o['record'])
# replace all empty spaces and "/" in the stream name
stream = o['stream'].replace("/", "_")
o['stream'] = stream
filename = o['stream'] + '-' + now + '.csv'
filename = os.path.expanduser(os.path.join(destination_path, filename))
file_is_empty = (not os.path.isfile(filename)) or os.stat(filename).st_size == 0
# flattened_record = flatten(o['record'])
flattened_record = o['record']
if fixed_headers is not None and o['stream'] in fixed_headers:
if o['stream'] not in headers:
headers[o['stream']] = fixed_headers[o['stream']]
else:
if o['stream'] not in headers and not file_is_empty:
with open(filename, 'r') as csvfile:
reader = csv.reader(csvfile,
delimiter=delimiter,
quotechar=quotechar)
first_line = next(reader)
headers[o['stream']] = first_line if first_line else flattened_record.keys()
else:
headers[o['stream']] = flattened_record.keys()
with open(filename, 'a') as csvfile:
writer = csv.DictWriter(csvfile,
headers[o['stream']],
extrasaction='ignore',
delimiter=delimiter,
quotechar=quotechar,
)
if file_is_empty:
writer.writeheader()
# We use simplejson to re-serialize the data to avoid formatting issues in the CSV
r = simplejson.dumps(flattened_record)
# if \\u0000 is being used as line terminator, replace with \n
r = r.replace("\\u0000", "\\n")
writer.writerow(simplejson.loads(r))
job_metrics_file_path = os.path.expanduser(os.path.join(destination_path, "job_metrics.json"))
if not os.path.isfile(job_metrics_file_path):
pathlib.Path(job_metrics_file_path).touch()
with open(job_metrics_file_path, 'r+') as job_metrics_file:
content = dict()
try:
content = json.loads(job_metrics_file.read())
except Exception:
pass
if not content.get('recordCount'):
content['recordCount'] = dict()
content['recordCount'][o['stream']] = content['recordCount'].get(o['stream'], 0) + 1
job_metrics_file.seek(0)
job_metrics_file.write(json.dumps(content))
state = None
elif message_type == 'STATE':
logger.debug('Setting state to {}'.format(o['value']))
state = o['value']
elif message_type == 'SCHEMA':
stream = o['stream']
schemas[stream] = o['schema']
validators[stream] = Draft4Validator(o['schema'])
key_properties[stream] = o['key_properties']
else:
logger.warning("Unknown message type {} in message {}"
.format(o['type'], o))
return state
def send_usage_stats():
try:
version = pkg_resources.get_distribution('target-csv').version
conn = http.client.HTTPConnection('collector.singer.io', timeout=10)
conn.connect()
params = {
'e': 'se',
'aid': 'singer',
'se_ca': 'target-csv',
'se_ac': 'open',
'se_la': version,
}
conn.request('GET', '/i?' + urllib.parse.urlencode(params))
response = conn.getresponse()
conn.close()
except:
logger.debug('Collection request failed')
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', help='Config file')
args = parser.parse_args()
if args.config:
with open(args.config) as input_json:
config = json.load(input_json)
else:
config = {}
if not config.get('disable_collection', False):
logger.info('Sending version information to singer.io. ' +
'To disable sending anonymous usage data, set ' +
'the config parameter "disable_collection" to true')
threading.Thread(target=send_usage_stats).start()
input_messages = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
state = persist_messages(config.get('delimiter', ','),
config.get('quotechar', '"'),
input_messages,
config.get('destination_path', ''),
config.get('fixed_headers'),
config.get('validate', True))
emit_state(state)
logger.debug("Exiting normally")
if __name__ == '__main__':
main()