comparison contrib/zeitsort.py @ 486:d80f40d239d2

renamed scripts. getan-report and getan-day-report now installed as a script. old script moved to done/contrib
author Magnus Schieder <mschieder@intevation.de>
date Fri, 29 Jun 2018 18:38:55 +0200
parents scripts/zeitsort.py@999a438474f2
children
comparison
equal deleted inserted replaced
485:726206815059 486:d80f40d239d2
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 # zeitsort
5 # --------
6 # (c) 2008 by Sascha L. Teichmann <sascha.teichmann@intevation.de>
7 #
8 # Simple script which sorts lines of zeiterfassung.txt files by date.
9 #
10 # This is Free Software licensed under the terms of GPLv3 or later.
11 # For details see LICENSE coming with the source of 'getan'.
12 #
13
14 import sys
15 import re
16
17 from datetime import date
18
19 DATE = re.compile("(\d\d)\.(\d\d)\.(\d\d\d\d)")
20
21 def date_cmp(a, b):
22 ma = DATE.search(a)
23 mb = DATE.search(b)
24 if not ma and not mb: return cmp(a, b)
25 if ma and not mb: return -1
26 if not ma and mb: return +1
27 da = date(int(ma.group(3)), int(ma.group(2)), int(ma.group(1)))
28 db = date(int(mb.group(3)), int(mb.group(2)), int(mb.group(1)))
29 return cmp(da, db)
30
31 def main():
32 all = []
33 while True:
34 line = sys.stdin.readline()
35 if not line: break
36 if not DATE.search(line):
37 # handle multi lines
38 if not all: all.append(line)
39 else: all[-1] += line
40 else:
41 all.append(line)
42 all.sort(date_cmp)
43 sys.stdout.write(''.join(all))
44 sys.stdout.flush()
45
46 if __name__ == '__main__':
47 main()
48
49 # 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)