# CodeQ: an online programming tutor. # Copyright (C) 2015 UL FRI # # This program is free software: you can redistribute it and/or modify it under # the terms of the GNU Affero General Public License as published by the Free # Software Foundation, either version 3 of the License, or (at your option) any # later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more # details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . 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): session = server.user_session.get_session_by_id(sid) 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') # experiment support for allowing none/automatic/manual/all hints # descriptor: {'id': 'hints', 'group': 'none|automatic|manual|all'} allowed_hints = 'all' for experiment in session.get_experiments(): if experiment.get('id') == 'hints': allowed_hints = experiment.get('group') break hints = [] # manually defined problem-specific hints if not hints and hasattr(problem_module, 'hint') and \ allowed_hints in ('all', 'manual'): hints = problem_module.hint(program) # generic language hints (style etc.) if not hints and hasattr(language_module, 'hint'): hints = language_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, solution=program, 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()