Mercurial > getan
comparison scripts/zeitsort.py @ 380:20fde79f8e12
Move all scripts in a common scripts directory
Currently console scripts were kept in several directories. Now use a common
directory for all scripts.
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Mon, 05 Jan 2015 10:54:20 +0100 |
parents | getan/contrib/zeitsort.py@804211b988aa |
children | 999a438474f2 |
comparison
equal
deleted
inserted
replaced
379:47890f38e558 | 380:20fde79f8e12 |
---|---|
1 #!/usr/bin/env python | |
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: |