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

allow output to a stream like stdout #155

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 14 additions & 13 deletions singer/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,33 +222,34 @@ 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_message(message, out=sys.stdout):
out.write(format_message(message) + '\n')
out.flush()


def write_record(stream_name, record, stream_alias=None, time_extracted=None):
def write_record(stream_name, record, stream_alias=None, time_extracted=None, out=sys.stdout):
"""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,
time_extracted=time_extracted))
time_extracted=time_extracted), out=out)


def write_records(stream_name, records):
def write_records(stream_name, records, out=sys.stdout):
"""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)
write_record(stream_name, record, out=out)


def write_schema(stream_name, schema, key_properties, bookmark_properties=None, stream_alias=None):
def write_schema(stream_name, schema, key_properties, bookmark_properties=None, stream_alias=None,
out=sys.stdout):
"""Write a schema message.

stream = 'test'
Expand All @@ -266,22 +267,22 @@ def write_schema(stream_name, schema, key_properties, bookmark_properties=None,
stream=(stream_alias or stream_name),
schema=schema,
key_properties=key_properties,
bookmark_properties=bookmark_properties))
bookmark_properties=bookmark_properties), out=out)


def write_state(value):
def write_state(value, out=sys.stdout):
"""Write a state message.

write_state({'last_updated_at': '2017-02-14T09:21:00'})
"""
write_message(StateMessage(value=value))
write_message(StateMessage(value=value), out=out)


def write_version(stream_name, version):
def write_version(stream_name, version, out=sys.stdout):
"""Write an activate version message.

stream = 'test'
version = int(time.time())
write_version(stream, version)
"""
write_message(ActivateVersionMessage(stream_name, version))
write_message(ActivateVersionMessage(stream_name, version), out=out)