Tulip  4.2.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 <QtGui/QDialog>
51 #include <QtGui/QPlainTextEdit>
52 #include <QtCore/QDir>
53 #include <QtCore/QVector>
54 #include <QtCore/QSet>
55 
56 namespace tlp {
57 
58 class TLP_PYTHON_SCOPE PythonInterpreter {
59 
60  PythonInterpreter();
61  ~PythonInterpreter();
62 
63  void holdGIL();
64  void releaseGIL();
65 
66 
67 
68  static PythonInterpreter _instance;
69 
70  bool _wasInit;
71  bool _runningScript;
72  QSet<QString> _currentImportPaths;
73  QAbstractScrollArea *_defaultConsoleWidget;
74  QString _pythonVersion;
75 
76 public :
77 
78  static const QString pythonPluginsPath;
79  static const QString pythonPluginsPathHome;
80  static const char pythonReservedCharacters[];
81  static const char *pythonKeywords[];
82 
83  static PythonInterpreter *getInstance();
84 
85  bool interpreterInit() ;
86 
87  bool loadTulipPythonPlugin(const QString &pluginPath);
88 
89  void loadTulipPythonPluginsFromDir(const QString &pluginsPath);
90 
91  void loadTulipPythonPluginsFromDefaultDirs();
92 
93  bool importModule(const QString &moduleName);
94 
95  bool registerNewModuleFromString(const QString &moduleName, const QString &moduleSrcCode);
96 
97  bool runString(const QString &pyhtonCode, const QString &scriptFilePath="");
98 
99  bool runGraphScript(const QString &module, const QString &function, tlp::Graph *graph, const QString &scriptFilePath="");
100 
101  template<typename T>
102  bool evalSingleStatementAndGetValue(const QString &pythonStatement, T &value);
103 
104  template<typename PARAM_TYPE>
105  bool callFunctionOneParam(const QString &module, const QString &function, const PARAM_TYPE &parameter);
106 
107  template<typename PARAM1_TYPE, typename PARAM2_TYPE>
108  bool callFunctionTwoParams(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2);
109 
110  template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename PARAM3_TYPE>
111  bool callFunctionThreeParams(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2,
112  const PARAM3_TYPE &parameter3);
113 
114  template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename PARAM3_TYPE, typename PARAM4_TYPE>
115  bool callFunctionFourParams(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2,
116  const PARAM3_TYPE &parameter3, const PARAM4_TYPE &parameter4);
117 
118  bool callFunction(const QString &module, const QString &function, const tlp::DataSet &parameters);
119 
120  template<typename PARAM_TYPE, typename RETURN_TYPE>
121  bool callFunctionOneParamAndGetReturnValue(const QString &module, const QString &function, const PARAM_TYPE &parameter, RETURN_TYPE &returnValue);
122 
123  template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename RETURN_TYPE>
124  bool callFunctionTwoParamsAndGetReturnValue(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2,
125  RETURN_TYPE &returnValue);
126 
127  template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename PARAM3_TYPE, typename RETURN_TYPE>
128  bool callFunctionThreeParamsAndGetReturnValue(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2,
129  const PARAM3_TYPE &parameter3, RETURN_TYPE &returnValue);
130 
131  template<typename PARAM1_TYPE, typename PARAM2_TYPE, typename PARAM3_TYPE, typename PARAM4_TYPE, typename RETURN_TYPE>
132  bool callFunctionFourParamsAndGetReturnValue(const QString &module, const QString &function, const PARAM1_TYPE &parameter1, const PARAM2_TYPE &parameter2,
133  const PARAM3_TYPE &parameter3, const PARAM4_TYPE &parameter4, RETURN_TYPE &returnValue);
134 
135  template<typename RETURN_TYPE>
136  bool callFunctionAndGetReturnValue(const QString &module, const QString &function, const tlp::DataSet &parameters, RETURN_TYPE &returnValue);
137 
138  bool functionExists(const QString &moduleName, const QString &functionName);
139 
140  void addModuleSearchPath(const QString &path, const bool beforeOtherPaths = false);
141 
142  void deleteModule(const QString &moduleName);
143 
144  bool reloadModule(const QString &moduleName);
145 
146  void stopCurrentScript();
147 
148  void pauseCurrentScript(const bool pause=true);
149 
150  bool isScriptPaused() const;
151 
152  void setProcessQtEventsDuringScriptExecution(bool processQtEvents);
153 
154  bool isRunningScript() const {
155  return _runningScript;
156  }
157 
158  QString getPythonVersionStr() const {
159  return _pythonVersion;
160  }
161 
162  double getPythonVersion() const;
163 
164  QString getPythonShellBanner();
165 
166  void setDefaultSIGINTHandler();
167 
168  QVector<QString> getImportedModulesList();
169 
170  QVector<QString> getBaseTypesForType(const QString &type);
171 
172  QVector<QString> getGlobalDictEntries(const QString &prefixFilter = "");
173 
174  QVector<QString> getObjectDictEntries(const QString &objectName, const QString &prefixFilter = "");
175 
176  QString getVariableType(const QString &varName);
177 
178  void setDefaultConsoleWidget(QAbstractScrollArea *consoleWidget);
179 
180  void setConsoleWidget(QAbstractScrollArea *consoleWidget);
181 
182  void resetConsoleWidget();
183 
184  void initConsoleOutput();
185 
186  void loadTulipPythonPluginsFromDir();
187 
188  QString getStandardOutput() const;
189 
190  QString getStandardErrorOutput() const;
191 
192  void clearOutputBuffers();
193 
194  void setOutputEnabled(const bool enableOutput);
195 
196  bool outputEnabled() const;
197 
198  void sendOutputToConsole(const QString &output, bool stdErr);
199 
200 protected:
201 
202  PyObject* callPythonFunction(const QString &module, const QString &function, const tlp::DataSet &parameters);
203  PyObject* evalPythonStatement(const QString &pythonStatement);
204 
205 };
206 
207 #include "PythonInterpreter.cxx"
208 
209 }
210 
211 #endif /* PYTHONINTERPRETER_H_ */