view contrib/zeitsort.py @ 536:a59622c06936

Fix generating reports if no week given unix_week() cannot handle week being None but does so for year. It just assumes the current year in this case, although this only duplicates the initialisation of year with c_year in this context. week being None gives TypeError: unsupported operand type(s) for -: 'NoneType' and 'int' load_entries() handles the case with no week given, thus call it anyhow. This fix makes it possible to invoke scripts/getan-report with a year given but no week and seeing the expected result (data for a whole year) instead of a traceback.
author Tom Gottfried <tom@intevation.de>
date Thu, 09 Jan 2020 11:32:34 +0100
parents d80f40d239d2
children
line wrap: on
line source
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# zeitsort
# --------
# (c) 2008 by Sascha L. Teichmann <sascha.teichmann@intevation.de>
#
# Simple script which sorts lines of zeiterfassung.txt files by date.
#
# This is Free Software licensed under the terms of GPLv3 or later.
# For details see LICENSE coming with the source of 'getan'.
#

import sys
import re

from datetime import date

DATE = re.compile("(\d\d)\.(\d\d)\.(\d\d\d\d)")

def date_cmp(a, b):
    ma = DATE.search(a)
    mb = DATE.search(b)
    if not ma and not mb: return cmp(a, b)
    if ma and not mb: return -1
    if not ma and mb: return +1
    da = date(int(ma.group(3)), int(ma.group(2)), int(ma.group(1)))
    db = date(int(mb.group(3)), int(mb.group(2)), int(mb.group(1)))
    return cmp(da, db)

def main():
    all = []
    while True:
        line = sys.stdin.readline()
        if not line: break
        if not DATE.search(line):
            # handle multi lines
            if not all: all.append(line)
            else:       all[-1] += line
        else:
            all.append(line)
    all.sort(date_cmp)
    sys.stdout.write(''.join(all))
    sys.stdout.flush()

if __name__ == '__main__':
    main()

# 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)