From a293cf9d5fd64ff29bc05daa80d2f9a3a8c0abe0 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Fri, 4 Sep 2015 16:57:17 +0200 Subject: Add Python push&pull services to wsgi_server --- server/python_session.py | 4 +++- wsgi_server.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/server/python_session.py b/server/python_session.py index 161ba0d..4c4a781 100644 --- a/server/python_session.py +++ b/server/python_session.py @@ -28,6 +28,8 @@ class PythonSession(object): _m.connect() self._python = _m.Python() + self.create() + def run(self, code=None, inputs=None, timeout=1.0): with self._access_lock: return self._python.run(code, inputs, timeout) @@ -40,7 +42,7 @@ class PythonSession(object): def pull(self): with self._access_lock: if self._interpreter is None: - return 'Python is not running' + return None return self._python.pull(self._interpreter) def push(self, stdin): diff --git a/wsgi_server.py b/wsgi_server.py index fda576b..324f338 100644 --- a/wsgi_server.py +++ b/wsgi_server.py @@ -143,6 +143,24 @@ class Query(CodeqService): session.update_solution(problem_id, trace, program) return result +# Pull stdout/stderr from the session's Python interpreter. +class PythonPull(CodeqService): + def process(self, js, session): + python = session.get_python() + output = python.pull() + return {'code': 0, 'message': 'ok', 'terminal': {'text': output if output else ''}} + +# Push stdin to the session's Python interpreter. +class PythonPush(CodeqService): + def process(self, js, session): + text = js.get('text') + if text is None: + return {'code': 1, 'message': 'No input specified'} + + python = session.get_python() + python.push(text) + return {'code': 0, 'message': 'ok'} + class Hint(CodeqService): def process(self, js, session): language = js.get('language') @@ -211,6 +229,9 @@ api.add_route('/activity', activity) query = Query() api.add_route('/query', query) +api.add_route('/python_pull', PythonPull()) +api.add_route('/python_push', PythonPush()) + hint = Hint() api.add_route('/hint', hint) -- cgit v1.2.1