bjoern@356: # -*- coding: utf-8 -*- bjoern@356: # bjoern@356: # (c) 2014 by Björn Ricks tom@536: # 2017, 2018, 2020 Intevation GmbH bjoern@356: # bjoern@356: # This is Free Software licensed under the terms of GPLv3 or later. bjoern@356: # For details see LICENSE coming with the source of 'getan'. bjoern@356: bernhard@412: # import logging bjoern@356: import os.path bjoern@356: import sys bjoern@356: bjoern@356: from datetime import date, datetime, timedelta bjoern@356: bjoern@356: from jinja2 import Environment, ChoiceLoader, FileSystemLoader, PackageLoader bjoern@356: bjoern@356: from getan.backend import Backend, DEFAULT_DATABASE bjoern@356: bernhard@412: # logging.basicConfig(level='DEBUG') # quick fix until getan-eval.py offers it bernhard@412: # logger = logging.getLogger() bernhard@412: bjoern@356: bjoern@356: def human_time(delta): bjoern@356: days = delta.days bjoern@356: seconds = days * 3600 * 24 bjoern@356: s = seconds + delta.seconds mschieder@511: h = s // 3600 mschieder@511: m = (s % 3600) // 60 bjoern@356: if (s % 60) >= 30: bjoern@356: m += 1 bjoern@356: if m == 60: bjoern@356: m = 0 bjoern@356: h += 1 thomas@370: return "%d:%02d" % (h, m) bjoern@356: bjoern@356: bjoern@356: def date_format(d): bjoern@356: return d.strftime("%d.%m.%Y") bjoern@356: bjoern@356: bjoern@389: def duration(entries): bjoern@356: total = timedelta(0) bjoern@389: for entry in entries: bjoern@389: total += entry.get_duration() bjoern@356: return total bjoern@356: bjoern@356: bjoern@356: def unix_week(week, year=None): bjoern@356: """Convert iso week to unix week bjoern@356: bjoern@356: For unix week see man date "%W" bjoern@356: Args: bjoern@356: week: Week number as int to convert bjoern@356: year: Year as int. If year is none the current year is used. bjoern@356: """ bjoern@356: if not year: bjoern@356: year = datetime.now().year bjoern@356: firstday = date(year, 1, 4) bjoern@356: isoweek = firstday.isocalendar()[1] bjoern@356: unixweek = int(firstday.strftime("%W")) bjoern@356: diff = isoweek - unixweek bjoern@356: return week - diff bjoern@356: bjoern@356: bjoern@356: def render(template, database=None, year=None, week=None, project=None, bjoern@360: user=None, empty_projects=True): bjoern@356: if not user: bernhard@431: user = os.getenv("USER") or "USER" bjoern@356: bjoern@356: if not database: bjoern@356: if os.path.isfile(DEFAULT_DATABASE): bjoern@356: database = os.path.abspath(DEFAULT_DATABASE) bjoern@356: else: bjoern@356: database = os.path.expanduser(os.path.join("~", ".getan", bjoern@356: DEFAULT_DATABASE)) bjoern@356: if not os.path.isfile(database): mschieder@467: print("'%s' does not exist or is not a file." % database, file=sys.stderr) bjoern@356: sys.exit(1) bjoern@356: bjoern@356: u_week = None bjoern@356: bjoern@356: c_year = int(date.today().strftime("%Y")) bjoern@356: c_week = datetime.now().isocalendar()[1] bjoern@356: bjoern@356: if week is not None and year is None: bjoern@356: year = c_year bjoern@356: bjoern@356: if not os.path.isfile(database): mschieder@467: print("'%s' does not exist or is not a file." % database, file=sys.stderr) bjoern@356: sys.exit(1) bjoern@356: bjoern@356: loader = ChoiceLoader([FileSystemLoader(os.path.expanduser(os.path.join( bjoern@356: "~", ".getan", "templates"))), bjoern@356: PackageLoader("getan")]) bjoern@356: bjoern@356: env = Environment(loader=loader) bjoern@356: env.filters["human_time"] = human_time bjoern@356: env.filters["date_format"] = date_format bjoern@389: env.filters["duration"] = duration bjoern@356: bjoern@356: template_name = template or "wochenbericht" bjoern@356: template = env.get_template(template_name) bjoern@356: bjoern@356: backend = Backend(database) bjoern@356: bjoern@356: if not project: bjoern@356: projects = backend.load_projects() bjoern@356: else: bjoern@356: project = backend.load_project(project) bjoern@356: projects = [project] bjoern@356: tom@536: if week is not None: bjoern@356: u_week = "%02d" % unix_week(week, year) tom@536: tom@536: for project in projects: tom@536: project.load_entries(year, u_week) bjoern@356: bjoern@360: if not empty_projects: bjoern@360: projects = [project for project in projects if project.entries] bjoern@360: bjoern@389: entries = [] bjoern@389: for project in projects: bjoern@389: entries.extend(project.entries) bjoern@389: bjoern@356: context = dict() bjoern@389: context["entries"] = entries bjoern@356: context["project"] = project bjoern@356: context["projects"] = projects bjoern@356: context["user"] = user bjoern@356: context["database"] = database bjoern@356: context["year"] = year bjoern@356: context["week"] = week bjoern@356: context["current_week"] = c_week bjoern@356: context["current_year"] = c_year bjoern@356: context["unix_week"] = u_week bjoern@389: context["total_time"] = duration(entries) bjoern@372: context["today"] = date.today() bjoern@356: return template.render(context)