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

feat: add JSON support to export #617

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/hamster-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def assist(self, *args):
if assist_command == "start":
hamster_client._activities(sys.argv[-1])
elif assist_command == "export":
formats = "html tsv xml ical".split()
formats = "html tsv xml ical json".split()
chosen = sys.argv[-1]
formats = [f for f in formats if not chosen or f.startswith(chosen)]
print("\n".join(formats))
Expand Down Expand Up @@ -422,7 +422,7 @@ def version(self):
* list [start-date [end-date]]: List activities
* search [terms] [start-date [end-date]]: List activities matching a search
term
* export [html|tsv|ical|xml] [start-date [end-date]]: Export activities with
* export [html|tsv|ical|xml|json] [start-date [end-date]]: Export activities with
the specified format
* current: Print current activity
* activities: List all the activities names, one per line.
Expand Down
23 changes: 23 additions & 0 deletions src/hamster/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def simple(facts, start_date, end_date, format, path = None):

if format == "tsv":
writer = TSVWriter(report_path)
if format == "json":
writer = JSONWriter(report_path)
elif format == "xml":
writer = XMLWriter(report_path)
elif format == "ical":
Expand Down Expand Up @@ -323,3 +325,24 @@ def _finish(self, facts):
self.file.write(Template(self.main_template).safe_substitute(data))

return

class JSONWriter(ReportWriter):
def __init__(self, path):
ReportWriter.__init__(self, path)
self.activity_list = []

def _write_fact(self, fact):
self.activity_list.append(
{
"name": fact.activity,
"start_time": str(fact.start_time),
"end_time": str(fact.end_time),
"duration_minutes": stuff.duration_minutes(fact.delta),
"category": fact.category,
"description": fact.description,
"tags": ": ".join(fact.tags),
}
)

def _finish(self, facts):
self.file.write(json.dumps(self.activity_list))