summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@araneo.org>2015-09-04 16:55:36 +0200
committerTimotej Lazar <timotej.lazar@araneo.org>2015-09-04 16:55:36 +0200
commit3752341cf43043fe89742baff35ad74df9fe0f4d (patch)
tree05d7cabe2e55c3f0d4454e32951e74dfffc9ca77 /js
parent3a6dce51204e33fb063c504b442822067b1ac008 (diff)
Initial Python intepreter implementation
Using the new codeq.console library.
Diffstat (limited to 'js')
-rw-r--r--js/codeq/comms.js10
-rw-r--r--js/python.js66
2 files changed, 54 insertions, 22 deletions
diff --git a/js/codeq/comms.js b/js/codeq/comms.js
index 8b89cde..25c1f8c 100644
--- a/js/codeq/comms.js
+++ b/js/codeq/comms.js
@@ -121,6 +121,16 @@
return send('query', query);
},
+ sendPush: function commsSendPush (json) {
+ json['sid'] = codeq.sid;
+ return send('python_push', json);
+ },
+
+ sendPull: function commsSendPull (json) {
+ json['sid'] = codeq.sid;
+ return send('python_pull', json);
+ },
+
sendHint: function commsSendHint (json) {
json['sid'] = codeq.sid;
return send('hint', json);
diff --git a/js/python.js b/js/python.js
index f4703b2..df23773 100644
--- a/js/python.js
+++ b/js/python.js
@@ -7,17 +7,45 @@
var firstCharacterPos = {'line': 0, 'ch': 0};
var makePythonTerminalHandler = function (jqConsole, editor, problem_id, activityHandler) {
- var terminal = jqConsole.terminal(function (command, terminal) {
- terminal.echo('Not implemented yet.');
- }, {
- 'history': true,
- 'prompt': '>>> ',
- 'greetings': 'CodeQ python terminal proxy',
- 'exit': false,
- 'clear': false
- });
+ var terminal = codeq.makeConsole(jqConsole, {
+ 'greeting': 'CodeQ Python terminal proxy'
+ });
+ terminal.onInput = function (text) {
+ if (!text)
+ return;
+ codeq.comms.sendPush({
+ 'text': text + '\n'
+ }).then(
+ function pushSuccess (data) {
+ if (data.code !== 0)
+ terminal.append('error: ' + data.message);
+ },
+ function pushFailed (error) {
+ terminal.append('exception: '+ error);
+ }
+ ).done();
+ };
+
+ var interval_id = setInterval(function () {
+ codeq.comms.sendPull({}).then(
+ function pullSuccess (data) {
+ if (data.code === 0) {
+ t = data.terminal;
+ if (!t.text)
+ return;
+ terminal.append(t.text);
+ }
+ else {
+ terminal.append('error: ' + data.message);
+ }
+ },
+ function pullFailed (error) {
+ terminal.append('exception: '+ error);
+ }
+ ).done();
+ }, 200);
- return {};
+ return terminal;
};
var makeActivityHandler = function (editor) {
@@ -282,8 +310,7 @@
});
$('#btn_code_hint').on('click', function () {
// handler.processServerHints([{id:'drop_down', start: 20, end: 26, choices:['ena', 'dva', 'tri']}]);
- jqConsole.echo('?- hint.');
- jqConsole.pause();
+ terminal.append('>>> hint\n');
var doc = editor.getDoc();
codeq.comms.sendHint({
'language': 'python',
@@ -291,21 +318,18 @@
'problem_id': problem.id
}).then(
function hintSuccess(data) {
- jqConsole.resume();
if (data.code === 0)
handler.processServerHints(data.hints);
else
- jqConsole.error(data.message);
+ terminal.append('error: ' + data.message);
},
function hintFailed (error) {
- jqConsole.resume();
- jqConsole.exception(error);
+ terminal.append('exception: ' + error);
}
).done();
});
$('#btn_code_test').on('click', function () {
- jqConsole.echo('?- test.');
- jqConsole.pause();
+ terminal.append('>>> test\n');
var doc = editor.getDoc();
codeq.comms.sendTest({
'language': 'python',
@@ -313,15 +337,13 @@
'problem_id': problem.id
}).then(
function testSuccess(data) {
- jqConsole.resume();
if (data.code === 0)
handler.processServerHints(data.hints);
else
- jqConsole.error(data.message);
+ terminal.append('error: ' + data.message);
},
function testFailed (error) {
- jqConsole.resume();
- jqConsole.exception(error);
+ terminal.append('exception: ' + error);
}
).done();
});