summaryrefslogtreecommitdiff
path: root/kpov_judge/test_task.py
diff options
context:
space:
mode:
Diffstat (limited to 'kpov_judge/test_task.py')
-rwxr-xr-xkpov_judge/test_task.py67
1 files changed, 29 insertions, 38 deletions
diff --git a/kpov_judge/test_task.py b/kpov_judge/test_task.py
index 3fad962..f4f7676 100755
--- a/kpov_judge/test_task.py
+++ b/kpov_judge/test_task.py
@@ -1,14 +1,11 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+import getpass
import io
import yaml
import json
import urllib.request, urllib.parse, urllib.error
-try:
- from dialog import Dialog
-except:
- Dialog = None
import os
import inspect
import sys
@@ -17,14 +14,15 @@ import kpov_random_helpers
import random
import argparse
import locale
+import readline
locale.setlocale(locale.LC_ALL, ['C', 'utf8'])
+readline.set_completer_delims(readline.get_completer_delims().replace('/', ''))
+readline.parse_and_bind('tab: complete')
# SERVER_URL = "http://localhost/kpov_judge/"
#SERVER_URL = "http://kpov.fri.uni-lj.si/kpov_judge/"
#TASK_URL = SERVER_URL + '{task_id}/task.py'
-#PARAMS_URL = SERVER_URL + '{task_id}/params.json'
-#SUBMIT_URL = SERVER_URL + '{task_id}/results.json'
TASK_URL = "file://" + os.getcwd() + '/tasks'
PARAMS_FILE = os.path.expanduser("~/.kpov_params.yaml")
@@ -32,7 +30,7 @@ DEFAULT_LANGUAGE = 'si'
"""get the parameters for a task either from the user
or from a file."""
-def get_params_dialog(params, meta, param_name_list=None, dialog=None, language = None):
+def get_params(params, meta, param_name_list=None, language=None):
if param_name_list is None:
param_name_list = list(meta.keys())
if language is None:
@@ -40,25 +38,23 @@ def get_params_dialog(params, meta, param_name_list=None, dialog=None, language
for name in param_name_list:
if name not in meta:
pass
- got_param = False
- while not got_param:
- m = meta.get(name, {})
- try:
- description = m['descriptions'][language]
- except KeyError:
- description = name
- if params.get(name, None) is None:
- init = ''
+ m = meta.get(name, {})
+ try:
+ description = m['descriptions'][language]
+ except KeyError:
+ description = name
+ init = params.get(name) or ''
+ try:
+ if name == 'password':
+ s = getpass.getpass('{}: '.format(description))
else:
- init = params[name]
- ret, s = dialog.inputbox(description, init=init)
- if ret == 'ok':
- if m.get('w', True):
- params[name] = s
- elif name not in params:
- params[name] = None
- got_param = (ret == 'ok') and name in params
- dialog.clear()
+ s = input('{} [{}]: '.format(description, init))
+ if s and m.get('w', True):
+ params[name] = s
+ elif name not in params:
+ params[name] = None
+ except EOFError:
+ print()
return params
def add_meta_to_argparser(argparser, meta, defaults = {}):
@@ -90,7 +86,7 @@ def load_task(stream):
# args_list = inspect.getargs(task.func_code)[0]
return d['task'], d['task_check'], d['params_meta'], d['gen_params']
-def locate_task(params, argparser, dialog):
+def locate_task(params, argparser, quiet=False):
# first the URL where all tasks are stored
url_meta = {
'task_url': {'descriptions': {'si': 'URL z nalogami', 'en': 'Root URL for all tasks'}}
@@ -100,8 +96,8 @@ def locate_task(params, argparser, dialog):
argparser = add_meta_to_argparser(argparser, meta = url_meta, defaults = params)
args, unknown_args = argparser.parse_known_args()
params['task_url'] = args.task_url
- if dialog is not None:
- params = get_params_dialog(params, url_meta, dialog = dialog)
+ if not quiet:
+ params = get_params(params, url_meta)
# then the student's ID (and password if neccessarry)
fetch_params_meta = {
'username': {'descriptions': {'si': 'Uporabniško ime', 'en': 'Username'}},
@@ -116,8 +112,8 @@ def locate_task(params, argparser, dialog):
# 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 dialog is not None:
- params = get_params_dialog(params, fetch_params_meta, dialog=dialog)
+ if not quiet:
+ params = get_params(params, fetch_params_meta)
return argparser, params
def load_params(filename):
@@ -147,14 +143,9 @@ if __name__ == '__main__':
help='the language used', default=params.get('language', DEFAULT_LANGUAGE))
basic_args, unknown_args = basic_argparser.parse_known_args()
params['language'] = basic_args.language
- if not basic_args.help and (
- not basic_args.quiet and Dialog is not None):
- dialog = Dialog(dialog="dialog")
- else:
- dialog = None
# continue with the parameters needed to get the task
- argparser, params = locate_task(params, basic_argparser, dialog=dialog)
+ argparser, params = locate_task(params, basic_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:
@@ -201,8 +192,8 @@ if __name__ == '__main__':
for k in task_params_meta:
if k in args and args[k] is not None:
task_params[k] = args[k]
- if dialog is not None:
- task_params = get_params_dialog(task_params, task_params_meta, dialog=dialog, language = params['language'])
+ 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())