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

Support print csv format #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
31 changes: 26 additions & 5 deletions pull_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


class FormattedIncident(object):
def pretty_output(self):
def _pretty_output(self):
return u'Time: {}\nService: {}\nDescription: {}\nURL: {}\nNotes:\n{}\n'.format(
self.created_on.strftime('%A, %B %-d - %-I:%M %p'),
self.service,
Expand All @@ -33,6 +33,20 @@ def pretty_output(self):
self.notes,
)

def _csv_output(self):
return u'{},{},{},{},"{}"'.format(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use python's CSV library to create this row so that escaping is done correctly?

self.created_on.strftime('%A, %B %-d - %-I:%M %p'),
self.service,
self.description,
self.url,
self.notes,
)

def output(self, print_csv):
if print_csv:
return self._csv_output()
else:
return self._pretty_output()

def recent_incidents_for_services(services, time_window):
service_ids = [service.id for service in services]
Expand All @@ -48,6 +62,7 @@ def print_all_incidents(
time_window_days,
group_by_description=False,
group_by_service=False,
print_csv=False,
include_stats=False,
include_incidents_as_blockquote=False,
):
Expand All @@ -74,13 +89,14 @@ def print_all_incidents(
sorted_group_to_incident_list = sorted_description_to_incident_list
if group_by_service or group_by_description:
for group, incident_list in sorted_group_to_incident_list.iteritems():
print("########### {}: {} ##########\n".format(len(incident_list), group))
if not print_csv:
print("########### {}: {} ##########\n".format(len(incident_list), group))
if not silent:
for incident in incident_list:
print(incident.pretty_output())
print(incident.output(print_csv))
else:
for incident in all_incidents:
print(incident.pretty_output())
print(incident.output(print_csv))

print('Total Pages: {}'.format(len(all_incidents)))
if include_incidents_as_blockquote:
Expand Down Expand Up @@ -187,6 +203,10 @@ def is_non_actionable(incident):
action="store_true",
default=False,
help="Group PD incidents by service")
parser.add_argument("--print-csv",
action="store_true",
default=False,
help="Print in CSV format")
parser.add_argument("--include-stats",
action="store_true",
default=False,
Expand All @@ -204,7 +224,8 @@ def is_non_actionable(incident):
silent=args.silent,
group_by_description=args.group_by_description,
group_by_service=args.group_by_service,
print_csv=args.print_csv,
include_stats=args.include_stats,
include_incidents_as_blockquote=args.include_incidents_as_blockquote,
time_window_days=args.days
)
)