-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgtps_pythoninterpreter.h
More file actions
141 lines (118 loc) · 3.78 KB
/
gtps_pythoninterpreter.h
File metadata and controls
141 lines (118 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* GTlab - Gas Turbine laboratory
* Source File: gtps_pythoninterpreter.h
*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: 2024 German Aerospace Center (DLR)
*
* Created on: 20.10.2022
* Author: Marvin Noethen (DLR AT-TWK)
*/
#ifndef GTPSPYTHONINTERPRETER_H
#define GTPSPYTHONINTERPRETER_H
#include <QString>
#include <QStringList>
#include <QFile>
#include "gt_versionnumber.h"
/**
* @brief The GtpsPythonInterpreter class
*/
class GtpsPythonInterpreter
{
public:
enum Status
{
Valid = 0,
Invalid,
NotSupported
};
/**
* @brief GtpsPythonInterpreter
* @param pythonExe Path to the Python executable.
*/
explicit GtpsPythonInterpreter(const QString& pythonExe);
/**
* @brief Returns true if the Python executalbe is valid and the evaluator
* can execute Python code. Otherwise it returns false.
* @return True, if the evaluator can execute Python code.
*/
bool isValid();
/**
* @brief Returns the Python version the evaluator uses.
* @return The Python version the evaluator uses.
*/
const GtVersionNumber& version() const;
/**
* @brief Returns the shared Python library filename
* (e.g. /path/to/libpython.3.9.so)
* @return Filename of to the shared Python library.
*/
const QString& sharedLib() const;
/**
* @brief Returns the list of paths that are registered in sys.path.
* @return The list of paths that are registered in sys.path.
*/
const QStringList& sysPaths() const;
/**
* @brief Retruns the PYTHONHOME path.
* @return The PYTHONHOME path.
*/
QString pythonHomePath() const;
/**
* @brief Evaluates the given pythonCommand using the Python executable
* passed to the constructor.
* @param pythonCommand Python command to evaluate.
* @param ok Optional control flag. Is true if the evaluation
* was successful.
* @return The output string resulting from the execution of the
* Python code.
*/
QString runCommand(const QString& pythonCommand, bool* ok = nullptr) const;
/**
* @brief Runs a python script
* @param scriptFile The file to the script. Note, this file needs to
* be already written to disk and must have a filename.
*
* @param ok Optional control flag. Is true if the evaluation
* was successful.
* @return The output string resulting from the execution of the
* Python code.
*/
QString runScript(const QFile& scriptFile, bool* ok = nullptr) const;
/**
* @brief Function to call the python interpreter with generic arguments
* @param args The argument list to pass to python
* @param ok Optional control flag. Is true if the evaluation
* was successful.
* @return The output string resulting from the execution of the
* Python code.
*/
QString runPythonInterpreter(const QStringList& args, bool* ok = nullptr) const;
Status status() const;
private:
// Python sys paths.
QStringList m_sysPaths{};
// Python executalbe.
QString m_pythonExe{};
// Shared python library
QString m_sharedLib{};
// Python version.
GtVersionNumber m_pyVersion{};
// Interpreter status.
Status m_status{Invalid};
/**
* @brief Asks the Python interpreter for the Python version and stores
* the result in the version variable.
*/
void setPythonVersion();
/**
* @brief Asks the interpreter for the path to the shared library and
* stores it in the shared lib variable.
*/
void setSharedLibPath();
/**
* @brief Asks the interpreter for the paths registered in sys.path and
* stores them in the sys paths variable.
*/
void setSysPaths();
};
#endif // GTPSPYTHONINTERPRETER_H