alb3rt-python
Version:
50 lines (38 loc) • 1.25 kB
JavaScript
;
const fs = require('fs-extra'),
PythonShell = require('python-shell'),
CONFIG = require('alb3rt-core').config;
class Alb3rtPython {
constructor() {
this.scripts = {};
this.run = this.run.bind(this);
this.stop = this.stop.bind(this);
}
abort(id, reason) {
console.log('[alb3rt-python] Aborting attempt to run script', id, 'reason:', reason);
}
run(id, onSuccessCallback) {
const scriptPath = CONFIG.PYTHON_SCRIPTS_PATH + '/' + id + '.py';
fs.exists(scriptPath, scriptExists => {
if (!scriptExists) {
this.abort(id, 'doesn\'t exist...');
return;
}
if (this.scripts[id]) {
this.abort(id, 'already running...');
return;
}
const shell = new PythonShell(scriptPath);
console.log('[alb3rt-python] Running script', id);
this.scripts[id] = shell;
onSuccessCallback(shell);
});
}
stop(id) {
if (this.scripts[id]) {
console.log('[alb3rt-python] Terminating script', id);
delete this.scripts[id];
}
}
}
module.exports = new Alb3rtPython();