bjoern@283: #!/usr/bin/env python
bjoern@283: # -*- coding: utf-8 -*-
bjoern@283: #
bjoern@358: # (c) 2013, 2014 by Björn Ricks <bjoern.ricks@intevation.de>
bjoern@283: #
bjoern@283: # This is Free Software licensed under the terms of GPLv3 or later.
bjoern@283: # For details see LICENSE coming with the source of 'getan'.
bjoern@283: 
bjoern@373: import codecs
bjoern@373: import locale
bjoern@373: import sys
bjoern@373: 
bjoern@283: from datetime import date, datetime, timedelta
bjoern@283: from optparse import OptionParser
bjoern@283: 
bjoern@358: from getan.template import render
bjoern@283: 
bjoern@347: 
bjoern@283: def main():
bjoern@283:     parser = OptionParser()
bjoern@283:     parser.add_option("-d", "--database", dest="database",
bjoern@347:                       help="getan database",  metavar="DATABASE")
bjoern@283:     parser.add_option("-t", "--template", dest="template", metavar="TEMPLATE",
bjoern@347:                       help="name of getan template")
bernhard@430:     parser.add_option("-u", "--user", dest="user", help="name of user")
bjoern@283:     parser.add_option("-p", "--project", dest="project",
bjoern@347:                       help="key of output project")
bjoern@283:     parser.add_option("-w", "--week", type="int", dest="week",
bjoern@347:                       help="week of year")
bjoern@283:     parser.add_option("-y", "--year", type="int", dest="year", help="year")
bjoern@358:     parser.add_option("-c", "--lastweek", dest="lastweek",
bjoern@358:                       help="entries of last working week",
bjoern@347:                       action="store_true")
bjoern@360:     parser.add_option("-m", "--empty", dest="empty",
bjoern@360:                       help="show projects without an entries",
bjoern@360:                       action="store_true")
bjoern@373:     parser.add_option("--encoding", dest="encoding",
bjoern@373:                       help="encoding of output", metavar="ENCODING")
bjoern@283: 
bjoern@283:     (options, args) = parser.parse_args()
bjoern@283: 
bjoern@358:     if options.lastweek:
bjoern@358:         week = (datetime.now() - timedelta(7)).isocalendar()[1]
bjoern@358:         year = int(date.today().strftime("%Y"))
bjoern@283:     else:
bjoern@358:         year = options.year
bjoern@358:         week = options.week
bjoern@283: 
bjoern@283:     template_name = options.template or "wochenbericht"
bjoern@283: 
bjoern@373:     if not options.encoding:
bernhard@432:         encoding = locale.getdefaultlocale()[1] or "utf-8"
bjoern@373: 
bjoern@373:     Writer = codecs.getwriter(encoding)
bjoern@373:     sys.stdout = Writer(sys.stdout)
bjoern@373: 
bernhard@431:     user = None
bjoern@390:     if options.user:
bjoern@390:         user = options.user.decode(encoding)
bjoern@390: 
bjoern@390:     print render(database=options.database, user=user,
bjoern@358:                  template=template_name, year=year, week=week,
bjoern@360:                  project=options.project, empty_projects=options.empty)
bjoern@283: 
bjoern@283: 
bjoern@283: if __name__ == '__main__':
bjoern@283:     main()
bjoern@283: 
bjoern@283: # vim:set ts=4 sw=4 si et sta sts=4 :