Commit 9496ec38 authored by NICLAUSSE Nicolas's avatar NICLAUSSE Nicolas

add option to redirect io (disable by default); implemented in pythonInterpreter

parent 7da8ef13
......@@ -48,7 +48,7 @@ public:
public slots:
virtual QString interpret(const QString& command, int *stat) = 0;
virtual void init(void) {} ;
virtual void init(bool redirect_io = false) {} ;
private:
dtkScriptInterpreterPrivate *d;
......
......@@ -34,7 +34,11 @@ static const QString dtkScriptInterpreterPythonRedirector_declare =
" def __init__(self):\n"
" self.data = ''\n"
" def write(self, stuff):\n"
" self.data+= stuff\n";
" self.data+= stuff\n"
" def flush(self):\n"
" pass\n"
" def reset(self):\n"
" self.data = ''\n";
static const QString dtkScriptInterpreterPythonRedirector_define =
"redirector = Redirector()\n"
......@@ -49,8 +53,14 @@ class dtkScriptInterpreterPythonPrivate
{
public:
QString buffer;
bool redirect_io = false;
public:
PyObject *module = nullptr;
};
// /////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////
......@@ -77,9 +87,14 @@ dtkScriptInterpreterPython::~dtkScriptInterpreterPython(void)
d = nullptr;
}
void dtkScriptInterpreterPython::init(void)
void dtkScriptInterpreterPython::init(bool redirect_io)
{
Py_Initialize();
d->redirect_io = redirect_io;
if (redirect_io) {
PyRun_SimpleString(dtkScriptInterpreterPythonRedirector_declare.toUtf8().constData());
}
QSettings settings(QSettings::IniFormat, QSettings::UserScope, "inria", "dtk-script");
settings.beginGroup("modules");
......@@ -102,6 +117,9 @@ void dtkScriptInterpreterPython::init(void)
} else {
dtkWarn() << Q_FUNC_INFO << "No init function";
}
if (redirect_io) {
PyRun_SimpleString(dtkScriptInterpreterPythonRedirector_define.toUtf8().constData());
}
}
void dtkScriptInterpreterPython::release(void)
......@@ -141,16 +159,32 @@ QString dtkScriptInterpreterPython::interpret(const QString& command, int *stat)
return QString();
}
PyObject *module = PyImport_AddModule("__main__");
if (!d->module)
d->module = PyImport_AddModule("__main__");
switch (PyRun_SimpleString(qPrintable(statement))) {
switch(PyRun_SimpleString(qPrintable(statement))) {
case 0: *stat = Status_Ok; break;
case -1: *stat = Status_Error; break;
default: break;
}
PyErr_Print();
if (d->redirect_io) {
PyObject *redtor = PyObject_GetAttrString(d->module, "redirector");
if (redtor) {
PyObject *lresult = PyObject_GetAttrString(redtor, "data");
char *method = (char *)"reset";
PyObject_CallMethod(redtor, method, nullptr);
QString s = QString(PyString_AsString(lresult)).simplified();
Py_DECREF(redtor);
return s;
} else {
qDebug() << "no redirector found";
return QString();
}
}
PyErr_Print();
return QString();
}
......
......@@ -29,8 +29,8 @@ protected:
static dtkScriptInterpreterPython *s_instance;
public slots:
QString interpret(const QString& command, int *stat);
void init(void);
QString interpret(const QString& command, int *stat) override;
void init(bool redirect_io = false) override ;
void release(void);
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment