changeset 358:2b2e371b9bed

Use the new getan.template module in getan-eval.py script
author Björn Ricks <bjoern.ricks@intevation.de>
date Mon, 03 Mar 2014 14:32:12 +0100
parents f13dc9fd62d5
children 5df363bba5cb
files getan/contrib/getan-eval.py
diffstat 1 files changed, 12 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/getan/contrib/getan-eval.py	Mon Mar 03 14:31:25 2014 +0100
+++ b/getan/contrib/getan-eval.py	Mon Mar 03 14:32:12 2014 +0100
@@ -1,32 +1,15 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 #
-# (c) 2013 by Björn Ricks <bjoern.ricks@intevation.de>
+# (c) 2013, 2014 by Björn Ricks <bjoern.ricks@intevation.de>
 #
 # This is Free Software licensed under the terms of GPLv3 or later.
 # For details see LICENSE coming with the source of 'getan'.
-#
-
-import os.path
-import sqlite3 as db
-import sys
 
 from datetime import date, datetime, timedelta
 from optparse import OptionParser
 
-from jinja2 import Environment, ChoiceLoader, FileSystemLoader, PackageLoader
-
-from getan.contrib.zeiterfassung import unix_week
-
-DEFAULT_DATABASE = "time.db"
-
-
-def tolerant_close(c):
-    if c:
-        try:
-            c.close()
-        except:
-            pass
+from getan.template import render
 
 
 def main():
@@ -41,65 +24,25 @@
     parser.add_option("-w", "--week", type="int", dest="week",
                       help="week of year")
     parser.add_option("-y", "--year", type="int", dest="year", help="year")
-    parser.add_option("-c", "--lastweek", dest="entries of last working week",
+    parser.add_option("-c", "--lastweek", dest="lastweek",
+                      help="entries of last working week",
                       action="store_true")
 
     (options, args) = parser.parse_args()
 
-    if os.path.isfile(DEFAULT_DATABASE):
-        database = os.path.abspath(DEFAULT_DATABASE)
-    else:
-        database = os.path.expanduser(os.path.join("~", ".getan",
-                                      DEFAULT_DATABASE))
-    if options.database:
-        database = options.database
-
-    if options.user:
-        user = options.user
+    if options.lastweek:
+        week = (datetime.now() - timedelta(7)).isocalendar()[1]
+        year = int(date.today().strftime("%Y"))
     else:
-        user = os.getenv("USER")
-
-    year = options.year
-    week = options.week
-    u_week = None
-
-    if not year:
-        year = int(date.today().strftime("%Y"))
-
-    if week is None:
-        week = (datetime.now() - timedelta(7)).isocalendar()[1]
-
-    u_week = "%02d" % unix_week(week, year)
-
-    if not os.path.isfile(database):
-        print >> sys.stderr, "'%s' does not exist or is not a file." % database
-        sys.exit(1)
-
-    loader = ChoiceLoader([FileSystemLoader(os.path.expanduser(os.path.join(
-        "~", ".getan", "templates"))),
-        PackageLoader("getan")])
-    env = Environment(loader=loader)
+        year = options.year
+        week = options.week
 
     template_name = options.template or "wochenbericht"
-    template = env.get_template(template_name)
-
-    context = dict()
-    context["user"] = user
-    context["database"] = database
-    context["year"] = year
-    context["week"] = week
-    context["unix_week"] = u_week
 
-    con = None
-    cur = None
-    try:
-        con = db.connect(database)
-        cur = con.cursor()
-    finally:
-        tolerant_close(cur)
-        tolerant_close(con)
+    print render(database=options.database, user=options.user,
+                 template=template_name, year=year, week=week,
+                 project=options.project)
 
-    print template.render(context)
 
 if __name__ == '__main__':
     main()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)