Skip to content

Commit

Permalink
remove the ReportWriter.export function (fix issue #373)
Browse files Browse the repository at this point in the history
No longer used in hamster. And could lead to
ValueError: I/O operation on closed file
  • Loading branch information
ederag committed Jan 10, 2019
1 parent 8b166c4 commit 45c5e87
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/hamster-cli
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ class HamsterClient(object):
facts = self.storage.get_facts(start_time, end_time)

writer = reports.simple(facts, start_time.date(), end_time.date(), export_format)
print(writer.export())


def _activities(self, search=""):
Expand Down
14 changes: 7 additions & 7 deletions src/hamster/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ def simple(facts, start_date, end_date, format, path = None):
class ReportWriter(object):
#a tiny bit better than repeating the code all the time
def __init__(self, path = None, datetime_format = "%Y-%m-%d %H:%M:%S"):
self.file = open(path, "w") if path else codecs.getwriter("utf8")(StringIO())
# if path is empty or None, print to stdout
self.file = open(path, "w") if path else StringIO()
self.path = path
self.datetime_format = datetime_format

def export(self):
return self.file.getvalue()

def write_report(self, facts):
try:
for fact in facts:
Expand All @@ -100,8 +99,10 @@ def write_report(self, facts):

self._finish(facts)
finally:
if isinstance(self.file, IOBase):
self.file.close()
if not self.path:
# print the full report to stdout
print(self.file.getvalue())
self.file.close()

def _start(self, facts):
raise NotImplementedError
Expand Down Expand Up @@ -296,7 +297,6 @@ def _finish(self, facts):
date_facts.append([str_date, by_date.get(date, [])])
date += dt.timedelta(days=1)


data = dict(
title = self.title,

Expand Down

0 comments on commit 45c5e87

Please sign in to comment.