From 7dd74ded5bfcd36b6a498c0dff71235c4bc764ca Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Thu, 21 Feb 2019 01:52:43 +0100 Subject: test_task: improve presentation Print all non-writable task parameters first and only query for the writable ones. Add headers to and clean up the output. --- kpov_judge/test_task.py | 55 ++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/kpov_judge/test_task.py b/kpov_judge/test_task.py index aa9a268..43aaf38 100755 --- a/kpov_judge/test_task.py +++ b/kpov_judge/test_task.py @@ -24,8 +24,11 @@ TASK_URL = "file://" + os.getcwd() + '/tasks' PARAMS_FILE = os.path.expanduser("~/.kpov_params.yaml") DEFAULT_LANGUAGE = 'si' +def print_header(title, spacing=1): + print('\n'*spacing + '> {}'.format(title)) + # get the parameters for a task either from the user or from a file -def get_params(params, meta, language=None): +def get_params(params, params_meta, language=None): # prefill input() prompt with given text def rlinput(prompt, prefill=''): readline.set_startup_hook(lambda: readline.insert_text(prefill)) @@ -34,24 +37,26 @@ def get_params(params, meta, language=None): finally: readline.set_startup_hook() - param_name_list = list(meta.keys()) if language is None: language = params.get('language', DEFAULT_LANGUAGE) - for name in param_name_list: - m = meta.get(name, {}) - description = m.get('descriptions', {}).get(language, name) - try: - if m.get('masked', False): - s = getpass.getpass('{}: '.format(description)) - else: - s = rlinput('{}: '.format(description), params.get(name, '')) - if s and m.get('w', True): - params[name] = s - elif name not in params: - params[name] = None - except EOFError: - print() + # print all non-writable parameters first, then prompt for writable ones + for name, meta in sorted(params_meta.items(), key=lambda n: n[1].get('w', True)): + description = meta.get('descriptions', {}).get(language, name) + if name not in params: + params[name] = None + if meta.get('w', True): + try: + if meta.get('masked', False): + s = getpass.getpass('{}: '.format(description)) + else: + s = rlinput('{}: '.format(description), params.get(name, '')) + if s: + params[name] = s + except EOFError: + print() + else: + print('{}: {}'.format(name, params.get(name, ''))) return params def add_meta_to_argparser(argparser, meta, defaults={}): @@ -81,6 +86,7 @@ def locate_task(params, argparser, quiet=False): args, unknown_args = argparser.parse_known_args() params['task_url'] = args.task_url if not quiet: + print_header('Task', spacing=0) params = get_params(params, url_meta) # and finally, the name of the task @@ -92,7 +98,6 @@ def locate_task(params, argparser, quiet=False): params[k] = vars(args).get(k, params.get(k, None)) if not quiet: params = get_params(params, fetch_params_meta) - print() return params def http_auth(url, username, password): @@ -185,6 +190,7 @@ if __name__ == '__main__': ('username', {'descriptions': {'si': 'Uporabniško ime', 'en': 'Username'}}), ('password', {'descriptions': {'si': 'Geslo', 'en': 'Password'}, 'masked': True}), )) + print_header('KPOV login') user_params = get_params(params, user_meta, params['language']) http_auth(task_url, user_params['username'], user_params['password']) @@ -209,6 +215,7 @@ if __name__ == '__main__': if args.get(k): task_params[k] = args[k] if not basic_args.quiet: + print_header('Task parameters') task_params = get_params(task_params, task_params_meta, language=params['language']) public_params = {} @@ -221,19 +228,19 @@ if __name__ == '__main__': yaml.dump(params, f) try: - print() - print('Running task… ') + print_header('Results', spacing=0 if basic_args.quiet else 1) + print('Running task... ', end='', flush=True) task_result = task(**public_params) - print('Checking task… ') + print('checking task... ', end='', flush=True) task_params['token'] = tokens[task_name] # hack to avoid changing task_check signature score, hints = task_check(task_result, task_params) del task_params['token'] - print('Done!') - print() + print('done!') print('Score: {}'.format(score)) - print('Hints:') + + print_header('Hints') for hint in hints: - print(hint) + print(hint.strip()) except Exception as e: import traceback traceback.print_exc() -- cgit v1.2.1