HAL
python_thread.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 #include <QThread>
28 #include <QString>
29 #include <QMutex>
30 #include <QVariant>
31 #include <QElapsedTimer>
33 
34 namespace hal {
35  class Module;
36  class Gate;
37 
48  {
49  int mState;
50  public:
52  PythonMutex();
53 
55  ~PythonMutex();
56  };
57 
59  {
60  public:
62  private:
63  Q_OBJECT
64  QString mScript;
65  QString mErrorMessage;
66  QString mResult;
67  QString mStdoutBuffer;
68  unsigned long mPythonThreadID;
69  QVariant mInput;
70  QMutex mInputMutex;
71  QElapsedTimer mElapsedTimer;
72  bool mSingleStatement;
73  bool mAbortRequested;
74  int mSpamCount;
75  Q_SIGNALS:
76  void stdOutput(QString txt);
77  void stdError(QString txt);
78  void requireInput(int type, QString prompt, QVariant defaultValue);
79  public:
80  PythonThread(const QString& script, bool singleStatement, QObject* parent = nullptr);
81  ~PythonThread();
82  void run() override;
83  void interrupt();
84  QString errorMessage() const { return mErrorMessage; }
85  QString result() const { return mResult; }
86  void handleStdout(const QString& output) override;
87  void handleError(const QString& output) override;
88  std::string handleConsoleInput(const QString& prompt);
89  std::string handleStringInput(const QString& prompt, const QString& defval);
90  int handleNumberInput(const QString& prompt, int defval);
91  Module* handleModuleInput(const QString& prompt);
92  Gate* handleGateInput(const QString& prompt);
93  std::string handleFilenameInput(const QString& prompt, const QString& filetype);
94  void clear() override;
95  void setInput(const QVariant& inp);
96  bool getInput(InputType type, QString prompt, QVariant defaultValue);
97  void unlock();
98  const QString& stdoutBuffer() const { return mStdoutBuffer; }
100  };
101 }
Definition: gate.h:58
Interface for handling python outputs.
PythonMutex()
Aquire GIL.
~PythonMutex()
Release GIL.
void clear() override
void stdError(QString txt)
Gate * handleGateInput(const QString &prompt)
void handleStdout(const QString &output) override
QString errorMessage() const
Definition: python_thread.h:84
QString result() const
Definition: python_thread.h:85
void setInput(const QVariant &inp)
std::string handleStringInput(const QString &prompt, const QString &defval)
int handleNumberInput(const QString &prompt, int defval)
bool getInput(InputType type, QString prompt, QVariant defaultValue)
std::string handleConsoleInput(const QString &prompt)
Module * handleModuleInput(const QString &prompt)
void handleError(const QString &output) override
void stdOutput(QString txt)
void requireInput(int type, QString prompt, QVariant defaultValue)
const QString & stdoutBuffer() const
Definition: python_thread.h:98
void run() override
PythonThread(const QString &script, bool singleStatement, QObject *parent=nullptr)
std::string handleFilenameInput(const QString &prompt, const QString &filetype)
PinType type
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
QObject * parent() const const