teichmann@15: #!/usr/bin/env python
teichmann@15: # -*- coding: utf-8 -*-
teichmann@15: #
teichmann@15: # zeitsort
teichmann@15: # --------
teichmann@15: # (c) 2008 by Sascha L. Teichmann <sascha.teichmann@intevation.de>
teichmann@15: #
teichmann@15: # Simple script which sorts lines of zeiterfassung.txt files by date.
teichmann@15: #
teichmann@15: # This is Free Software licensed under the terms of GPLv3 or later.
teichmann@15: # For details see LICENSE coming with the source of 'getan'.
teichmann@15: #
teichmann@15: 
teichmann@15: import sys
teichmann@15: import re
teichmann@15: 
teichmann@15: from datetime import date
teichmann@15: 
teichmann@15: DATE = re.compile("(\d\d)\.(\d\d)\.(\d\d\d\d)")
teichmann@15: 
teichmann@15: def date_cmp(a, b):
teichmann@15:     ma = DATE.search(a)
teichmann@15:     mb = DATE.search(b)
teichmann@15:     if not ma and not mb: return cmp(a, b)
teichmann@15:     if ma and not mb: return -1
teichmann@15:     if not ma and mb: return +1
teichmann@15:     da = date(int(ma.group(3)), int(ma.group(2)), int(ma.group(1)))
teichmann@15:     db = date(int(mb.group(3)), int(mb.group(2)), int(mb.group(1)))
teichmann@15:     return cmp(da, db)
teichmann@15: 
teichmann@15: def main():
teichmann@15:     all = []
teichmann@15:     while True:
teichmann@15:         line = sys.stdin.readline()
teichmann@15:         if not line: break
teichmann@15:         if not DATE.search(line):
teichmann@15:             # handle multi lines
teichmann@15:             if not all: all.append(line)
teichmann@15:             else:       all[-1] += line
teichmann@15:         else:
teichmann@15:             all.append(line)
teichmann@15:     all.sort(date_cmp)
teichmann@15:     sys.stdout.write(''.join(all))
teichmann@15:     sys.stdout.flush()
teichmann@15: 
teichmann@15: if __name__ == '__main__':
teichmann@15:     main()
teichmann@15: 
teichmann@15: # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: