Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newlinefix #6

Merged
merged 6 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ __pycache__/
*.py[cod]
*$py.class

# Mac
.DS_Store

# C extensions
*.so

Expand Down
6 changes: 6 additions & 0 deletions .secrets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# IMPORTANT! This folder is hidden from git - if you need to store config files or other secrets,
# make sure those are never staged for commit into your git repo. You can store them here or another
# secure location.

*
!.gitignore
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
from setuptools import setup

setup(name='target-csv',
version='0.3.3',
version='0.3.6',
description='Singer.io target for writing CSV files',
author='Stitch',
url='https://singer.io',
classifiers=['Programming Language :: Python :: 3 :: Only'],
py_modules=['target_csv'],
install_requires=[
'jsonschema==2.6.0',
'singer-python>=5.1.0,<=5.3.1',
'singer-python==5.12.1',
'simplejson==3.11.1'
],
entry_points='''
[console_scripts]
Expand Down
16 changes: 10 additions & 6 deletions target_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import sys
import json
import simplejson
import csv
import threading
import http.client
Expand Down Expand Up @@ -38,7 +39,7 @@ def flatten(d, parent_key='', sep='__'):
return dict(items)


def persist_messages(delimiter, quotechar, messages, destination_path, fixed_headers):
def persist_messages(delimiter, quotechar, messages, destination_path, fixed_headers, validate):
state = None
schemas = {}
key_properties = {}
Expand All @@ -58,8 +59,8 @@ def persist_messages(delimiter, quotechar, messages, destination_path, fixed_hea
if o['stream'] not in schemas:
raise Exception("A record for stream {}"
"was encountered before a corresponding schema".format(o['stream']))

validators[o['stream']].validate(o['record'])
if validate:
validators[o['stream']].validate(o['record'])

filename = o['stream'] + '-' + now + '.csv'
filename = os.path.expanduser(os.path.join(destination_path, filename))
Expand All @@ -82,7 +83,7 @@ def persist_messages(delimiter, quotechar, messages, destination_path, fixed_hea
else:
headers[o['stream']] = flattened_record.keys()

with open(filename, 'a') as csvfile:
with open(filename, 'a', newline='') as csvfile:
writer = csv.DictWriter(csvfile,
headers[o['stream']],
extrasaction='ignore',
Expand All @@ -91,7 +92,9 @@ def persist_messages(delimiter, quotechar, messages, destination_path, fixed_hea
if file_is_empty:
writer.writeheader()

writer.writerow(flattened_record)
# We use simplejson to re-serialize the data to avoid formatting issues in the CSV
r = simplejson.dumps(flattened_record)
writer.writerow(simplejson.loads(r))

state = None
elif message_type == 'STATE':
Expand Down Expand Up @@ -150,7 +153,8 @@ def main():
config.get('quotechar', '"'),
input_messages,
config.get('destination_path', ''),
config.get('fixed_headers'))
config.get('fixed_headers'),
config.get('validate', True))

emit_state(state)
logger.debug("Exiting normally")
Expand Down