changeset 463:24ccca2442b1

optparse to argparse Migration for scripts/getan-eval.py.
author Magnus Schieder <mschieder@intevation.de>
date Mon, 30 Apr 2018 12:13:35 +0200
parents 52398469cacc
children 2ab6fdde1010
files TODO scripts/getan-eval.py
diffstat 2 files changed, 32 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/TODO	Wed Mar 21 10:04:31 2018 +0100
+++ b/TODO	Mon Apr 30 12:13:35 2018 +0200
@@ -67,15 +67,9 @@
     This is a larger drawback after moving all entries somewhere,
     it is unclear which task's entries are shown now.
 
-20180104 BER (minor) Display licensing information with --version and
-  usage. Implementation idea: change this when moving away from optparse.
-
 20170709 BER: Give scripts/getan-eval.py a more specific name and add
   it as a script to be installed.
 
-20170529 BER: Python compatibility: switch from deprecated optparse module
-  to argparse. Affects getan/main.py and scripts/getan-eval.py.
-
 20170504 BER: Some multi-user installations do not want a logfile by default.
   We could solve this requirement by making it configurable.
 
--- a/scripts/getan-eval.py	Wed Mar 21 10:04:31 2018 +0100
+++ b/scripts/getan-eval.py	Mon Apr 30 12:13:35 2018 +0200
@@ -11,56 +11,58 @@
 import sys
 
 from datetime import date, datetime, timedelta
-from optparse import OptionParser
+import argparse
 
 from getan.template import render
 
 
 def main():
-    parser = OptionParser()
-    parser.add_option("-d", "--database", dest="database",
-                      help="getan database",  metavar="DATABASE")
-    parser.add_option("-t", "--template", dest="template", metavar="TEMPLATE",
-                      help="name of getan template")
-    parser.add_option("-u", "--user", dest="user", help="name of user")
-    parser.add_option("-p", "--project", dest="project",
-                      help="key of output project")
-    parser.add_option("-w", "--week", type="int", dest="week",
-                      help="week of year")
-    parser.add_option("-y", "--year", type="int", dest="year", help="year")
-    parser.add_option("-c", "--lastweek", dest="lastweek",
-                      help="entries of last working week",
-                      action="store_true")
-    parser.add_option("-m", "--empty", dest="empty",
-                      help="show projects without an entries",
-                      action="store_true")
-    parser.add_option("--encoding", dest="encoding",
-                      help="encoding of output", metavar="ENCODING")
+    usage='getan-eval.py [options]'
+    parser = argparse.ArgumentParser(prog='getan', usage=usage)
 
-    (options, args) = parser.parse_args()
+    parser.add_argument('-d', '--database', dest='database',metavar="DATABASE",
+                        help='metavar="DATABASE')
+    parser.add_argument('-t', '--template', dest='template',
+                        metavar='TEMPLATE', help='name of getan template')
+    parser.add_argument('-u', '--user', dest='user', help='name of user')
+    parser.add_argument('-p', '--project', dest='project',
+                        help='key of output project')
+    parser.add_argument('-w', '--week', type=int, dest='week',
+                        help='week of year')
+    parser.add_argument('-y', '--year', type=int, dest='year', help='year')
+    parser.add_argument('-c', '--lastweek', dest='lastweek',
+                        help='entries of last working week',
+                        action='store_true')
+    parser.add_argument('-m', '--empty', dest='empty',
+                        help='show projects without an entries',
+                        action="store_true")
+    parser.add_argument('--encoding', dest='encoding',
+                        help='encoding of output', metavar='ENCODING')
 
-    if options.lastweek:
+    args = parser.parse_args()
+
+    if args.lastweek:
         week = (datetime.now() - timedelta(7)).isocalendar()[1]
         year = int(date.today().strftime("%Y"))
     else:
-        year = options.year
-        week = options.week
+        year = args.year
+        week = args.week
 
-    template_name = options.template or "wochenbericht"
+    template_name = args.template or "wochenbericht"
 
-    if not options.encoding:
+    if not args.encoding:
         encoding = locale.getdefaultlocale()[1] or "utf-8"
 
     Writer = codecs.getwriter(encoding)
     sys.stdout = Writer(sys.stdout)
 
     user = None
-    if options.user:
-        user = options.user.decode(encoding)
+    if args.user:
+        user = args.user.decode(encoding)
 
-    print render(database=options.database, user=user,
+    print render(database=args.database, user=user,
                  template=template_name, year=year, week=week,
-                 project=options.project, empty_projects=options.empty)
+                 project=args.project, empty_projects=args.empty)
 
 
 if __name__ == '__main__':
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)