diff options
-rw-r--r-- | server/python_session.py | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/server/python_session.py b/server/python_session.py index 011adc3..87ae60f 100644 --- a/server/python_session.py +++ b/server/python_session.py @@ -19,6 +19,7 @@ class PythonSession(object): """ def __init__(self): self._access_lock = threading.Lock() + self._sent_hints = [] self._interpreter = None # Proxy for calling the Python runner. We use a separate connection for @@ -67,31 +68,42 @@ class PythonSession(object): def hint(self, sid, problem_id, program): session = server.user_session.get_session_by_id(sid) 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') - # Check syntax. - try: - tree = ast.parse(program, filename='user') - except SyntaxError as ex: - error_msg = '{}{}^\n{}'.format(ex.text, ' '*(ex.offset-1), ex.msg) - return [{'id': 'syntax_error', 'args': {'lineno': ex.lineno, 'message': error_msg}}] - - # Try problem-specific hints. - if hasattr(problem_module, 'hint'): + hints = [] + if hasattr(language_module, 'hint'): + hints = language_module.hint(program) + if not hints and hasattr(problem_module, 'hint'): hints = problem_module.hint(session, program) - if hints: - return hints + if not hints: + hints = [{'id': 'no_hint'}] - # Finally return a generic "try thinking a bit" message. - return [{'id': 'no_hint'}] + self._instantiate_and_save_hints(language_module, problem_module, hints) + return hints def test(self, sid, problem_id, program): session = server.user_session.get_session_by_id(sid) 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: n_correct, n_all = problem_module.test(session, program) - return [{'id': 'test_results', 'args': {'passed': n_correct, 'total': n_all}}] + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': n_all}}] except AttributeError as ex: - return [{'id': 'test_results', 'args': {'passed': 0, 'total': 0}}] + hints = [{'id': 'test_results', 'args': {'passed': 0, 'total': 0}}] + + 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) |