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
|
# coding=utf-8
import threading
from db.models import Problem
import server
import server.user_session
__all__ = ['RobotSession']
class RobotSession(server.LanguageSession):
"""Abstracts a Robot session.
Only public methods are available to the outside world due to the use of multiprocessing managers.
Therefore prefix any private methods with an underscore (_).
No properties are accessible; use getters and setters instead.
Values are passed by value instead of by reference (deep copy!).
"""
def __init__(self, output_cb=None):
self._access_lock = threading.Lock()
self._sent_hints = []
def destroy(self):
pass
def __del__(self):
pass
def hint(self, sid, problem_id, program):
p = Problem.get(id=problem_id)
language_module = server.problems.load_language(p.language, 'common')
problem_module = server.problems.load_problem(p.language, p.group, p.identifier, 'common')
hints = []
if hasattr(language_module, 'hint'):
hints = language_module.hint(program)
if not hints and hasattr(problem_module, 'hint'):
hints = problem_module.hint(program)
if not hints:
hints = [{'id': 'no_hint'}]
self._instantiate_and_save_hints(language_module, problem_module, hints)
return hints
def test(self, sid, problem_id, program):
p = Problem.get(id=problem_id)
language_module = server.problems.load_language(p.language, 'common')
problem_module = server.problems.load_problem(p.language, p.group, p.identifier, 'common')
try:
passed, hints = problem_module.test(program)
if passed:
session = server.user_session.get_session_by_id(sid)
session.update_solution(problem_id, done=True)
except AttributeError as ex:
hints = [{'id': 'system_error', 'args': {'message': 'test function does not exist'}}]
self._instantiate_and_save_hints(language_module, problem_module, hints)
return hints
# Add hint parameters (such as message index) based on hint class. Append
# the finalized hints to the list of sent hints.
def _instantiate_and_save_hints(self, language_mod, problem_mod, hints):
with self._access_lock:
for hint in hints:
for mod in [language_mod, problem_mod]:
if hasattr(mod, 'hint_type') and hint['id'] in mod.hint_type:
hint_type = mod.hint_type[hint['id']]
hint_type.instantiate(hint, self._sent_hints)
self._sent_hints.extend(hints)
server.language_session_handlers['robot'] = lambda user_session, problem_id, language_identifier, group_identifier, problem_identifier: RobotSession()
|