[hamster-applet] added simple TSV and XML support (accessible via save button in overview)



commit e50c887903dae2183ffc90d4426a9ef826c0dfed
Author: Toms Bauģis <toms baugis gmail com>
Date:   Sat Jun 6 21:29:31 2009 +0100

    added simple TSV and XML support (accessible via save button in overview)
---
 hamster/reports.py |   65 ++++++++++++++++++++++++++++++++++++------
 hamster/stats.py   |   80 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 133 insertions(+), 12 deletions(-)

diff --git a/hamster/reports.py b/hamster/reports.py
index e01012d..81e23a0 100644
--- a/hamster/reports.py
+++ b/hamster/reports.py
@@ -22,8 +22,9 @@ from hamster import stuff, storage
 import os
 import datetime as dt
 import webbrowser
+from xml.dom.minidom import Document
 
-def simple(facts, start_date, end_date):
+def simple(facts, start_date, end_date, format, filename):
     dates_dict = stuff.dateDict(start_date, "start_")
     dates_dict.update(stuff.dateDict(end_date, "end_"))
     
@@ -39,9 +40,60 @@ def simple(facts, start_date, end_date):
         title = _(u"Overview for %(start_B)s %(start_d)s, %(start_Y)s") % dates_dict
     
 
-    report_path = os.path.join(os.path.expanduser("~"), "%s.html" % title)
-    report_path = stuff.locale_from_utf8(report_path)
-    report = open(report_path, "w")    
+    report_path = stuff.locale_from_utf8(filename)
+    report = open(report_path, "w")
+    
+    
+    if format == "tsv":
+        #comment of cvs file structure
+        report.write(_("#activity, start datetime ISO, end datetime ISO, duration in minutes, category, description\n"))
+        for fact in facts:
+            fact["description"] = fact["description"] or ""
+            fact["category"] = fact["category"] or _("Unsorted")
+            fact["start_time"] = fact["start_time"].strftime("%Y-%m-%d %H:%M:%S")
+            if fact["end_time"]:
+                fact["end_time"] = fact["end_time"].strftime("%Y-%m-%d %H:%M:%S")
+            else:
+                fact["end_time"] = ""                
+            fact["delta"] = fact["delta"].seconds / 60 + fact["delta"].days * 24 * 60
+
+            report.write("%(name)s\t%(start_time)s\t%(end_time)s\t%(delta)s\t%(category)s\t%(description)s\n" % fact)
+
+    elif format == "xml":
+        doc = Document()
+        activity_list = doc.createElement("activities")
+        
+        for fact in facts:
+            fact["description"] = fact["description"] or ""
+            fact["category"] = fact["category"] or _("Unsorted")
+            fact["start_time"] = fact["start_time"].strftime("%Y-%m-%d %H:%M:%S")
+            if fact["end_time"]:
+                fact["end_time"] = fact["end_time"].strftime("%Y-%m-%d %H:%M:%S")
+            else:
+                fact["end_time"] = ""
+            fact["delta"] = str(fact["delta"].seconds / 60 + fact["delta"].days * 24 * 60)
+
+            doc_fact = doc.createElement("activity")
+            doc_fact.setAttribute("name", fact["name"])
+            doc_fact.setAttribute("start_time", fact["start_time"])
+            doc_fact.setAttribute("end_time", fact["end_time"])
+            doc_fact.setAttribute("duration_minutes", fact["delta"])
+            doc_fact.setAttribute("category", fact["category"])
+            doc_fact.setAttribute("description", fact["description"])
+            
+            activity_list.appendChild(doc_fact)
+        
+        doc.appendChild(activity_list)        
+        report.write(doc.toxml())
+        
+    else: #default to HTML
+        write_html(report, title, facts)
+
+    report.close()
+    
+def write_html(report, title, facts):
+    """TODO bring template to external file or write to PDF"""
+    
     report.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
 <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
@@ -187,9 +239,4 @@ def simple(facts, start_date, end_date):
     report.write("</table>\n")
 
     report.write("</body>\n</html>")
-
-    
-    report.close()
-
-    webbrowser.open_new("file://"+report_path)
     
diff --git a/hamster/stats.py b/hamster/stats.py
index 755a348..761b9a5 100644
--- a/hamster/stats.py
+++ b/hamster/stats.py
@@ -552,9 +552,83 @@ class StatsViewer(object):
         custom_fact.show()
         
     def on_report_button_clicked(self, widget):
-        from hamster import reports
-        facts = storage.get_facts(self.start_date, self.end_date)
-        reports.simple(facts, self.start_date, self.end_date)
+        chooser = gtk.FileChooserDialog(title = _("Save report - Time Tracker"),
+                                        parent = None,
+                                        action=gtk.FILE_CHOOSER_ACTION_SAVE,
+                                        buttons=(gtk.STOCK_CANCEL,
+                                                 gtk.RESPONSE_CANCEL,
+                                                 gtk.STOCK_SAVE,
+                                                 gtk.RESPONSE_OK))
+
+        chooser.set_current_folder(os.path.expanduser("~"))
+
+        #set suggested name to something readable, replace backslashes with dots
+        #so the name is valid in linux
+        filename = "Time track %s - %s" % (self.start_date.strftime("%x").replace("/", "."),
+                                           self.end_date.strftime("%x").replace("/", "."))
+        chooser.set_current_name(filename)
+
+        filters = {}
+
+        filter = gtk.FileFilter()
+        filter.set_name(_("HTML Report"))
+        filter.add_mime_type("text/html")
+        filter.add_pattern("*.html")
+        filter.add_pattern("*.htm")
+        filters["html"] = filter
+        chooser.add_filter(filter)
+
+        filter = gtk.FileFilter()
+        filter.set_name(_("Tab Separated Values (TSV)"))
+        filter.add_mime_type("text/plain")
+        filter.add_pattern("*.tsv")
+        filter.add_pattern("*.txt")
+        filters["tsv"] = filter
+        chooser.add_filter(filter)
+
+        filter = gtk.FileFilter()
+        filter.set_name(_("XML"))
+        filter.add_mime_type("text/xml")
+        filter.add_pattern("*.xml")
+        filters["xml"] = filter
+        chooser.add_filter(filter)
+
+        filter = gtk.FileFilter()
+        filter.set_name("All files")
+        filter.add_pattern("*")
+        chooser.add_filter(filter)
+        
+        
+        response = chooser.run()
+        if response == gtk.RESPONSE_OK:
+            format = "html"
+            if chooser.get_filter() == filters["tsv"]:
+                format = "tsv"
+            elif chooser.get_filter() == filters["xml"]:
+                format = "xml"
+
+            from hamster import reports
+            facts = storage.get_facts(self.start_date, self.end_date)
+            path = chooser.get_filename()
+            
+            reports.simple(facts,
+                           self.start_date,
+                           self.end_date,
+                           format,
+                           path)
+
+            if format in ("tsv", "xml"):
+                gtk.show_uri(gtk.gdk.Screen(),
+                             "file://" + os.path.split(path)[0], 0L)
+            else:
+                webbrowser.open_new("file://"+report_path)
+
+        chooser.destroy()
+        
+        # supported types: HTML, CSV, XML
+        #save_as.add
+        
+        
 
     def after_activity_update(self, widget, renames):
         self.do_graph()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]