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//=============================================================================================================
64{
65 bool success = false;
66 int exitCode = -1;
67 bool timedOut = false;
68 QString stdOut;
69 QString stdErr;
70 float progressPct = -1.0f;
71};
72
73//=============================================================================================================
82{
83 QString pythonExe = QStringLiteral("python3");
84 QString workingDir;
85 QStringList extraEnv;
86 int timeoutMsec = -1;
87 bool unbuffered = true;
88 QString venvDir;
89 QString packageDir;
91};
92
93//=============================================================================================================
100using PythonLineCallback = std::function<void(int channel, const QString& line)>;
101
102//=============================================================================================================
109using PythonProgressCallback = std::function<void(float pct, const QString& msg)>;
110
111//=============================================================================================================
130class UTILSSHARED_EXPORT PythonRunner : public QObject
131{
132 Q_OBJECT
133
134public:
135 //=========================================================================================================
141 explicit PythonRunner(QObject* pParent = nullptr);
142
143 //=========================================================================================================
150 explicit PythonRunner(const PythonRunnerConfig& config, QObject* pParent = nullptr);
151
152 //=========================================================================================================
159
160 //=========================================================================================================
166 const PythonRunnerConfig& config() const;
167
168 //=========================================================================================================
175
176 //=========================================================================================================
187
188 //=========================================================================================================
197 PythonRunnerResult run(const QString& scriptPath,
198 const QStringList& args = {});
199
200 //=========================================================================================================
209 PythonRunnerResult runCode(const QString& code,
210 const QStringList& args = {});
211
212 //=========================================================================================================
218 bool isPythonAvailable() const;
219
220 //=========================================================================================================
226 QString pythonVersion() const;
227
228 //=========================================================================================================
236 bool isPackageAvailable(const QString& packageName) const;
237
238 //=========================================================================================================
257
258 //=========================================================================================================
270 PythonRunnerResult runInVenv(const QString& scriptPath,
271 const QStringList& args = {});
272
273 //=========================================================================================================
279 QString venvPythonPath() const;
280
281signals:
282 //=========================================================================================================
289 void lineReceived(int channel, const QString& line);
290
291 //=========================================================================================================
298 void progressUpdated(float pct, const QString& msg);
299
300 //=========================================================================================================
307
308private:
309 PythonRunnerConfig m_config;
310 PythonLineCallback m_lineCb;
311 PythonProgressCallback m_progressCb;
312
313 //=========================================================================================================
317 PythonRunnerResult execute(const QStringList& fullArgs);
318
319 //=========================================================================================================
325 bool parseProgressLine(const QString& line, float& pct, QString& msg) const;
326};
327
328} // namespace UTILSLIB
329
330#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)