#!/usr/bin/env python # -*- coding: utf-8 -*- import yaml import json import urllib import urllib2 import dialog import os import inspect import sys from bson.son import SON import kpov_random_helpers # 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://./" PARAMS_URL = None PARAMS_FILE = os.path.expanduser("~/.kpov_params.yaml") DEFAULT_LANGUAGE = 'si' """get the parameters for a task either from the user or from a file.""" def get_params_dialog(params, param_name_list, meta): lang = params['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'][lang] except KeyError: description = '' ret, s = dialog.inputbox(m.get('descriptions', name), init=params.get(name, '')) if ret == 0 and m.get('w', False): params[name] = s got_param = (ret == 0) and name in params return params def get_params_web(params, param_name_list, meta, url=PARAMS_URL): req = urllib2.Request(url.format(**saved_params)) response = urllib2.urlopen(req) params = json.load(response) return params def get_params_args(params, param_names_list, meta): pass def auth_open(url, username, password): password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() # Add the username and password. # If we knew the realm, we could use it instead of None. password_mgr.add_password(None, SERVER_URL, username, password) handler = urllib2.HTTPBasicAuthHandler(password_mgr) # create "opener" (OpenerDirector instance) opener = urllib2.build_opener(handler) # use the opener to fetch a URL # opener.open(SERVER_URL) # Install the opener. # Now all calls to urllib2.urlopen use our opener. urllib2.install_opener(opener) def load_task(stream): # the stream should compile into at least the function # task(...) and a dictionary "params_meta". source = stream.read() t = compile(source, 'task.py', 'exec') exec(t) # get a list of arguments for task(...) # args_list = inspect.getargs(task.func_code)[0] return task, task_check, params_meta if __name__ == '__main__': # get default parameters try: with open(PARAMS_FILE) as f: saved_params = yaml.load(f) except: saved_params = dict() # 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 import argparse task_argparser = argparse.ArgumentParser(description='Get the task parameters', conflict_handler='resolve', add_help=False) task_argparser.add_argument('-h', '--help', action='store_true') task_argparser.add_argument('-q', '--quiet', action='store_true', help='disable dialog') task_argparser.add_argument('-pf','--params_file', nargs='?', help='a local file containing saved param values', default=PARAMS_FILE) basic_args = task_argparser.parse_args() print basic_args task_argparser.add_argument('-pu', '--params_url', nargs=1, help='the URL for task parameters', default=PARAMS_URL) task_argparser.add_argument('-tu', '--task_url', nargs='?', help='the root URL for all tasks', default=TASK_URL) task_argparser.add_argument('-l', '--language', nargs=1, help='the language used', default=DEFAULT_LANGUAGE) task_argparser.add_argument('task_name', help='task name') args = task_argparser.parse_args() print args lang = args.language print args.task_dir, args.task_name task_fname = os.path.join(args.task_dir, args.task_name, 'task.py') # get task source try: with open(task_fname) as source: task, task_check, params_meta = load_task(source) except: if args.help: task_argparser.print_help() else: task_argparser.print_usage() exit(1) # get task parameters params_argparser = argparse.ArgumentParser(parents=[task_argparser], conflict_handler='resolve', add_help=True) for pm_name, pm in params_meta.iteritems(): params_argparser.add_argument('--' + pm_name, nargs = 1, help=pm['descriptions'][lang]) if args.help: params_argparser.print_help() args = params_argparser.parse_args() # run task.task() s = task(**public_params) # run task.task_check() result = task_check(s, params) # print results