annotate getan/project.py @ 369:c18bd0042eda

Empty workpackage defaults to "-" instead of "----" The length of workpackage names is varying, so default to a single easily replaceable character.
author Thomas Arendsen Hein <thomas@intevation.de>
date Mon, 03 Mar 2014 15:49:22 +0100
parents bc12acbff143
children 8b44243b799a
rev   line source
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
1 #!/usr/bin/env python
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
3 #
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
4 # (c) 2008, 2009, 2010 by
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
5 # Sascha L. Teichmann <sascha.teichmann@intevation.de>
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
6 # Ingo Weinzierl <ingo.weinzierl@intevation.de>
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
7 #
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
8 # This is Free Software licensed unter the terms of GPLv3 or later.
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
9 # For details see LICENSE coming with the source of 'getan'.
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
10
145
ccd47a2d37a6 Carefully handle non unicode strings for urwid
Björn Ricks <bjoern.ricks@intevation.de>
parents: 137
diff changeset
11 import locale
355
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
12 import re
145
ccd47a2d37a6 Carefully handle non unicode strings for urwid
Björn Ricks <bjoern.ricks@intevation.de>
parents: 137
diff changeset
13
354
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
14 from datetime import datetime, timedelta
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
15
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
16
136
92b7582b8f44 Use new style classes
Björn Ricks <bjoern.ricks@intevation.de>
parents: 135
diff changeset
17 class Project(object):
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
18
352
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
19 def __init__(self, backend, id, key, desc, total):
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
20 self.backend = backend
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
21 self.id = id
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
22 self.key = key
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
23 self.desc = desc
352
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
24 self._entries = None
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
25 self.total = total
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
26 self.start = None
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
27 self.stop = None
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
28
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
29 def year(self):
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
30 total = 0
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
31 now = datetime.now()
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
32 for entry in self.entries:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
33 start = entry.start
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
34 if start.year == now.year:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
35 total += (entry.end - start).seconds
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
36 return total
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
37
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
38 def month(self):
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
39 total = 0
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
40 now = datetime.now()
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
41 for entry in self.entries:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
42 start = entry.start
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
43 if start.month == now.month and start.year == now.year:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
44 total += (entry.end - start).seconds
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
45 return total
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
46
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
47 def week(self):
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
48 total = 0
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
49 now = datetime.now()
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
50 tweek = now.strftime('%W')
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
51 for entry in self.entries:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
52 start = entry.start
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
53 if start.strftime('%W') == tweek and start.year == now.year:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
54 total += (entry.end - start).seconds
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
55 return total
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
56
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
57 def day(self):
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
58 total = 0
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
59 now = datetime.now()
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
60 for entry in self.entries:
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
61 start = entry.start
135
ce707fbb9666 Change coding style of if clauses
Björn Ricks <bjoern.ricks@intevation.de>
parents: 23
diff changeset
62 if start.month == now.month and start.year == now.year \
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
63 and start.day == now.day:
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
64 total += (entry.end - start).seconds
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
65 return total
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
66
352
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
67 def load_entries(self, year=None, week=None):
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
68 self._entries = self.backend.load_entries(self.id, year, week)
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
69
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
70 @property
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
71 def entries(self):
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
72 if self._entries is None:
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
73 self.load_entries()
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
74 return self._entries
2e7885dc6669 Add lazy loading of Project Entries
Björn Ricks <bjoern.ricks@intevation.de>
parents: 311
diff changeset
75
354
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
76 def get_total_duration(self):
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
77 dur = timedelta(0)
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
78 for entry in self.entries:
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
79 dur += entry.get_duration()
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
80 return dur
4f5094f3f615 Add method to calculate the total duration of a project
Björn Ricks <bjoern.ricks@intevation.de>
parents: 352
diff changeset
81
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
82
136
92b7582b8f44 Use new style classes
Björn Ricks <bjoern.ricks@intevation.de>
parents: 135
diff changeset
83 class Entry(object):
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
84
355
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
85 WORKPACKAGE = re.compile("^\[(\w*)(\s|\])")
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
86
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
87 def __init__(self, id, project_id, start, end, desc):
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
88 self.id = id
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
89 self.project_id = project_id
311
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
90 self.start = start
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
91 self.end = end
349b4571e120 Fix codingstyle for pep8
Björn Ricks <bjoern.ricks@intevation.de>
parents: 293
diff changeset
92 self.desc = desc
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
93
145
ccd47a2d37a6 Carefully handle non unicode strings for urwid
Björn Ricks <bjoern.ricks@intevation.de>
parents: 137
diff changeset
94 # carefully handle non unicode string
ccd47a2d37a6 Carefully handle non unicode strings for urwid
Björn Ricks <bjoern.ricks@intevation.de>
parents: 137
diff changeset
95 # urwid seems to have some issue with plain str
147
8cc1f83d32ee Description may be not set
Björn Ricks <bjoern.ricks@intevation.de>
parents: 145
diff changeset
96 if self.desc and not isinstance(self.desc, unicode):
145
ccd47a2d37a6 Carefully handle non unicode strings for urwid
Björn Ricks <bjoern.ricks@intevation.de>
parents: 137
diff changeset
97 self.desc = unicode(self.desc, locale.getpreferredencoding())
ccd47a2d37a6 Carefully handle non unicode strings for urwid
Björn Ricks <bjoern.ricks@intevation.de>
parents: 137
diff changeset
98
355
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
99 c = self.desc
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
100 m = self.WORKPACKAGE.match(c)
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
101 if m:
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
102 self.workpackage = m.group(1)
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
103 c = c[m.end():].strip()
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
104 else:
369
c18bd0042eda Empty workpackage defaults to "-" instead of "----"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 355
diff changeset
105 self.workpackage = "-"
355
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
106 c = c.replace('\x1b', '')
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
107 self.comment = c
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
108
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
109 def get_workpackage(self):
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
110 return self.workpackage
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
111
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
112 def get_duration(self):
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
113 return (self.end - self.start)
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
114
355
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
115 def get_comment(self):
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
116 return self.comment
bc12acbff143 Add methods to return a workpackage [wp] from a Entry description
Björn Ricks <bjoern.ricks@intevation.de>
parents: 354
diff changeset
117
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
118 def __str__(self):
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
119 return ("[%s | %s | %s | %s | %s]" %
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
120 (self.id, self.project_id, self.start, self.end, self.desc))
137
5a4946fd9e6d Update whitespace
Björn Ricks <bjoern.ricks@intevation.de>
parents: 136
diff changeset
121
23
9c4e8ba3c4fa Added a new implementation of 'getan' based on urwid, a python console user interface library.
Ingo Weinzierl <ingo_weinzierl@web.de>
parents:
diff changeset
122 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)