Mercurial > getan
annotate contrib/zeiterfassung @ 46:5f87604ea5ed
add year option for contrib/zeiterfassung
default for year is the current year
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Mon, 07 Feb 2011 11:28:25 +0100 |
parents | 22aa74768d97 |
children | 3f97954868e4 |
rev | line source |
---|---|
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
3 # |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
4 # zeiterfassung |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
5 # ------------- |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
6 # (c) 2008 by Sascha L. Teichmann <sascha.teichmann@intevation.de> |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
7 # |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
8 # Simple script which generates lines for zeiterfassung.txt files. |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
9 # |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
10 # This is Free Software licensed under the terms of GPLv3 or later. |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
11 # For details see LICENSE coming with the source of 'getan'. |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
12 # |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
13 import os |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
14 import os.path |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
15 import sys |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
16 import getopt |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
17 import re |
10
4f782a05b4dc
contrib/zeiterfassung: Added parameter -e | --encoding=
Sascha L. Teichmann <teichmann@intevation.de>
parents:
9
diff
changeset
|
18 import codecs |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
19 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
20 from pysqlite2 import dbapi2 as db |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
21 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
22 from datetime import date |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
23 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
24 DEFAULT_DATABASE = "time.db" |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
25 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
26 TYPE_OF_ENTRY = "-" |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
27 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
28 WORKPACKAGE = re.compile("^\[(\w*)(\s|\])") |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
29 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
30 USAGE = '''usage: %s <options> |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
31 with <options> |
9
4e8f5545256d
* Applied and completed Stephan Holl's zeiterfassung.bessere-lesbarkeit.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
8
diff
changeset
|
32 [--user=|-u <user>] : Name of user, default: $USER |
4e8f5545256d
* Applied and completed Stephan Holl's zeiterfassung.bessere-lesbarkeit.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
8
diff
changeset
|
33 [--database=|-d <database>]: getan database, default: time.db |
4e8f5545256d
* Applied and completed Stephan Holl's zeiterfassung.bessere-lesbarkeit.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
8
diff
changeset
|
34 [--project=|-p <key>] : Key of output project, default: all |
10
4f782a05b4dc
contrib/zeiterfassung: Added parameter -e | --encoding=
Sascha L. Teichmann <teichmann@intevation.de>
parents:
9
diff
changeset
|
35 [--encoding=|-e encoding] : encoding of output, default: none |
13
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
36 [--week=]|-w <week>] : week of year |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
37 [--list|-l] : list all projects |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
38 [--help|-h] : This text''' |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
39 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
40 LIST_PROJECTS = ''' |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
41 SELECT key, description, active FROM projects |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
42 ''' |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
43 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
44 PROJECT_ID_BY_KEY = ''' |
9
4e8f5545256d
* Applied and completed Stephan Holl's zeiterfassung.bessere-lesbarkeit.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
8
diff
changeset
|
45 SELECT id, description FROM projects where key = :key |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
46 ''' |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
47 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
48 ALL_PROJECT_IDS = ''' |
9
4e8f5545256d
* Applied and completed Stephan Holl's zeiterfassung.bessere-lesbarkeit.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
8
diff
changeset
|
49 SELECT id, key, description FROM projects |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
50 ''' |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
51 |
13
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
52 WEEK_ENTRIES = ''' |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
53 SELECT |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
54 date(start_time) AS t, |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
55 sum(strftime('%s', stop_time) - strftime('%s', start_time)), |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
56 'no description' AS description |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
57 FROM entries |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
58 WHERE |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
59 project_id = :project_id AND |
46
5f87604ea5ed
add year option for contrib/zeiterfassung
Bjoern Ricks <bricks@intevation.de>
parents:
16
diff
changeset
|
60 (strftime('%Y', start_time) ) = :year AND |
13
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
61 (description IS NULL or length(description) = 0) -- trim() function is missing |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
62 AND (strftime('%W', start_time) = :week OR strftime('%W', stop_time) = :week) |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
63 GROUP BY round(julianday(start_time)) |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
64 UNION |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
65 SELECT date(start_time) AS s, strftime('%s', stop_time) - strftime('%s', start_time), description |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
66 FROM entries |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
67 WHERE |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
68 project_id = :project_id AND |
46
5f87604ea5ed
add year option for contrib/zeiterfassung
Bjoern Ricks <bricks@intevation.de>
parents:
16
diff
changeset
|
69 (strftime('%Y', start_time) ) = :year AND |
13
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
70 description IS NOT NULL AND length(description) > 0 |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
71 AND (strftime('%W', start_time) = :week OR strftime('%W', stop_time) = :week) |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
72 ORDER BY t |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
73 ''' |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
74 |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
75 ENTRIES = ''' |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
76 SELECT |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
77 date(start_time), |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
78 sum(strftime('%s', stop_time) - strftime('%s', start_time)), |
11
feb6bb4427fe
Applied a variation of Stephan Holl's zeiterfassung.non-utf8-chars-in-db.patch to force the encoding given by --encoding=.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
10
diff
changeset
|
79 'no description' AS description |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
80 FROM entries |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
81 WHERE |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
82 project_id = :project_id AND |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
83 (description IS NULL or length(description) = 0) -- trim() function is missing |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
84 GROUP BY round(julianday(start_time)) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
85 UNION |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
86 SELECT date(start_time), strftime('%s', stop_time) - strftime('%s', start_time), description |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
87 FROM entries |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
88 WHERE |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
89 project_id = :project_id AND |
46
5f87604ea5ed
add year option for contrib/zeiterfassung
Bjoern Ricks <bricks@intevation.de>
parents:
16
diff
changeset
|
90 (strftime('%Y', start_time) ) = :year AND |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
91 description IS NOT NULL AND length(description) > 0 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
92 ''' |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
93 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
94 def human_time(s): |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
95 h = s / 3600 |
11
feb6bb4427fe
Applied a variation of Stephan Holl's zeiterfassung.non-utf8-chars-in-db.patch to force the encoding given by --encoding=.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
10
diff
changeset
|
96 m = (s % 3600) / 60 |
feb6bb4427fe
Applied a variation of Stephan Holl's zeiterfassung.non-utf8-chars-in-db.patch to force the encoding given by --encoding=.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
10
diff
changeset
|
97 if (s % 60) >= 30: |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
98 m += 1 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
99 if m == 60: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
100 m = 0 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
101 h += 1 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
102 return "%2d:%02d" % (h, m) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
103 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
104 class TermError(Exception): |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
105 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
106 def __init__(self, msg): |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
107 Exception.__init__(self) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
108 self.msg = msg |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
109 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
110 def __str__(self): |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
111 return repr(self.msg) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
112 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
113 def tolerantClose(c): |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
114 if c: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
115 try: c.close() |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
116 except: pass |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
117 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
118 def usage(exit_code = 0): |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
119 print USAGE % sys.argv[0] |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
120 sys.exit(exit_code) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
121 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
122 def main(): |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
123 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
124 database = DEFAULT_DATABASE |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
125 user = None |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
126 list_projects = False |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
127 project = None |
10
4f782a05b4dc
contrib/zeiterfassung: Added parameter -e | --encoding=
Sascha L. Teichmann <teichmann@intevation.de>
parents:
9
diff
changeset
|
128 encoding = None |
13
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
129 week = None |
46
5f87604ea5ed
add year option for contrib/zeiterfassung
Bjoern Ricks <bricks@intevation.de>
parents:
16
diff
changeset
|
130 year = None |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
131 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
132 opts, args = getopt.getopt( |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
133 sys.argv[1:], |
13
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
134 'd:u:p:e:hl:w:', |
46
5f87604ea5ed
add year option for contrib/zeiterfassung
Bjoern Ricks <bricks@intevation.de>
parents:
16
diff
changeset
|
135 ['database=', 'user=', 'project=', 'encoding=', 'help', 'list', 'week=', 'year=']) |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
136 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
137 for opt, val in opts: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
138 if opt in ("--database", "-d"): |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
139 database = val |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
140 elif opt in ("--user", "-u"): |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
141 user = val |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
142 elif opt in ("--project", "-p"): |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
143 project = val |
10
4f782a05b4dc
contrib/zeiterfassung: Added parameter -e | --encoding=
Sascha L. Teichmann <teichmann@intevation.de>
parents:
9
diff
changeset
|
144 elif opt in ("--encoding", "-e"): |
4f782a05b4dc
contrib/zeiterfassung: Added parameter -e | --encoding=
Sascha L. Teichmann <teichmann@intevation.de>
parents:
9
diff
changeset
|
145 encoding = val |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
146 elif opt in ("--help", "-h"): |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
147 usage() |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
148 elif opt in ("--list", "-l"): |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
149 list_projects = True |
13
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
150 elif opt in ("--week", "-w"): |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
151 week = val |
46
5f87604ea5ed
add year option for contrib/zeiterfassung
Bjoern Ricks <bricks@intevation.de>
parents:
16
diff
changeset
|
152 elif opt in ("--year", "-y"): |
5f87604ea5ed
add year option for contrib/zeiterfassung
Bjoern Ricks <bricks@intevation.de>
parents:
16
diff
changeset
|
153 year = val |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
154 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
155 if not user: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
156 user = os.getenv("USER") |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
157 |
46
5f87604ea5ed
add year option for contrib/zeiterfassung
Bjoern Ricks <bricks@intevation.de>
parents:
16
diff
changeset
|
158 if not year: |
5f87604ea5ed
add year option for contrib/zeiterfassung
Bjoern Ricks <bricks@intevation.de>
parents:
16
diff
changeset
|
159 year = date.today().strftime("%Y") |
5f87604ea5ed
add year option for contrib/zeiterfassung
Bjoern Ricks <bricks@intevation.de>
parents:
16
diff
changeset
|
160 |
10
4f782a05b4dc
contrib/zeiterfassung: Added parameter -e | --encoding=
Sascha L. Teichmann <teichmann@intevation.de>
parents:
9
diff
changeset
|
161 if encoding: |
4f782a05b4dc
contrib/zeiterfassung: Added parameter -e | --encoding=
Sascha L. Teichmann <teichmann@intevation.de>
parents:
9
diff
changeset
|
162 Writer = codecs.getwriter(encoding) |
4f782a05b4dc
contrib/zeiterfassung: Added parameter -e | --encoding=
Sascha L. Teichmann <teichmann@intevation.de>
parents:
9
diff
changeset
|
163 sys.stdout = Writer(sys.stdout) |
4f782a05b4dc
contrib/zeiterfassung: Added parameter -e | --encoding=
Sascha L. Teichmann <teichmann@intevation.de>
parents:
9
diff
changeset
|
164 |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
165 if not os.path.isfile(database): |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
166 print >> sys.stderr, "'%s' does not exist or is not a file." % database |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
167 sys.exit(1) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
168 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
169 con, cur = None, None |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
170 try: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
171 try: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
172 con = db.connect(database) |
11
feb6bb4427fe
Applied a variation of Stephan Holl's zeiterfassung.non-utf8-chars-in-db.patch to force the encoding given by --encoding=.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
10
diff
changeset
|
173 if encoding: |
feb6bb4427fe
Applied a variation of Stephan Holl's zeiterfassung.non-utf8-chars-in-db.patch to force the encoding given by --encoding=.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
10
diff
changeset
|
174 con.text_factory = lambda s: s.decode(encoding) |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
175 cur = con.cursor() |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
176 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
177 if list_projects: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
178 cur.execute(LIST_PROJECTS) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
179 while True: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
180 row = cur.fetchone() |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
181 if not row: break |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
182 print "%s %s %s" % (row[0], row[2] and "*" or "-", row[1]) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
183 else: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
184 if project: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
185 cur.execute(PROJECT_ID_BY_KEY, { 'key': project }) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
186 row = cur.fetchone() |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
187 if row is None: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
188 raise TermError("There is no project with key '%s'" % project) |
9
4e8f5545256d
* Applied and completed Stephan Holl's zeiterfassung.bessere-lesbarkeit.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
8
diff
changeset
|
189 project_ids = [[row[0], project, row[1]]] |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
190 else: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
191 cur.execute(ALL_PROJECT_IDS); |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
192 project_ids = cur.fetchall() |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
193 |
9
4e8f5545256d
* Applied and completed Stephan Holl's zeiterfassung.bessere-lesbarkeit.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
8
diff
changeset
|
194 for project_id, project, proj_desc in project_ids: |
4e8f5545256d
* Applied and completed Stephan Holl's zeiterfassung.bessere-lesbarkeit.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
8
diff
changeset
|
195 print "# project: %s (%s)" % (project, proj_desc) |
13
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
196 if not week is None: |
46
5f87604ea5ed
add year option for contrib/zeiterfassung
Bjoern Ricks <bricks@intevation.de>
parents:
16
diff
changeset
|
197 cur.execute(WEEK_ENTRIES, {'project_id': project_id, 'week': week, 'year' : year}) |
13
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
198 else: |
e347f0de5e22
Applied Stephan Holl's week-option.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
11
diff
changeset
|
199 cur.execute(ENTRIES, {'project_id': project_id}) |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
200 total = 0 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
201 while True: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
202 row = cur.fetchone() |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
203 if not row: break |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
204 d = date(*map(int, row[0].split('-'))) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
205 t = max(60, row[1]) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
206 c = row[2] |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
207 total += t |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
208 workpackage = "----" |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
209 if c: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
210 m = WORKPACKAGE.match(c) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
211 if m: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
212 workpackage = m.group(1) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
213 c = c[m.end():].strip() |
46
5f87604ea5ed
add year option for contrib/zeiterfassung
Bjoern Ricks <bricks@intevation.de>
parents:
16
diff
changeset
|
214 c = c.replace('\x1b', '') |
16
22aa74768d97
left justify user sign in zeiterfassung
Thomas Arendsen Hein <thomas@intevation.de>
parents:
13
diff
changeset
|
215 print "%s %sh %s %-3s [%s] %s" % ( |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
216 d.strftime("%d.%m.%Y"), |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
217 human_time(t), |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
218 TYPE_OF_ENTRY, |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
219 user, |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
220 workpackage, |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
221 c) |
9
4e8f5545256d
* Applied and completed Stephan Holl's zeiterfassung.bessere-lesbarkeit.patch
Sascha L. Teichmann <teichmann@intevation.de>
parents:
8
diff
changeset
|
222 print "# total: %sh\n\n" % human_time(total) |
8
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
223 finally: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
224 tolerantClose(cur) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
225 tolerantClose(con) |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
226 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
227 except TermError, e: |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
228 print >> sys.stderr, "error: %s" % e.msg |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
229 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
230 if __name__ == '__main__': |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
231 main() |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
232 |
20414d892f04
Added script to generate lines for zeiterfassung.txt
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
233 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |