From bd170fb7b4dbfc8eb3a18ecd6cdd437d5e11b5ed Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Thu, 6 Oct 2016 14:43:48 +0200 Subject: Python: add support for auxiliary code (like for Prolog) --- server/handlers.py | 22 +++++++++++++--------- server/python_session.py | 23 ++++++++++++++--------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/server/handlers.py b/server/handlers.py index 5e06463..7e27d55 100644 --- a/server/handlers.py +++ b/server/handlers.py @@ -1,5 +1,5 @@ # CodeQ: an online programming tutor. -# Copyright (C) 2015 UL FRI +# Copyright (C) 2015,2016 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 @@ -195,15 +195,19 @@ class Query(CodeqService): # Push user program to the Python interpreter to be exec'd. class PythonExec(CodeqService): def process(self, request): - program = request.data.get('program') - python = request.session.current_language_session() - if program is None: - request.reply({'code': 1, 'message': 'No program specified'}) - elif not isinstance(python, server.python_session.PythonSession): - request.reply({'code': 2, 'message': 'The currently active session is not Python'}) + problem_id = request.data.get('problem_id') + if problem_id is None: + request.reply({'code': 4, 'message': 'Problem ID not given'}) else: - python.exec(program) - request.reply({'code': 0, 'message': 'ok'}) + program = request.data.get('program') + python = request.session.current_language_session() + if program is None: + request.reply({'code': 1, 'message': 'No program specified'}) + elif not isinstance(python, server.python_session.PythonSession): + request.reply({'code': 2, 'message': 'The currently active session is not Python'}) + else: + python.exec(program, problem_id) + request.reply({'code': 0, 'message': 'ok'}) # Send an interrupt to the Python interpreter. diff --git a/server/python_session.py b/server/python_session.py index ecb075a..2eb2a69 100644 --- a/server/python_session.py +++ b/server/python_session.py @@ -1,5 +1,5 @@ # CodeQ: an online programming tutor. -# Copyright (C) 2015 UL FRI +# Copyright (C) 2015,2016 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 @@ -26,9 +26,10 @@ import sys import threading import time +from db.models import Problem import server import server.user_session -from db.models import Problem +from server.problems import get_facts, load_language, load_problem __all__ = ['PythonSession'] @@ -82,8 +83,11 @@ class PythonSession(server.LanguageSession): return results - def exec(self, program): - self._notifier.send(('exec', program)) + def exec(self, program, problem_id): + problem = Problem.get(id=problem_id) + problem_module = load_problem(problem.language, problem.group, problem.identifier, 'common') + aux_code = get_facts(problem.language, problem_module) + self._notifier.send(('exec', aux_code+program)) def push(self, stdin): self._notifier.send(('push', stdin)) @@ -105,8 +109,9 @@ class PythonSession(server.LanguageSession): 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') + language_module = load_language(p.language, 'common') + problem_module = load_problem(p.language, p.group, p.identifier, 'common') + aux_code = get_facts(p.language, problem_module) # experiment support for allowing none/automatic/manual/all hints # descriptor: {'id': 'hints', 'group': 'none|automatic|manual|all'} @@ -117,19 +122,19 @@ class PythonSession(server.LanguageSession): break # check if the program is correct - passed, msgs = problem_module.test(self.run, program) + passed, msgs = problem_module.test(self.run, program, aux_code) if passed: session.update_solution(problem_id, solution=program, done=True) else: hints = [] # manually defined problem-specific hints if not hints and hasattr(language_module, 'hint'): - hints = language_module.hint(self.run, program) + hints = language_module.hint(self.run, program, aux_code) # generic language hints (style etc.) if not hints and hasattr(problem_module, 'hint') and \ allowed_hints in ('all', 'manual'): - hints = problem_module.hint(self.run, program) + hints = problem_module.hint(self.run, program, aux_code) if hints: msgs.extend(hints) -- cgit v1.2.1