Mercurial > getan
annotate contrib/zeitsort @ 17:2712fc2b8276
human_seconds: Use timespec instead of seconds and document it.
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Tue, 18 Aug 2009 15:45:12 +0200 |
parents | 0bb0100b2c8d |
children |
rev | line source |
---|---|
15
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
3 # |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
4 # zeitsort |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
5 # -------- |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
6 # (c) 2008 by Sascha L. Teichmann <sascha.teichmann@intevation.de> |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
7 # |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
8 # Simple script which sorts lines of zeiterfassung.txt files by date. |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
9 # |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
10 # This is Free Software licensed under the terms of GPLv3 or later. |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
11 # For details see LICENSE coming with the source of 'getan'. |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
12 # |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
13 |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
14 import sys |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
15 import re |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
16 |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
17 from datetime import date |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
18 |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
19 DATE = re.compile("(\d\d)\.(\d\d)\.(\d\d\d\d)") |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
20 |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
21 def date_cmp(a, b): |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
22 ma = DATE.search(a) |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
23 mb = DATE.search(b) |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
24 if not ma and not mb: return cmp(a, b) |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
25 if ma and not mb: return -1 |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
26 if not ma and mb: return +1 |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
27 da = date(int(ma.group(3)), int(ma.group(2)), int(ma.group(1))) |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
28 db = date(int(mb.group(3)), int(mb.group(2)), int(mb.group(1))) |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
29 return cmp(da, db) |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
30 |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
31 def main(): |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
32 all = [] |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
33 while True: |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
34 line = sys.stdin.readline() |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
35 if not line: break |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
36 if not DATE.search(line): |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
37 # handle multi lines |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
38 if not all: all.append(line) |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
39 else: all[-1] += line |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
40 else: |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
41 all.append(line) |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
42 all.sort(date_cmp) |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
43 sys.stdout.write(''.join(all)) |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
44 sys.stdout.flush() |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
45 |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
46 if __name__ == '__main__': |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
47 main() |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
48 |
0bb0100b2c8d
Added a script to sort zeiterfassung.txt files by date. Useful to merge them.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
49 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |