forked from kactus2/kactus2dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonInterpreter.h
More file actions
151 lines (119 loc) · 4.27 KB
/
PythonInterpreter.h
File metadata and controls
151 lines (119 loc) · 4.27 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
142
143
144
145
146
147
148
149
150
151
//-----------------------------------------------------------------------------
// File: PythonInterpreter.h
//-----------------------------------------------------------------------------
// Project: Kactus2
// Author: Esko Pekkarinen
// Date: 03.02.2021
//
// Description:
// Convenience class for accessing Python interpreter.
//-----------------------------------------------------------------------------
#ifndef PYTHON_INTERPRETER_H
#define PYTHON_INTERPRETER_H
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <QObject>
#include <QFile>
#include <string>
#include <PythonAPI/WriteChannel.h>
//-----------------------------------------------------------------------------
//! Convenience class for accessing Python interpreter.
//-----------------------------------------------------------------------------
class PythonInterpreter : public QObject, public WriteChannel
{
Q_OBJECT
public:
/*!
* The constructor.
*
* @param [in] outputChannel Channel for standard messages and interpreter output.
* @param [in] errorChannel Channel for error messages.
* @param [in] printPromt Flag for enabling prompt printing.
* @param [in] parent The parent object.
*/
explicit PythonInterpreter(WriteChannel* outputChannel,
WriteChannel* errorChannel, bool printPromt = true,
QObject* parent = nullptr);
// ! The destructor.
~PythonInterpreter() final = default;
/*!
* Initializes the interpreter. This function must be called before writing any commands with write().
*
* @param [in] interactive Flag for enabling interactive std input. Set to true on command-line.
*
* @return True, if initialization was successful, otherwise false.
*/
bool initialize(bool interactive = true);
/*!
* Clean up the interpreter state at end of life time.
*/
void finalize();
/*!
* Execute a single line in the interpreter.
*
* @param [in] line The line to execute.
*/
void execute(std::string const& line);
signals:
void executeDone();
public slots:
/*!
* Write a command for interpreter to execute.
*
* @param [in] command The command to execute.
*/
virtual void write(QString const& command) override final;
/*!
* Execute the given arbitrary long string in the interpreter.
*
* @param [in] string The string to execute.
*/
void executeString(QString const& string);
/*!
* Run a script from a given file.
*
* @param [in] filePath Path to the file to run.
*
* @return Return value of the script or 1 if script file couldn't be opened.
*/
int runFile(QString const& filePath);
private:
/*!
* Redirect the interpreter output and error output to WriteChannels.
*
* @param [in] interactive Flag for enabling interactive std input. Set to true on command-line.
*
* @return True, if the input and outputs could be redirected, otherwise false.
*/
bool redirectIO(bool interactive);
/*!
* Import and set the core api available in python context.
*
* @return True, if the api was set, otherwise false.
*/
bool setAPI();
/*!
* Write prompt to output.
*/
void printPrompt() const;
//-----------------------------------------------------------------------------
// Data.
//-----------------------------------------------------------------------------
//! Buffer to store multiple lines for command to execute.
std::string inputBuffer_{ };
//! Flag for enabling prompt printing.
bool printPrompt_{ true };
//! True, if the current command requires multiple lines (e.g. loop).
bool runMultiline_{ false };
//! Channel for interpreter output.
WriteChannel* outputChannel_{ nullptr };
//! Channel for interpreter errors.
WriteChannel* errorChannel_{ nullptr };
//! The global context for the interpreter.
PyObject* globalContext_{ nullptr };
//! The local context for the interpreter when running a command.
PyObject* localContext_{ nullptr };
//! Interpreter thread state value holder.
PyThreadState* threadState_{ nullptr };
};
#endif // PYTHON_INTERPRETER_H