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.py70
1 files changed, 46 insertions, 24 deletions
diff --git a/kpov_judge/test_task.py b/kpov_judge/test_task.py
index d3dcec7..28fc513 100755
--- a/kpov_judge/test_task.py
+++ b/kpov_judge/test_task.py
@@ -91,14 +91,9 @@ def locate_task(params, argparser, quiet=False):
params['task_url'] = args.task_url
if not quiet:
params = get_params(params, url_meta)
- # then the student's ID (and password if neccessarry)
- fetch_params_meta = collections.OrderedDict({
- 'username': {'descriptions': {'si': 'Uporabniško ime', 'en': 'Username'}},
- })
- if params.get('task_url', '').startswith('http'):
- fetch_params_meta['password'] = {'descriptions': {'si': 'Geslo', 'en': 'Password'}, 'masked': True}
+
# and finally, the name of the task
- fetch_params_meta['task_name'] = {'descriptions': {'si': 'Ime naloge', 'en': 'Task name'}, }
+ fetch_params_meta = collections.OrderedDict({'task_name': {'descriptions': {'si': 'Ime naloge', 'en': 'Task name'}}})
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
@@ -106,6 +101,7 @@ 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 load_params(filename):
@@ -113,7 +109,8 @@ def load_params(filename):
return yaml.load(open(filename))
except:
return {}
-
+
+
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
@@ -124,7 +121,7 @@ if __name__ == '__main__':
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,
+ 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()
@@ -143,34 +140,59 @@ if __name__ == '__main__':
try:
task_url = params['task_url']
task_name = params['task_name']
- if task_url.startswith('http'):
- http_auth(task_url, params['username'], params['password'])
- print("Fetching {task_url}/{task_name}/task.py…".format(**params))
+
source = urllib.request.urlopen("{task_url}/{task_name}/task.py".format(**params))
task, task_check, task_params_meta, gen_params = load_task(source)
except Exception as e:
- print(str(e))
- print()
- for k, v in params.items():
- if k not in ('password', 'task_params'):
- print('{}: {}'.format(k, v))
+ import traceback
+ traceback.print_exc()
with open(basic_args.params_file, 'w') as f:
yaml.dump(params, f)
exit(1)
- # get task parameters
+ # get stored task parameters
params['task_params'] = params.get('task_params', {})
- params['task_params'][params['task_name']] = params['task_params'].get(params['task_name'], {})
- task_params = params['task_params'][params['task_name']]
+ task_params = params['task_params'].setdefault(task_name, {})
+
+ # ensure we have a submission token
+ if task_url.startswith('http'):
+ # check if existing token is valid
+ if task_params.get('token'):
+ response = urllib.request.urlopen(
+ '{task_url}/{task_name}/params-token.json'.format(**params),
+ data=urllib.parse.urlencode({'params': json.dumps(task_params)}).encode())
+ response = json.load(io.TextIOWrapper(response))
+ if response:
+ # got a good token
+ task_params.update(response)
+ else:
+ # did not get a token, try again with password
+ del task_params['token']
+
+ # authenticate to get a new token
+ if not task_params.get('token'):
+ # get the student's ID and password
+ # TODO clunky, should refactor all argument-getting stuff
+ fetch_params_meta = {'username': {'descriptions': {'si': 'Uporabniško ime', 'en': 'Username'}}}
+ params = get_params(params, fetch_params_meta, params['language'])
+ fetch_pass_meta = {'password': {'descriptions': {'si': 'Geslo', 'en': 'Password'}, 'masked': True}}
+ params_pass = get_params({}, fetch_pass_meta, params['language'])
+
+ try:
+ http_auth(task_url, params['username'], params_pass['password'])
+ response = urllib.request.urlopen('{task_url}/{task_name}/params.json'.format(**params))
+ response = json.load(io.TextIOWrapper(response))
+ if response:
+ task_params.update(response)
+ except Exception as ex:
+ print(ex)
+ exit(2)
+
if basic_args.generate_params:
#prejema lahko samo stringe in ne številk (potrebno je str(int)
# print ("params before: {} {}".format(params, task_params))
task_params.update(gen_params(params['username'], task_params_meta))
# print ("params after: {} {}".format(params, task_params))
- if task_url.startswith('http'):
- response = urllib.request.urlopen('{task_url}/{task_name}/params.json'.format(**params))
- web_task_params = json.load(io.TextIOWrapper(response))
- task_params.update(web_task_params)
task_argparser = argparse.ArgumentParser(parents=[argparser], conflict_handler='resolve', add_help=True)
add_meta_to_argparser(task_argparser, task_params_meta, defaults=task_params)