mschieder@482: #!/usr/bin/env python3 ingo_weinzierl@23: # -*- coding: utf-8 -*- ingo_weinzierl@23: # ingo_weinzierl@23: # (c) 2008, 2009, 2010 by ingo_weinzierl@23: # Sascha L. Teichmann ingo_weinzierl@23: # Ingo Weinzierl bernhard@413: # (c) 2017 by Intevation GmbH bernhard@413: # Authors: bernhard@413: # * Sascha L. Teichmann bernhard@413: # * Ingo Weinzierl bernhard@445: # * Bernhard Reiter ingo_weinzierl@23: # ingo_weinzierl@23: # This is Free Software licensed unter the terms of GPLv3 or later. ingo_weinzierl@23: # For details see LICENSE coming with the source of 'getan'. ingo_weinzierl@23: bjoern@355: import re bjoern@145: bjoern@354: from datetime import datetime, timedelta ingo_weinzierl@23: bjoern@311: bjoern@136: class Project(object): ingo_weinzierl@23: bjoern@352: def __init__(self, backend, id, key, desc, total): bjoern@352: self.backend = backend bjoern@311: self.id = id bjoern@311: self.key = key bjoern@311: self.desc = desc bjoern@352: self._entries = None bjoern@311: self.total = total bjoern@311: self.start = None bjoern@311: self.stop = None mschieder@469: self.open= None ingo_weinzierl@23: mschieder@478: def update_total(self): mschieder@478: total = 0 mschieder@478: for entry in self.entries: mschieder@478: total += (entry.end - entry.start).seconds mschieder@478: self.total = total mschieder@478: ingo_weinzierl@23: def year(self): ingo_weinzierl@23: total = 0 bjoern@311: now = datetime.now() ingo_weinzierl@23: for entry in self.entries: ingo_weinzierl@23: start = entry.start ingo_weinzierl@23: if start.year == now.year: ingo_weinzierl@23: total += (entry.end - start).seconds ingo_weinzierl@23: return total ingo_weinzierl@23: ingo_weinzierl@23: def month(self): ingo_weinzierl@23: total = 0 bjoern@311: now = datetime.now() ingo_weinzierl@23: for entry in self.entries: ingo_weinzierl@23: start = entry.start ingo_weinzierl@23: if start.month == now.month and start.year == now.year: ingo_weinzierl@23: total += (entry.end - start).seconds ingo_weinzierl@23: return total ingo_weinzierl@23: ingo_weinzierl@23: def week(self): ingo_weinzierl@23: total = 0 bjoern@311: now = datetime.now() ingo_weinzierl@23: tweek = now.strftime('%W') ingo_weinzierl@23: for entry in self.entries: ingo_weinzierl@23: start = entry.start ingo_weinzierl@23: if start.strftime('%W') == tweek and start.year == now.year: ingo_weinzierl@23: total += (entry.end - start).seconds ingo_weinzierl@23: return total ingo_weinzierl@23: ingo_weinzierl@23: def day(self): ingo_weinzierl@23: total = 0 bjoern@311: now = datetime.now() ingo_weinzierl@23: for entry in self.entries: ingo_weinzierl@23: start = entry.start bjoern@135: if start.month == now.month and start.year == now.year \ bjoern@311: and start.day == now.day: ingo_weinzierl@23: total += (entry.end - start).seconds ingo_weinzierl@23: return total ingo_weinzierl@23: bjoern@352: def load_entries(self, year=None, week=None): bjoern@352: self._entries = self.backend.load_entries(self.id, year, week) bjoern@352: bjoern@352: @property bjoern@352: def entries(self): bjoern@352: if self._entries is None: bjoern@352: self.load_entries() bjoern@352: return self._entries bjoern@352: bjoern@354: def get_total_duration(self): bjoern@354: dur = timedelta(0) bjoern@354: for entry in self.entries: bjoern@354: dur += entry.get_duration() bjoern@354: return dur bjoern@354: ingo_weinzierl@23: bjoern@136: class Entry(object): ingo_weinzierl@23: bernhard@398: WORKPACKAGE = re.compile("^\[([^\s\]]+)(\s|\])") bjoern@355: ingo_weinzierl@23: def __init__(self, id, project_id, start, end, desc): bjoern@311: self.id = id ingo_weinzierl@23: self.project_id = project_id bjoern@311: self.start = start bjoern@311: self.end = end bjoern@311: self.desc = desc bjoern@371: self.workpackage = "-" mschieder@469: self.open = None ingo_weinzierl@23: bernhard@413: # we add this attribute for use in jinja2 templates, bernhard@413: # as filters like sort() or groupby() work only on attributes bernhard@413: # and sorting or grouping by day is common for reporting bernhard@413: self.startisoday = start.date().isoformat() bernhard@413: bjoern@355: c = self.desc bjoern@371: if c: bjoern@371: m = self.WORKPACKAGE.match(c) bjoern@371: if m: bjoern@371: self.workpackage = m.group(1) bjoern@371: c = c[m.end():].strip() bjoern@371: c = c.replace('\x1b', '') bjoern@355: self.comment = c bjoern@355: bjoern@355: def get_workpackage(self): bjoern@355: return self.workpackage bjoern@355: bjoern@355: def get_duration(self): ingo_weinzierl@23: return (self.end - self.start) ingo_weinzierl@23: bjoern@355: def get_comment(self): bjoern@355: return self.comment bjoern@355: ingo_weinzierl@23: def __str__(self): ingo_weinzierl@23: return ("[%s | %s | %s | %s | %s]" % bjoern@388: (self.id, self.project_id, self.start, self.end, self.desc)) bjoern@137: ingo_weinzierl@23: # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: