Mantid
Loading...
Searching...
No Matches
PythonRunner.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9
10#include <QDir>
11#include <QTemporaryFile>
12#include <QTextStream>
13#include <sstream>
14#include <stdexcept>
15
16using namespace MantidQt::API;
17
18namespace {
19Mantid::Kernel::Logger g_log("PythonRunner");
20
22QString PYTHON_V1_WARN = "Warning: Python API v1 call has been made.\n";
23} // namespace
24
31QString PythonRunner::runPythonCode(const QString &code, bool no_output) {
33
34 if (g_log.is(Logger::Priority::PRIO_DEBUG))
35 g_log.debug() << "Running Python code:\n" << qPrintable(code) << "\n";
36
37 if (no_output) {
38 emit runAsPythonScript(code, true);
39 return QString();
40 }
41
42 // Otherwise we need to gather the information from stdout. This is achieved
43 // by redirecting the stdout stream
44 // to a temproary file and then reading its contents
45 // A QTemporaryFile object is used since the file is automatically deleted
46 // when the object goes out of scope
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());
50 }
51 // The file name is only valid when the file is open
52 QString tmpstring = tmp_file.fileName();
53 tmp_file.close();
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;
57 emit runAsPythonScript(code_to_run, true);
58
59 // Now get the output
60 tmp_file.open();
61 QTextStream stream(&tmp_file);
62 tmpstring.clear();
63
64 while (!stream.atEnd()) {
65 tmpstring.append(stream.readLine().trimmed() + "\n");
66 }
67 if (g_log.is(Logger::Priority::PRIO_DEBUG))
68 g_log.debug() << "Raw output from execution:\n" << qPrintable(tmpstring) << "\n";
69 return tmpstring;
70}
76const QString PythonRunner::stringList2Tuple(const QStringList &list) {
77 QString tuple("(");
78 QStringList::const_iterator end = list.end();
79 for (QStringList::const_iterator it = list.begin(); it != end; ++it) {
80 tuple += "'" + *it + "',";
81 }
82 tuple += ")";
83 return tuple;
84}
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.
Definition: Logger.h:52
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
bool is(int level) const
Returns true if at least the given log level is set.
Definition: Logger.cpp:146
Kernel::Logger g_log("ExperimentInfo")
static logger object