Skip to content

Commit

Permalink
use CSV as default output format
Browse files Browse the repository at this point in the history
  • Loading branch information
kouk committed Oct 26, 2016
1 parent 1c3b91e commit 213829a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mgship/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import click

from mgship import mgship
from mgship.destination import console
from mgship.destination import csv


@click.command()
def main(args=None):
"""Console script for mgship"""

mgship.Archive(console.Destination()).ship()
mgship.Archive(csv.Destination()).ship()


if __name__ == "__main__":
Expand Down
31 changes: 31 additions & 0 deletions mgship/destination/csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Print events to the console as CSV."""
from __future__ import absolute_import
import sys
import csv


from contextlib import contextmanager

__all__ = ['Destination']


def print_events():
eid = None
writer = csv.writer(sys.stdout)
while True:
event = yield eid
envelope = event.get('envelope', {})
writer.writerow([
','.join(event.get('tags', [])),
envelope.get('sender', ''),
envelope.get('targets', ''),
event.get('event', ''),
event.get('timestamp', '')])


def make_sink():
sink = print_events()
next(sink)
yield sink

Destination = contextmanager(make_sink)
24 changes: 24 additions & 0 deletions mgship/destination/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Print events to the console as JSON."""
from __future__ import absolute_import
import sys
import json


from contextlib import contextmanager

__all__ = ['Destination']


def print_events():
eid = None
while True:
event = yield eid
print json.dumps(event)


def make_sink():
sink = print_events()
next(sink)
yield sink

Destination = contextmanager(make_sink)

0 comments on commit 213829a

Please sign in to comment.