summaryrefslogtreecommitdiff
path: root/server/robot_session.py
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@araneo.org>2015-09-30 20:59:52 +0200
committerTimotej Lazar <timotej.lazar@araneo.org>2015-09-30 20:59:52 +0200
commit3aff9ac1cd68f3dbdba1bd8371ed6ae7614dbd64 (patch)
tree1147ce74a13f40f64522aaf6ffac5e40f5253fc8 /server/robot_session.py
parentd9a62c1cde08dd26d5675d46ff73f8ebb50a55ef (diff)
Add RobotSession
Currently it does nothing useful.
Diffstat (limited to 'server/robot_session.py')
-rw-r--r--server/robot_session.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/server/robot_session.py b/server/robot_session.py
new file mode 100644
index 0000000..e941641
--- /dev/null
+++ b/server/robot_session.py
@@ -0,0 +1,65 @@
+# coding=utf-8
+
+import threading
+
+from db.models import Problem
+from . import problems
+
+__all__ = ['RobotSession']
+
+class RobotSession(object):
+ """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):
+ language, problem_group, problem = Problem.get_identifier(problem_id)
+ language_module = problems.load_language(language, 'common')
+ problem_module = problems.load_problem(language, problem_group, problem, '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):
+ language, problem_group, problem = Problem.get_identifier(problem_id)
+ language_module = problems.load_language(language, 'common')
+ problem_module = problems.load_problem(language, problem_group, problem, 'common')
+
+ try:
+ passed, hints = problem_module.test(program)
+ 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)