teichmann@8: #!/usr/bin/env python teichmann@8: # -*- coding: utf-8 -*- teichmann@8: # teichmann@8: # zeiterfassung teichmann@8: # ------------- teichmann@8: # (c) 2008 by Sascha L. Teichmann bjoern@128: # (c) 2011, 2012 by Björn Ricks teichmann@8: # teichmann@8: # Simple script which generates lines for zeiterfassung.txt files. teichmann@8: # teichmann@8: # This is Free Software licensed under the terms of GPLv3 or later. teichmann@8: # For details see LICENSE coming with the source of 'getan'. teichmann@8: # teichmann@8: import sys teichmann@8: import getopt teichmann@10: import codecs bjoern@127: import locale teichmann@8: bjoern@359: from datetime import datetime, timedelta teichmann@8: bjoern@359: from getan.template import render teichmann@8: teichmann@8: DEFAULT_DATABASE = "time.db" teichmann@8: bjoern@359: ZEITERFASSUNG_TEMPLATE = "zeiterfassung" teichmann@8: teichmann@8: USAGE = '''usage: %s teichmann@8: with teichmann@9: [--user=|-u ] : Name of user, default: $USER teichmann@9: [--database=|-d ]: getan database, default: time.db teichmann@9: [--project=|-p ] : Key of output project, default: all teichmann@10: [--encoding=|-e encoding] : encoding of output, default: none teichmann@13: [--week=]|-w ] : week of year bjoern@51: [--year=]|-y ] : year teichmann@8: [--list|-l] : list all projects bjoern@60: [--help|-h] : This text bjoern@108: [--emtpy|-m] : show empty projects bjoern@108: [--lastweek|-c] : entries of last working week''' teichmann@8: bjoern@51: bjoern@359: def usage(exit_code=0): teichmann@8: print USAGE % sys.argv[0] teichmann@8: sys.exit(exit_code) teichmann@8: bjoern@359: teichmann@8: def main(): teichmann@8: bjoern@359: database = DEFAULT_DATABASE bjoern@359: user = None teichmann@8: list_projects = False bjoern@359: project = None bjoern@359: encoding = None bjoern@359: week = None bjoern@359: year = None bjoern@359: empty_proj = False bjoern@359: database = None bjoern@359: template = ZEITERFASSUNG_TEMPLATE bjoern@122: teichmann@8: opts, args = getopt.getopt( teichmann@8: sys.argv[1:], bjoern@108: 'd:u:p:e:hl:w:y:mc', bjoern@108: ['database=', 'user=', 'project=', 'encoding=', 'help', 'list', 'week=', bjoern@108: 'year=', 'empty', 'lastweek']) teichmann@8: teichmann@8: for opt, val in opts: teichmann@8: if opt in ("--database", "-d"): teichmann@8: database = val teichmann@8: elif opt in ("--user", "-u"): teichmann@8: user = val teichmann@8: elif opt in ("--project", "-p"): teichmann@8: project = val teichmann@10: elif opt in ("--encoding", "-e"): teichmann@10: encoding = val teichmann@8: elif opt in ("--help", "-h"): teichmann@8: usage() teichmann@8: elif opt in ("--list", "-l"): teichmann@8: list_projects = True bricks@46: elif opt in ("--year", "-y"): bricks@46: year = val bjoern@192: elif opt in ("--week", "-w"): bjoern@192: week = int(val) bjoern@192: elif opt in ("--lastweek", "-c") and not week: bjoern@359: week = (datetime.now() - timedelta(7)).isocalendar()[1] bjoern@60: elif opt in ("--empty", "-m"): bjoern@60: empty_proj = True teichmann@8: bjoern@127: if not encoding: bjoern@127: encoding = locale.getdefaultlocale()[1] bjoern@359: bjoern@127: Writer = codecs.getwriter(encoding) bjoern@127: sys.stdout = Writer(sys.stdout) teichmann@10: bjoern@359: print render(user=user, database=database, week=week, year=year, bjoern@360: template=template, project=project, empty_projects=empty_proj) teichmann@8: teichmann@8: teichmann@8: if __name__ == '__main__': teichmann@8: main() teichmann@8: teichmann@8: # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: