39 #ifndef PYTHONINTERPRETER_H_
40 #define PYTHONINTERPRETER_H_
44 typedef _object PyObject;
47 #include <tulip/PythonCppTypesConverter.h>
48 #include <tulip/Graph.h>
50 #include <QtGui/QDialog>
51 #include <QtGui/QPlainTextEdit>
52 #include <QtCore/QDir>
53 #include <QtCore/QVector>
54 #include <QtCore/QSet>
58 class TLP_PYTHON_SCOPE PythonInterpreter {
68 static PythonInterpreter _instance;
72 QSet<QString> _currentImportPaths;
73 QAbstractScrollArea *_defaultConsoleWidget;
74 QString _pythonVersion;
78 static const QString pythonPluginsPath;
79 static const QString pythonPluginsPathHome;
80 static const char pythonReservedCharacters[];
81 static const char *pythonKeywords[];
83 static PythonInterpreter *getInstance();
85 bool interpreterInit() ;
87 bool loadTulipPythonPlugin(
const QString &pluginPath);
89 void loadTulipPythonPluginsFromDir(
const QString &pluginsPath);
91 void loadTulipPythonPluginsFromDefaultDirs();
93 bool importModule(
const QString &moduleName);
95 bool registerNewModuleFromString(
const QString &moduleName,
const QString &moduleSrcCode);
97 bool runString(
const QString &pyhtonCode,
const QString &scriptFilePath=
"");
99 bool runGraphScript(
const QString &module,
const QString &
function,
tlp::Graph *graph,
const QString &scriptFilePath=
"");
102 bool evalSingleStatementAndGetValue(
const QString &pythonStatement, T &value);
104 template<
typename PARAM_TYPE>
105 bool callFunctionOneParam(
const QString &module,
const QString &
function,
const PARAM_TYPE ¶meter);
107 template<
typename PARAM1_TYPE,
typename PARAM2_TYPE>
108 bool callFunctionTwoParams(
const QString &module,
const QString &
function,
const PARAM1_TYPE ¶meter1,
const PARAM2_TYPE ¶meter2);
110 template<
typename PARAM1_TYPE,
typename PARAM2_TYPE,
typename PARAM3_TYPE>
111 bool callFunctionThreeParams(
const QString &module,
const QString &
function,
const PARAM1_TYPE ¶meter1,
const PARAM2_TYPE ¶meter2,
112 const PARAM3_TYPE ¶meter3);
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 ¶meter1,
const PARAM2_TYPE ¶meter2,
116 const PARAM3_TYPE ¶meter3,
const PARAM4_TYPE ¶meter4);
118 bool callFunction(
const QString &module,
const QString &
function,
const tlp::DataSet ¶meters);
120 template<
typename PARAM_TYPE,
typename RETURN_TYPE>
121 bool callFunctionOneParamAndGetReturnValue(
const QString &module,
const QString &
function,
const PARAM_TYPE ¶meter, RETURN_TYPE &returnValue);
123 template<
typename PARAM1_TYPE,
typename PARAM2_TYPE,
typename RETURN_TYPE>
124 bool callFunctionTwoParamsAndGetReturnValue(
const QString &module,
const QString &
function,
const PARAM1_TYPE ¶meter1,
const PARAM2_TYPE ¶meter2,
125 RETURN_TYPE &returnValue);
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 ¶meter1,
const PARAM2_TYPE ¶meter2,
129 const PARAM3_TYPE ¶meter3, RETURN_TYPE &returnValue);
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 ¶meter1,
const PARAM2_TYPE ¶meter2,
133 const PARAM3_TYPE ¶meter3,
const PARAM4_TYPE ¶meter4, RETURN_TYPE &returnValue);
135 template<
typename RETURN_TYPE>
136 bool callFunctionAndGetReturnValue(
const QString &module,
const QString &
function,
const tlp::DataSet ¶meters, RETURN_TYPE &returnValue);
138 bool functionExists(
const QString &moduleName,
const QString &functionName);
140 void addModuleSearchPath(
const QString &path,
const bool beforeOtherPaths =
false);
142 void deleteModule(
const QString &moduleName);
144 bool reloadModule(
const QString &moduleName);
146 void stopCurrentScript();
148 void pauseCurrentScript(
const bool pause=
true);
150 bool isScriptPaused()
const;
152 void setProcessQtEventsDuringScriptExecution(
bool processQtEvents);
154 bool isRunningScript()
const {
155 return _runningScript;
158 QString getPythonVersionStr()
const {
159 return _pythonVersion;
162 double getPythonVersion()
const;
164 QString getPythonShellBanner();
166 void setDefaultSIGINTHandler();
168 QVector<QString> getImportedModulesList();
170 QVector<QString> getBaseTypesForType(
const QString &type);
172 QVector<QString> getGlobalDictEntries(
const QString &prefixFilter =
"");
174 QVector<QString> getObjectDictEntries(
const QString &objectName,
const QString &prefixFilter =
"");
176 QString getVariableType(
const QString &varName);
178 void setDefaultConsoleWidget(QAbstractScrollArea *consoleWidget);
180 void setConsoleWidget(QAbstractScrollArea *consoleWidget);
182 void resetConsoleWidget();
184 void initConsoleOutput();
186 void loadTulipPythonPluginsFromDir();
188 QString getStandardOutput()
const;
190 QString getStandardErrorOutput()
const;
192 void clearOutputBuffers();
194 void setOutputEnabled(
const bool enableOutput);
196 bool outputEnabled()
const;
198 void sendOutputToConsole(
const QString &output,
bool stdErr);
202 PyObject* callPythonFunction(
const QString &module,
const QString &
function,
const tlp::DataSet ¶meters);
203 PyObject* evalPythonStatement(
const QString &pythonStatement);
207 #include "PythonInterpreter.cxx"