From 071b8c1abe145f8111d22b57651a85e4991bfd95 Mon Sep 17 00:00:00 2001
From: Timotej Lazar <timotej.lazar@fri.uni-lj.si>
Date: Tue, 2 Oct 2018 13:25:24 +0200
Subject: test_task.py: simplify argparser code

---
 kpov_judge/test_task.py | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)

(limited to 'kpov_judge')

diff --git a/kpov_judge/test_task.py b/kpov_judge/test_task.py
index f4f7676..206e809 100755
--- a/kpov_judge/test_task.py
+++ b/kpov_judge/test_task.py
@@ -65,7 +65,6 @@ def add_meta_to_argparser(argparser, meta, defaults = {}):
         except:
             desc = k
         argparser.add_argument('--'+k, nargs='?', help=desc, type=str, default=defaults.get(k, None))
-    return argparser
 
 def http_auth(url, username, password):
     password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
@@ -93,7 +92,7 @@ def locate_task(params, argparser, quiet=False):
     }
     if 'task_url' not in params:
         params['task_url'] = TASK_URL
-    argparser = add_meta_to_argparser(argparser, meta = url_meta, defaults = params)
+    add_meta_to_argparser(argparser, meta = url_meta, defaults = params)
     args, unknown_args = argparser.parse_known_args()
     params['task_url'] = args.task_url
     if not quiet:
@@ -107,14 +106,14 @@ def locate_task(params, argparser, quiet=False):
         fetch_params_meta['password'] = {'descriptions': {'si': 'Geslo', 'en': 'Password'}, }
     # and finally, the name of the task
     fetch_params_meta['task_name'] = {'descriptions': {'si': 'Ime naloge', 'en': 'Task name'}, }
-    argparser = add_meta_to_argparser(argparser, meta = fetch_params_meta, defaults = params)
+    add_meta_to_argparser(argparser, meta = fetch_params_meta, defaults = params)
     args, unknown_args = argparser.parse_known_args()
     # update params with the now known args
     for k, v in fetch_params_meta.items():
         params[k] = vars(args).get(k, params.get(k, None))
     if not quiet:
         params = get_params(params, fetch_params_meta)
-    return argparser, params
+    return params
 
 def load_params(filename):
     try:
@@ -129,23 +128,27 @@ def load_params(filename):
 if __name__ == '__main__':
     # get the parameters needed to get to the task, such as the URLs, the name of the task and optionally an ID from the student
     # start with the the parameters needed for the dialog gui to work
-    basic_argparser = argparse.ArgumentParser(description='Get the task parameters', conflict_handler='resolve', add_help=False)
-    basic_argparser.add_argument('-h', '--help', action='store_true')
-    basic_argparser.add_argument('-q', '--quiet', action='store_true', help='disable dialog', default=False)
-    basic_argparser.add_argument('-g', '--generate_params', action='store_true', help='Generate initial values the task parameters', default=False)
-    basic_argparser.add_argument('-pf','--params_file', nargs='?',
-        help='a local file containing saved param values', default=PARAMS_FILE)
-    basic_args, unknown_args = basic_argparser.parse_known_args()
+    argparser = argparse.ArgumentParser(conflict_handler='resolve', add_help=False,
+        description='Get task parameters')
+    argparser.add_argument('-h', '--help', action='store_true')
+    argparser.add_argument('-q', '--quiet', action='store_true',
+        help='disable prompts')
+    argparser.add_argument('-g', '--generate_params', action='store_true',
+        help='generate initial values for the task parameters')
+    argparser.add_argument('-pf','--params_file', nargs='?', default=PARAMS_FILE,
+        help='a local file containing saved param values')
+    basic_args, unknown_args = argparser.parse_known_args()
 
     # get default parameters including language
     params = load_params(basic_args.params_file)
-    basic_argparser.add_argument('-l', '--language', nargs='?',
-        help='the language used', default=params.get('language', DEFAULT_LANGUAGE))
-    basic_args, unknown_args = basic_argparser.parse_known_args()
+    argparser.add_argument('-l', '--language', nargs='?',
+        default=params.get('language', DEFAULT_LANGUAGE),
+        help='the language used')
+    basic_args, unknown_args = argparser.parse_known_args()
     params['language'] = basic_args.language
 
     # continue with the parameters needed to get the task
-    argparser, params = locate_task(params, basic_argparser, quiet=basic_args.quiet)
+    params = locate_task(params, argparser, quiet=basic_args.quiet)
     # TODO: if the task name is missing or invalid, try to get a list of tasks 
     # get task source and generate params if neccessarry
     try:
@@ -187,7 +190,7 @@ if __name__ == '__main__':
         web_task_params = json.load(io.TextIOWrapper(response))
         task_params.update(web_task_params)
     params_argparser = argparse.ArgumentParser(parents=[argparser], conflict_handler='resolve', add_help=True)
-    params_argparser = add_meta_to_argparser(params_argparser, task_params_meta, defaults=task_params)
+    add_meta_to_argparser(params_argparser, task_params_meta, defaults=task_params)
     args = vars(params_argparser.parse_args())
     for k in task_params_meta:
         if k in args and args[k] is not None:
@@ -195,7 +198,6 @@ if __name__ == '__main__':
     if not basic_args.quiet:
         task_params = get_params(task_params, task_params_meta, language=params['language'])
     if basic_args.help:
-        # params_argparser.print_help()
         print(params_argparser.format_help())
         exit(0)
 
-- 
cgit v1.2.1