v2.0.0
Loading...
Searching...
No Matches
python_runner.h
Go to the documentation of this file.
1//=============================================================================================================
27
28#ifndef PYTHON_RUNNER_H
29#define PYTHON_RUNNER_H
30
31//=============================================================================================================
32// INCLUDES
33//=============================================================================================================
34
35#include "utils_global.h"
36
37//=============================================================================================================
38// QT INCLUDES
39//=============================================================================================================
40
41#include <QObject>
42#include <QString>
43#include <QStringList>
44#include <QProcessEnvironment>
45
46#include <functional>
47
48//=============================================================================================================
49// DEFINE NAMESPACE UTILSLIB
50//=============================================================================================================
51
52namespace UTILSLIB
53{
54
55//=============================================================================================================
62{
63 bool success = false;
64 int exitCode = -1;
65 bool timedOut = false;
66 QString stdOut;
67 QString stdErr;
68 float progressPct = -1.0f;
69};
70
71//=============================================================================================================
78{
79 QString pythonExe = QStringLiteral("python3");
80 QString workingDir;
81 QStringList extraEnv;
82 int timeoutMsec = -1;
83 bool unbuffered = true;
84 QString venvDir;
85 QString packageDir;
87};
88
89//=============================================================================================================
96using PythonLineCallback = std::function<void(int channel, const QString& line)>;
97
98//=============================================================================================================
105using PythonProgressCallback = std::function<void(float pct, const QString& msg)>;
106
107//=============================================================================================================
124class UTILSSHARED_EXPORT PythonRunner : public QObject
125{
126 Q_OBJECT
127
128public:
129 //=========================================================================================================
135 explicit PythonRunner(QObject* pParent = nullptr);
136
137 //=========================================================================================================
144 explicit PythonRunner(const PythonRunnerConfig& config, QObject* pParent = nullptr);
145
146 //=========================================================================================================
153
154 //=========================================================================================================
160 const PythonRunnerConfig& config() const;
161
162 //=========================================================================================================
169
170 //=========================================================================================================
181
182 //=========================================================================================================
191 PythonRunnerResult run(const QString& scriptPath,
192 const QStringList& args = {});
193
194 //=========================================================================================================
203 PythonRunnerResult runCode(const QString& code,
204 const QStringList& args = {});
205
206 //=========================================================================================================
212 bool isPythonAvailable() const;
213
214 //=========================================================================================================
220 QString pythonVersion() const;
221
222 //=========================================================================================================
230 bool isPackageAvailable(const QString& packageName) const;
231
232 //=========================================================================================================
251
252 //=========================================================================================================
264 PythonRunnerResult runInVenv(const QString& scriptPath,
265 const QStringList& args = {});
266
267 //=========================================================================================================
273 QString venvPythonPath() const;
274
275signals:
276 //=========================================================================================================
283 void lineReceived(int channel, const QString& line);
284
285 //=========================================================================================================
292 void progressUpdated(float pct, const QString& msg);
293
294 //=========================================================================================================
301
302private:
303 PythonRunnerConfig m_config;
304 PythonLineCallback m_lineCb;
305 PythonProgressCallback m_progressCb;
306
307 //=========================================================================================================
311 PythonRunnerResult execute(const QStringList& fullArgs);
312
313 //=========================================================================================================
319 bool parseProgressLine(const QString& line, float& pct, QString& msg) const;
320};
321
322} // namespace UTILSLIB
323
324#endif // PYTHON_RUNNER_H
Public umbrella header for UTILSLIB exposing the UTILSSHARED_EXPORT macro and build-info accessors.
#define UTILSSHARED_EXPORT
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
std::function< void(float pct, const QString &msg)> PythonProgressCallback
std::function< void(int channel, const QString &line)> PythonLineCallback
Script execution result container.
Script execution configuration.
const PythonRunnerConfig & config() const
void setLineCallback(PythonLineCallback cb)
void setConfig(const PythonRunnerConfig &config)
QString pythonVersion() const
QString venvPythonPath() const
void progressUpdated(float pct, const QString &msg)
bool isPackageAvailable(const QString &packageName) const
PythonRunnerResult runCode(const QString &code, const QStringList &args={})
PythonRunnerResult ensureVenv()
PythonRunnerResult run(const QString &scriptPath, const QStringList &args={})
void setProgressCallback(PythonProgressCallback cb)
void finished(const UTILSLIB::PythonRunnerResult &result)
PythonRunnerResult runInVenv(const QString &scriptPath, const QStringList &args={})
PythonRunner(QObject *pParent=nullptr)
void lineReceived(int channel, const QString &line)