11#include <QTemporaryFile>
22QString PYTHON_V1_WARN =
"Warning: Python API v1 call has been made.\n";
34 if (g_log.
is(Logger::Priority::PRIO_DEBUG))
35 g_log.
debug() <<
"Running Python code:\n" << qPrintable(code) <<
"\n";
47 QTemporaryFile tmp_file;
48 if (!tmp_file.open()) {
49 throw std::runtime_error(
"An error occurred opening a temporary file in " + QDir::tempPath().toStdString());
52 QString tmpstring = tmp_file.fileName();
54 QString code_to_run =
"from __future__ import (absolute_import, division, print_function)\n"
55 "import sys; sys.stdout = open(\"" +
56 tmpstring +
"\", 'w');\n" + code;
61 QTextStream stream(&tmp_file);
64 while (!stream.atEnd()) {
65 tmpstring.append(stream.readLine().trimmed() +
"\n");
67 if (g_log.
is(Logger::Priority::PRIO_DEBUG))
68 g_log.
debug() <<
"Raw output from execution:\n" << qPrintable(tmpstring) <<
"\n";
78 QStringList::const_iterator end = list.end();
79 for (QStringList::const_iterator it = list.begin(); it != end; ++it) {
80 tuple +=
"'" + *it +
"',";
QString runPythonCode(const QString &code, bool no_output=false)
Run python code.
void runAsPythonScript(const QString &code, bool)
static const QString stringList2Tuple(const QStringList &list)
Converts a list of strings into a string recognised by Python as a tuple.
The Logger class is in charge of the publishing messages from the framework through various channels.
void debug(const std::string &msg)
Logs at debug level.
bool is(int level) const
Returns true if at least the given log level is set.
Kernel::Logger g_log("ExperimentInfo")
static logger object