summaryrefslogtreecommitdiff
path: root/kpov_judge/test_task.py
blob: 4658304d84554f2b9e425a723722d467432ed03a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import yaml
import json
import urllib
import urllib2
from dialog import Dialog
import os
import inspect
import sys
from bson.son import SON
import kpov_random_helpers
import random
import argparse

# 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_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, meta, param_name_list=None, dialog=None, language = None):
    if param_name_list is None:
        param_name_list = meta.keys()
    if language is None:
        language = params.get('language', DEFAULT_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 = ''
            else:
                init = params[name]
            ret, s = dialog.inputbox(description, init=init)
            if ret == 0:
                if m.get('w', True):
                    params[name] = s
                elif name not in params:
                    params[name] = None
            got_param = (ret == 0) and name in params
    return params

def add_meta_to_argparser(argparser, meta, defaults = {}):
    language = defaults.get('language', DEFAULT_LANGUAGE)
    for k, v in meta.iteritems():
        try:
            desc = v['descriptions'][language].encode("utf-8")
        except:
            desc = k
        print desc, type(desc)
        argparser.add_argument('--'+k, nargs=1, help=desc, type=unicode, default=defaults.get(k, None))
    return argparser

def http_auth(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, 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, gen_params

def locate_task(params, argparser, dialog):
    # first the URL where all tasks are stored
    url_meta = {
        'task_url': {'descriptions': {'si': 'URL z nalogami', 'en': 'Root URL for all tasks'}, }
    }
    if 'task_url' not in params:
        params['task_url'] = TASK_URL
    argparser = add_meta_to_argparser(argparser, meta = url_meta, defaults = params)
    args = argparser.parse_args()
    params['task_url'] = args.task_url
    if dialog is not None:
        params = get_params_dialog(params, url_meta, dialog = dialog)
    # then the student's ID (and password if neccessarry)
    fetch_params_meta = {
            'username':{'descriptions': {'si': 'Uporabniško ime', 'en': 'Username'}, },
    }
    if params['task_url'] is not None and params['task_url'].startswith('http'):
        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)
    args = argparser.parse_args()
    if dialog is not None:
        params = get_params_dialog(params, fetch_params_meta, dialog=dialog)
    return argparser, params

def load_params(filename):
    try:
        with open(filename) as f:
            saved_params = yaml.load(f)
    except:
        saved_params = dict()
    if saved_params is None:
        saved_params = dict()
    return saved_params
 
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 = basic_argparser.parse_args()
    # get default parameters including language
    params = load_params(basic_args.params_file)
    basic_argparser.add_argument('-l', '--language', nargs=1,
        help='the language used', default=params.get('language', DEFAULT_LANGUAGE))
    basic_args = basic_argparser.parse_args()
    params['language'] = basic_args.language
    if not basic_args.quiet:
        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)
    # 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:
        task_url = params['task_url']
        task_name = params['task_name']
        if task_url.startswith('http'):
            http_auth(task_url, params['username'], params['password'])
        req = urllib2.Request("{task_url}/{task_name}/task.py".format(**params))
        source = urllib2.urlopen(req)
        task, task_check, task_params_meta, gen_params = load_task(source)
    except Exception, e:
        print e
        if basic_args.help:
            argparser.print_help()
        else:
            argparser.print_usage()
        with open(basic_args.params_file, 'w') as f:
            yaml.dump(params, f)
        exit(1)
    # get task parameters
    params['task_params'] = params.get('task_params', dict())
    params['task_params'][params['task_name']] = params['task_params'].get(params['task_name'], dict())
    task_params = params['task_params'][params['task_name']]
    if basic_args.generate_params:
	#prejema lahko samo stringe in ne številk (potrebno je str(int)
        print "params before", params, task_params
        task_params.update(gen_params(params['username'], task_params_meta))
        print "params after", params, task_params
    if task_url.startswith('http'):
        req = urllib2.Request("{task_url}/{task_name}/params.json".format(**params))
        web_task_params = json.load(urllib2.urlopen(req))
        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)
    args = vars(params_argparser.parse_args())
    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:
        print task_params
        task_params = get_params_dialog(task_params, task_params_meta, dialog=dialog, language = params['language'])
    if basic_args.help:
        # params_argparser.print_help()
        print params_argparser.format_help()
        exit(0)
    # run task.task()
    public_params = dict()
    for k in inspect.getargs(task.func_code)[0]:
        public_params[k] = task_params[k]
    params['task_params'][params['task_name']] = task_params
    try:
        task_result = task(**public_params)
    # run task.task_check()
    except Exception, e:
        print("Error running task(...):")
        print(e)
    try:
        result = task_check(task_result, task_params)
    except Exception, e:
        print("Error running task_check(results, params):")
        print(e)
    # print results
    # save parameters for the next run
    with open(basic_args.params_file, 'w') as f:
        yaml.dump(params, f)