Kactus2
Kactus2 reference guide
Loading...
Searching...
No Matches
PythonInterpreter.h
Go to the documentation of this file.
1//-----------------------------------------------------------------------------
2// File: PythonInterpreter.h
3//-----------------------------------------------------------------------------
4// Project: Kactus2
5// Author: Esko Pekkarinen
6// Date: 03.02.2021
7//
8// Description:
9// Convenience class for accessing Python interpreter.
10//-----------------------------------------------------------------------------
11
12#ifndef PYTHON_INTERPRETER_H
13#define PYTHON_INTERPRETER_H
14
15#define PY_SSIZE_T_CLEAN
16#include <Python.h>
17
18#include <QObject>
19#include <QFile>
20
21#include <string>
22
24
25//-----------------------------------------------------------------------------
27//-----------------------------------------------------------------------------
28class PythonInterpreter : public QObject, public WriteChannel
29{
30 Q_OBJECT
31
32public:
33
42 explicit PythonInterpreter(WriteChannel* outputChannel,
43 WriteChannel* errorChannel, bool printPromt = true,
44 QObject* parent = nullptr);
45
46 // ! The destructor.
47 ~PythonInterpreter() final = default;
48
56 bool initialize(bool interactive = true);
57
61 void finalize();
62
68 void execute(std::string const& line);
69
70signals:
71
73
74public slots:
75
81 virtual void write(QString const& command) override final;
82
88 void executeString(QString const& string);
89
97 int runFile(QString const& filePath);
98
99private:
100
108 bool redirectIO(bool interactive);
109
115 bool setAPI();
116
120 void printPrompt() const;
121
122 //-----------------------------------------------------------------------------
123 // Data.
124 //-----------------------------------------------------------------------------
125
127 std::string inputBuffer_{ };
128
130 bool printPrompt_{ true };
131
133 bool runMultiline_{ false };
134
136 WriteChannel* outputChannel_{ nullptr };
137
139 WriteChannel* errorChannel_{ nullptr };
140
142 PyObject* globalContext_{ nullptr };
143
145 PyObject* localContext_{ nullptr };
146
148 PyThreadState* threadState_{ nullptr };
149};
150
151#endif // PYTHON_INTERPRETER_H
PythonInterpreter(WriteChannel *outputChannel, WriteChannel *errorChannel, bool printPromt=true, QObject *parent=nullptr)
~PythonInterpreter() final=default
bool initialize(bool interactive=true)
int runFile(QString const &filePath)
void execute(std::string const &line)
void executeString(QString const &string)
virtual void write(QString const &command) override final
WriteChannel()=default
The constructor.