From 3e5608217fdc63427e48a6569b211ceafef4c4eb Mon Sep 17 00:00:00 2001 From: Grzegorz Sobczyk Date: Mon, 3 Aug 2020 22:51:44 +0200 Subject: [PATCH] External activities - added possibility to export activities to external source using command line --- NEWS.md | 1 + src/hamster-cli.py | 4 ++-- src/hamster/reports.py | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index e48d83229..998f2da25 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,7 @@ - x: toggle export flag * Gathering activities from external source (right now only Jira and only via dbus) * Export activities as worklogs to jira +* Export activities to external source from command line (`hamster export external` command) ## Changes in 3.0.2 diff --git a/src/hamster-cli.py b/src/hamster-cli.py index 4cf078678..f0fde0418 100644 --- a/src/hamster-cli.py +++ b/src/hamster-cli.py @@ -237,7 +237,7 @@ def assist(self, *args): if assist_command == "start": hamster_client._activities(" ".join(args[1:])) elif assist_command == "export": - formats = "html tsv xml ical hamster".split() + formats = "html tsv xml ical hamster external".split() chosen = sys.argv[-1] formats = [f for f in formats if not chosen or f.startswith(chosen)] print("\n".join(formats)) @@ -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|hamster] [start-date [end-date]]: Export activities with + * export [html|tsv|ical|xml|hamster|external] [start-date [end-date]]: Export activities with the specified format * current: Print current activity * activities: List all the activities names, one per line. diff --git a/src/hamster/reports.py b/src/hamster/reports.py index c3947a089..3cb048ada 100644 --- a/src/hamster/reports.py +++ b/src/hamster/reports.py @@ -29,6 +29,7 @@ from string import Template from textwrap import dedent +from hamster import client from hamster.lib import datetime as dt from hamster.lib.configuration import runtime from hamster.lib import stuff @@ -57,6 +58,8 @@ def simple(facts, start_date, end_date, format, path = None): writer = ICalWriter(report_path) elif format == "hamster": writer = HamsterWriter(report_path) + elif format == "external": + writer = ExternalWriter(report_path) else: #default to HTML writer = HTMLWriter(report_path, start_date, end_date) @@ -168,6 +171,24 @@ def _write_fact(self, fact): def _finish(self, facts): pass +class ExternalWriter(ReportWriter): + def __init__(self, path): + ReportWriter.__init__(self, path) + self.storage = client.Storage() + + def _write_fact(self, fact): + exported = self.storage.export_fact(fact.id) + if exported: + self.file.write(_("Exported: %s - %s") % (fact.activity, fact.delta) + "\n") + fact.exported = True + self.storage.update_fact(fact.id, fact, False) + pass + else: + self.file.write(_("Fact not exported: %s" % fact.activity) + "\n") + + def _finish(self, facts): + pass + class XMLWriter(ReportWriter): def __init__(self, path): ReportWriter.__init__(self, path)