v2.0.0
Loading...
Searching...
No Matches
python_runner.h
Go to the documentation of this file.
1//=============================================================================================================
34
35#ifndef PYTHON_RUNNER_H
36#define PYTHON_RUNNER_H
37
38//=============================================================================================================
39// INCLUDES
40//=============================================================================================================
41
42#include "utils_global.h"
43
44//=============================================================================================================
45// QT INCLUDES
46//=============================================================================================================
47
48#include <QObject>
49#include <QString>
50#include <QStringList>
51#include <QProcessEnvironment>
52
53#include <functional>
54
55//=============================================================================================================
56// DEFINE NAMESPACE UTILSLIB
57//=============================================================================================================
58
59namespace UTILSLIB
60{
61
62//=============================================================================================================
69{
70 bool success = false;
71 int exitCode = -1;
72 bool timedOut = false;
73 QString stdOut;
74 QString stdErr;
75 float progressPct = -1.0f;
76};
77
78//=============================================================================================================
85{
86 QString pythonExe = QStringLiteral("python3");
87 QString workingDir;
88 QStringList extraEnv;
89 int timeoutMsec = -1;
90 bool unbuffered = true;
91 QString venvDir;
92 QString packageDir;
94};
95
96//=============================================================================================================
103using PythonLineCallback = std::function<void(int channel, const QString& line)>;
104
105//=============================================================================================================
112using PythonProgressCallback = std::function<void(float pct, const QString& msg)>;
113
114//=============================================================================================================
131class UTILSSHARED_EXPORT PythonRunner : public QObject
132{
133 Q_OBJECT
134
135public:
136 //=========================================================================================================
142 explicit PythonRunner(QObject* pParent = nullptr);
143
144 //=========================================================================================================
151 explicit PythonRunner(const PythonRunnerConfig& config, QObject* pParent = nullptr);
152
153 //=========================================================================================================
160
161 //=========================================================================================================
167 const PythonRunnerConfig& config() const;
168
169 //=========================================================================================================
176
177 //=========================================================================================================
188
189 //=========================================================================================================
198 PythonRunnerResult run(const QString& scriptPath,
199 const QStringList& args = {});
200
201 //=========================================================================================================
210 PythonRunnerResult runCode(const QString& code,
211 const QStringList& args = {});
212
213 //=========================================================================================================
219 bool isPythonAvailable() const;
220
221 //=========================================================================================================
227 QString pythonVersion() const;
228
229 //=========================================================================================================
237 bool isPackageAvailable(const QString& packageName) const;
238
239 //=========================================================================================================
258
259 //=========================================================================================================
271 PythonRunnerResult runInVenv(const QString& scriptPath,
272 const QStringList& args = {});
273
274 //=========================================================================================================
280 QString venvPythonPath() const;
281
282signals:
283 //=========================================================================================================
290 void lineReceived(int channel, const QString& line);
291
292 //=========================================================================================================
299 void progressUpdated(float pct, const QString& msg);
300
301 //=========================================================================================================
308
309private:
310 PythonRunnerConfig m_config;
311 PythonLineCallback m_lineCb;
312 PythonProgressCallback m_progressCb;
313
314 //=========================================================================================================
318 PythonRunnerResult execute(const QStringList& fullArgs);
319
320 //=========================================================================================================
326 bool parseProgressLine(const QString& line, float& pct, QString& msg) const;
327};
328
329} // namespace UTILSLIB
330
331#endif // PYTHON_RUNNER_H
utils library export/import macros.
#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)