Tulip  4.4.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
PythonInterpreter.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 /**
21  *
22  * This file is part of Tulip (www.tulip-software.org)
23  *
24  * Authors: David Auber and the Tulip development Team
25  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
26  *
27  * Tulip is free software; you can redistribute it and/or modify
28  * it under the terms of the GNU Lesser General Public License
29  * as published by the Free Software Foundation, either version 3
30  * of the License, or (at your option) any later version.
31  *
32  * Tulip is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
35  * See the GNU General Public License for more details.
36  *
37  */
38 
39 #ifndef PYTHONINTERPRETER_H_
40 #define PYTHONINTERPRETER_H_
41 
42 #ifndef PyObject_HEAD
43 struct _object;
44 typedef _object PyObject;
45 #endif
46 
47 #include <tulip/PythonCppTypesConverter.h>
48 #include <tulip/Graph.h>
49 
50 #include <QVector>
51 #include <QSet>
52 #include <QString>
53 
54 class QAbstractScrollArea;
55 class QPlainTextEdit;
56 class QTextBrowser;
57 
58 namespace tlp {
59 
60 class TLP_PYTHON_SCOPE PythonInterpreter {
61 
62  PythonInterpreter();
63  ~PythonInterpreter();
64 
65  void holdGIL();
66  void releaseGIL();
67 
68  void setDefaultConsoleWidget(QAbstractScrollArea *consoleWidget);
69  void setConsoleWidget(QAbstractScrollArea *consoleWidget);
70 
71  static PythonInterpreter _instance;
72 
73  bool _wasInit;
74  bool _runningScript;
75  QSet<QString> _currentImportPaths;
76  QAbstractScrollArea *_defaultConsoleWidget;
77  QString _pythonVersion;
78 
79 public :
80 
81  static const QString pythonPluginsPath;
82  static const QString pythonPluginsPathHome;
83  static const char pythonReservedCharacters[];
84  static const char *pythonKeywords[];
85 
86  static PythonInterpreter *getInstance();
87 
88  bool interpreterInit() ;
89 
90  bool loadTulipPythonPlugin(const QString &pluginPath);
91 
92  void loadTulipPythonPluginsFromDir(const QString &pluginsPath);
93 
94  void loadTulipPythonPluginsFromDefaultDirs();
95 
96  bool importModule(const QString &moduleName);
97 
98  bool registerNewModuleFromString(const QString &moduleName, const QString &moduleSrcCode);
99 
100  bool runString(const QString &pyhtonCode, const QString &scriptFilePath="");
101 
102  bool runGraphScript(const QString &module, const QString &function, tlp::Graph *graph, const QString &scriptFilePath="");
103 
104  template<typename T>
105  bool evalSingleStatementAndGetValue(const QString &pythonStatement, T &value);
106 
107  template<typename PARAM_TYPE>
108  bool callFunctionOneParam(const QString &module, const QString &function, const PARAM_TYPE &parameter);
109 
110  template<typename PARAM1_TYPE, typename PARAM2_TYPE>
111  bool callFunctionTwoParams(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2);
112 
113  template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename PARAM3_TYPE>
114  bool callFunctionThreeParams(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2,
115  const PARAM3_TYPE &parameter3);
116 
117  template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename PARAM3_TYPE, typename PARAM4_TYPE>
118  bool callFunctionFourParams(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2,
119  const PARAM3_TYPE &parameter3, const PARAM4_TYPE &parameter4);
120 
121  bool callFunction(const QString &module, const QString &function, const tlp::DataSet &parameters);
122 
123  template<typename PARAM_TYPE, typename RETURN_TYPE>
124  bool callFunctionOneParamAndGetReturnValue(const QString &module, const QString &function, const PARAM_TYPE &parameter, RETURN_TYPE &returnValue);
125 
126  template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename RETURN_TYPE>
127  bool callFunctionTwoParamsAndGetReturnValue(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2,
128  RETURN_TYPE &returnValue);
129 
130  template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename PARAM3_TYPE, typename RETURN_TYPE>
131  bool callFunctionThreeParamsAndGetReturnValue(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2,
132  const PARAM3_TYPE &parameter3, RETURN_TYPE &returnValue);
133 
134  template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename PARAM3_TYPE, typename PARAM4_TYPE, typename RETURN_TYPE>
135  bool callFunctionFourParamsAndGetReturnValue(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2,
136  const PARAM3_TYPE &parameter3, const PARAM4_TYPE &parameter4, RETURN_TYPE &returnValue);
137 
138  template<typename RETURN_TYPE>
139  bool callFunctionAndGetReturnValue(const QString &module, const QString &function, const tlp::DataSet &parameters, RETURN_TYPE &returnValue);
140 
141  bool functionExists(const QString &moduleName, const QString &functionName);
142 
143  void addModuleSearchPath(const QString &path, const bool beforeOtherPaths = false);
144 
145  void deleteModule(const QString &moduleName);
146 
147  bool reloadModule(const QString &moduleName);
148 
149  void stopCurrentScript();
150 
151  void pauseCurrentScript(const bool pause=true);
152 
153  bool isScriptPaused() const;
154 
155  void setProcessQtEventsDuringScriptExecution(bool processQtEvents);
156 
157  bool isRunningScript() const {
158  return _runningScript;
159  }
160 
161  QString getPythonVersionStr() const {
162  return _pythonVersion;
163  }
164 
165  double getPythonVersion() const;
166 
167  QString getPythonShellBanner();
168 
169  void setDefaultSIGINTHandler();
170 
171  QVector<QString> getImportedModulesList();
172 
173  QVector<QString> getBaseTypesForType(const QString &type);
174 
175  QVector<QString> getGlobalDictEntries(const QString &prefixFilter = "");
176 
177  QVector<QString> getObjectDictEntries(const QString &objectName, const QString &prefixFilter = "");
178 
179  QString getVariableType(const QString &varName);
180 
181  void setDefaultConsoleWidget(QPlainTextEdit *consoleWidget);
182  void setDefaultConsoleWidget(QTextBrowser *consoleWidget);
183 
184  void setConsoleWidget(QPlainTextEdit *consoleWidget);
185  void setConsoleWidget(QTextBrowser *consoleWidget);
186 
187  void resetConsoleWidget();
188 
189  void initConsoleOutput();
190 
191  void loadTulipPythonPluginsFromDir();
192 
193  QString getStandardOutput() const;
194 
195  QString getStandardErrorOutput() const;
196 
197  void clearOutputBuffers();
198 
199  void setOutputEnabled(const bool enableOutput);
200 
201  bool outputEnabled() const;
202 
203  void sendOutputToConsole(const QString &output, bool stdErr);
204 
205  QString readLineFromConsole();
206 
207  PyObject* callPythonFunction(const QString &module, const QString &function, const tlp::DataSet &parameters);
208 
209  PyObject* evalPythonStatement(const QString &pythonStatement);
210 
211 };
212 
213 #include "PythonInterpreter.cxx"
214 
215 }
216 
217 #endif /* PYTHONINTERPRETER_H_ */