[hamster-applet] consolidated everything in single file



commit 8830afc6560ebb5ba1ef3e3d8f582f0fffc6e73d
Author: Toms Bauģis <toms baugis gmail com>
Date:   Sun Jun 6 14:12:56 2010 +0100

    consolidated everything in single file

 data/html_template/by_date.html           |   15 ---
 data/html_template/by_date_row.html       |    7 -
 data/html_template/by_date_total_row.html |    7 -
 data/html_template/fact_row.html          |   22 ----
 data/html_template/main.html              |  134 ---------------------
 data/report_template.html                 |  187 +++++++++++++++++++++++++++++
 data/wscript_build                        |    2 +-
 src/hamster/reports.py                    |   78 ++++++++----
 8 files changed, 240 insertions(+), 212 deletions(-)
---
diff --git a/data/report_template.html b/data/report_template.html
new file mode 100644
index 0000000..e0f80df
--- /dev/null
+++ b/data/report_template.html
@@ -0,0 +1,187 @@
+<!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">
+<head>
+    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+    <meta name="author" content="hamster-applet" />
+    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js";></script>
+    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js";></script>
+    <link type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/base/jquery-ui.css"; rel="stylesheet" />
+
+
+    <title>$title</title>
+    <style type="text/css">
+        body {
+            font-family: "sans-serif";
+            font-size: 12px;
+            padding: 12px;
+            color: #303030;
+        }
+
+        h2 {
+            margin-top: 2em;
+            border-bottom: 2px solid #303030;
+        }
+        th, td {
+            text-align: left;
+            padding-right: 24px;
+        }
+
+        th {
+            padding-top: 12px;
+        }
+
+        .even {background-color: #eee;}
+        .odd {background-color: #fff;}
+
+        .by_day_controls {
+            margin-bottom: 24px;
+        }
+    </style>
+
+    <style type="text/css" media="print">
+        div#tabs ul {
+            display: none;
+        }
+
+        div#tabs {
+            border: none;
+        }
+
+        div.by_day_controls {
+            display: none;
+        }
+
+        p.template-instructions {
+            display: none;
+        }
+    </style>
+
+    <script type="text/javascript">
+        $(document).ready(function() {
+            $("#tabs").tabs()
+
+            $("#show_details").click(function() {
+                if ($(this).attr("checked")) {
+                    $(".by_date_row").show();
+                } else {
+                    $(".by_date_row").hide();
+                }
+
+            })
+
+            $("#show_totals").click(function() {
+                if ($(this).attr("checked")) {
+                    $(".by_date_total_row").show();
+                } else {
+                    $(".by_date_total_row").hide();
+                }
+
+
+            })
+        });
+    </script>
+</head>
+<body>
+
+<h1>$title</h1>
+
+<div id="tabs">
+	<ul>
+            <li><a href="#tabs-1">$totals_by_day_title</a></li>
+            <li><a href="#tabs-2">$activity_log_title</a></li>
+	</ul>
+	<div id="tabs-1">
+            <div class="by_day_controls">
+                Show:
+                <input type="checkbox" id="show_details" checked="checked"></input>
+                <label for="show_details">$activity_totals_heading</label>
+
+                <input type="checkbox" id="show_totals" checked="checked"></input>
+                <label for="show_totals">$category_totals_heading</label>
+            </div>
+
+            <table>
+                <tr>
+                    <th>Date</th>
+                    <th class="by_date_row">$activity_totals_heading</th>
+                    <th class="by_date_total_row">$category_totals_heading</th>
+                <tr>
+
+                    <by-date>
+                        <tr valign="top">
+                            <td>$date</td>
+                            <td class="by_date_row">
+                                <table width="100%">
+                                    <by-date-activity>
+                                        <tr>
+                                            <td>$activity ($category)</td>
+                                            <td style="text-align: right">
+                                                <!-- there is also duration_decimal and duration_minutes -->
+                                                $duration
+                                            </td>
+                                        </tr>
+                                    </by-date-activity>
+                                </table>
+                            </td>
+                            <td class="by_date_total_row">
+                                <table width="100%">
+                                    <by-date-category>
+                                        <tr>
+                                            <td>$category</td>
+                                            <td style="text-align: right">
+                                                <!-- there is also duration_decimal and duration_minutes -->
+                                                $duration
+                                            </td>
+                                    </by-date-category>
+                                </table>
+                            </td>
+                        </tr>
+                        <tr><td colspan="3">&nbsp</td></tr>
+                    </by-date>
+            </table>
+	</div>
+	<div id="tabs-2">
+            <table>
+                <tr>
+                    <th>$header_date</th>
+                    <th>$header_activity</th>
+                    <th>$header_category</th>
+                    <th>$header_tags</th>
+                    <th>$header_start</th>
+                    <th>$header_end</th>
+                    <th>$header_duration</th>
+                    <th>$header_description</th>
+                </tr>
+                <all-activities>
+                    <tr>
+                        <td>
+                            <!-- there is also date_iso -->
+                            $date
+                        </td>
+                        <td>$activity</td>
+                        <td>$category</td>
+                        <td>$tags</td>
+                        <td>
+                            <!-- there is also start_iso -->
+                            $start
+                        </td>
+                        <td>
+                            <!-- there is also end_iso -->
+                            $end
+                        </td>
+                        <td>
+                            <!-- there is also duration_decimal and duration_minutes -->
+                            $duration
+                        </td>
+                        <td>$description</td>
+                    </tr>
+                </all-activities>
+            </table>
+	</div>
+</div>
+
+<p class="template-instructions">
+    <a href="file:///$data_dir/report_template.html">$show_template</a>.
+    $template_instructions.
+</p>
diff --git a/data/wscript_build b/data/wscript_build
index 65f74b6..66e382d 100644
--- a/data/wscript_build
+++ b/data/wscript_build
@@ -5,7 +5,7 @@
 bld.install_files('${DATADIR}/hamster-applet', '*.ui')
 bld.install_files('${DATADIR}/hamster-applet', 'hamster.db Hamster_Applet.xml')
 bld.install_files('${DATADIR}/hamster-applet/art', 'art/*.png')
-bld.install_files('${DATADIR}/hamster-applet/html_template', 'html_template/*.html')
+bld.install_files('${DATADIR}/hamster-applet/report_template.html', 'report_template.html')
 
 bld.install_files('${DATADIR}/icons/hicolor/16x16/apps',   'art/16x16/hamster-applet.png')
 bld.install_files('${DATADIR}/icons/hicolor/22x22/apps',   'art/22x22/hamster-applet.png')
diff --git a/src/hamster/reports.py b/src/hamster/reports.py
index 86682f8..7744969 100644
--- a/src/hamster/reports.py
+++ b/src/hamster/reports.py
@@ -27,6 +27,7 @@ from hamster.i18n import C_
 from hamster.configuration import runtime
 import copy
 import itertools
+import re
 
 from string import Template
 
@@ -178,26 +179,45 @@ class HTMLWriter(ReportWriter):
         else:
             self.title = _(u"Activity log for %(start_B)s %(start_d)s â?? %(end_d)s, %(end_Y)s") % dates_dict
 
-        self.main_template = self._get_template("main.html")
-        self.fact_row_template = self._get_template("fact_row.html")
-        self.by_date_template= self._get_template("by_date.html")
-        self.by_date_row_template = self._get_template("by_date_row.html")
-        self.by_date_total_row_template = self._get_template("by_date_total_row.html")
 
-        self.fact_rows = []
-
-    def _get_template(self, name):
-        """returns contents of the template file. allows override from the
-           home folder"""
-        if os.path.exists(os.path.join(runtime.home_data_dir, name)):
-            template = os.path.join(runtime.home_data_dir, name)
+        # read the template, allow override
+        if os.path.exists(os.path.join(runtime.home_data_dir, "report_template.html")):
+            template = os.path.join(runtime.home_data_dir, "report_template.html")
         else:
-            template = os.path.join(runtime.data_dir, "html_template", name)
+            template = os.path.join(runtime.data_dir, "report_template.html")
 
+        self.main_template = ""
         with open(template, 'r') as f:
-            contents = f.read()
+            self.main_template =f.read()
+
+
+        self.fact_row_re = re.compile('<all-activities>(.*)</all-activities>', re.DOTALL)
+
+        self.fact_row_template = self.fact_row_re.search(self.main_template)
+
+        self.fact_row_template = self._extract_template('all-activities')
+
+        self.by_date_row_template = self._extract_template('by-date-activity')
+        self.by_date_total_row_template = self._extract_template("by-date-category")
+
+        self.by_date_template = self._extract_template('by-date')
+
+        self.fact_rows = []
+
+    def _extract_template(self, name):
+        pattern = re.compile('<%s>(.*)</%s>' % (name, name), re.DOTALL)
+
+        match = pattern.search(self.main_template)
+
+        if match:
+            self.main_template = self.main_template.replace(match.group(), "<the-actual-%s>" % name)
+            return match.groups()[0]
+
+        return ""
+
+    def _replace_template(self, target, name, value):
+        return target.replace('<the-actual-%s>' % name, value)
 
-        return contents
 
 
     def _write_fact(self, report, fact):
@@ -274,16 +294,20 @@ class HTMLWriter(ReportWriter):
                                           ))
 
 
-
-            by_date.append(Template(self.by_date_template).safe_substitute(
+            res = Template(self.by_date_template).safe_substitute(
                            dict(date = fact["date"].strftime(
                                        # date column format for each row in HTML report
                                        # Using python datetime formatting syntax. See:
                                        # http://docs.python.org/library/time.html#time.strftime
-                                       C_("html report","%b %d, %Y")),
-                                by_date_rows = "\n".join(by_date_rows),
-                                by_date_total_rows = "\n".join(by_date_total_rows),
-                           )))
+                                       C_("html report","%b %d, %Y"))
+                           ))
+
+            res = self._replace_template(res, 'by-date-activity', "\n".join(by_date_rows))
+            res = self._replace_template(res, 'by-date-category', "\n".join(by_date_total_rows))
+
+            by_date.append(res)
+
+
 
         data = dict(
             title = self.title,
@@ -302,14 +326,16 @@ class HTMLWriter(ReportWriter):
             header_duration = _("Duration"),
             header_description = _("Description"),
 
-            all_record_rows = "\n".join(self.fact_rows),
-            by_date_rows = "\n".join(by_date),
-
             data_dir = runtime.data_dir,
-            show_templates = _("Show templates"),
+            show_template = _("Show template"),
             template_instructions = _("You can override them by storing your version in %s" % runtime.home_data_dir),
         )
 
-        report.write(Template(self.main_template).safe_substitute(data))
+        self.main_template = Template(self.main_template).safe_substitute(data)
+        self.main_template = self._replace_template(self.main_template, 'all-activities', "\n".join(self.fact_rows))
+        self.main_template = self._replace_template(self.main_template, 'by-date', "\n".join(by_date))
+
+        report.write(self.main_template)
+
 
         return



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