39 #ifndef PYTHONINTERPRETER_H_
40 #define PYTHONINTERPRETER_H_
44 typedef _object PyObject;
47 #include <tulip/PythonCppTypesConverter.h>
48 #include <tulip/Graph.h>
54 class QAbstractScrollArea;
60 class TLP_PYTHON_SCOPE PythonInterpreter {
68 void setDefaultConsoleWidget(QAbstractScrollArea *consoleWidget);
69 void setConsoleWidget(QAbstractScrollArea *consoleWidget);
71 static PythonInterpreter _instance;
75 QSet<QString> _currentImportPaths;
76 QAbstractScrollArea *_defaultConsoleWidget;
77 QString _pythonVersion;
81 static const QString pythonPluginsPath;
82 static const QString pythonPluginsPathHome;
83 static const char pythonReservedCharacters[];
84 static const char *pythonKeywords[];
86 static PythonInterpreter *getInstance();
88 bool interpreterInit() ;
90 bool loadTulipPythonPlugin(
const QString &pluginPath);
92 void loadTulipPythonPluginsFromDir(
const QString &pluginsPath);
94 void loadTulipPythonPluginsFromDefaultDirs();
96 bool importModule(
const QString &moduleName);
98 bool registerNewModuleFromString(
const QString &moduleName,
const QString &moduleSrcCode);
100 bool runString(
const QString &pyhtonCode,
const QString &scriptFilePath=
"");
102 bool runGraphScript(
const QString &module,
const QString &
function,
tlp::Graph *graph,
const QString &scriptFilePath=
"");
105 bool evalSingleStatementAndGetValue(
const QString &pythonStatement, T &value);
107 template<
typename PARAM_TYPE>
108 bool callFunctionOneParam(
const QString &module,
const QString &
function,
const PARAM_TYPE ¶meter);
110 template<
typename PARAM1_TYPE,
typename PARAM2_TYPE>
111 bool callFunctionTwoParams(
const QString &module,
const QString &
function,
const PARAM1_TYPE ¶meter1,
const PARAM2_TYPE ¶meter2);
113 template<
typename PARAM1_TYPE,
typename PARAM2_TYPE,
typename PARAM3_TYPE>
114 bool callFunctionThreeParams(
const QString &module,
const QString &
function,
const PARAM1_TYPE ¶meter1,
const PARAM2_TYPE ¶meter2,
115 const PARAM3_TYPE ¶meter3);
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 ¶meter1,
const PARAM2_TYPE ¶meter2,
119 const PARAM3_TYPE ¶meter3,
const PARAM4_TYPE ¶meter4);
121 bool callFunction(
const QString &module,
const QString &
function,
const tlp::DataSet ¶meters);
123 template<
typename PARAM_TYPE,
typename RETURN_TYPE>
124 bool callFunctionOneParamAndGetReturnValue(
const QString &module,
const QString &
function,
const PARAM_TYPE ¶meter, RETURN_TYPE &returnValue);
126 template<
typename PARAM1_TYPE,
typename PARAM2_TYPE,
typename RETURN_TYPE>
127 bool callFunctionTwoParamsAndGetReturnValue(
const QString &module,
const QString &
function,
const PARAM1_TYPE ¶meter1,
const PARAM2_TYPE ¶meter2,
128 RETURN_TYPE &returnValue);
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 ¶meter1,
const PARAM2_TYPE ¶meter2,
132 const PARAM3_TYPE ¶meter3, RETURN_TYPE &returnValue);
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 ¶meter1,
const PARAM2_TYPE ¶meter2,
136 const PARAM3_TYPE ¶meter3,
const PARAM4_TYPE ¶meter4, RETURN_TYPE &returnValue);
138 template<
typename RETURN_TYPE>
139 bool callFunctionAndGetReturnValue(
const QString &module,
const QString &
function,
const tlp::DataSet ¶meters, RETURN_TYPE &returnValue);
141 bool functionExists(
const QString &moduleName,
const QString &functionName);
143 void addModuleSearchPath(
const QString &path,
const bool beforeOtherPaths =
false);
145 void deleteModule(
const QString &moduleName);
147 bool reloadModule(
const QString &moduleName);
149 void stopCurrentScript();
151 void pauseCurrentScript(
const bool pause=
true);
153 bool isScriptPaused()
const;
155 void setProcessQtEventsDuringScriptExecution(
bool processQtEvents);
157 bool isRunningScript()
const {
158 return _runningScript;
161 QString getPythonVersionStr()
const {
162 return _pythonVersion;
165 double getPythonVersion()
const;
167 QString getPythonShellBanner();
169 void setDefaultSIGINTHandler();
171 QVector<QString> getImportedModulesList();
173 QVector<QString> getBaseTypesForType(
const QString &type);
175 QVector<QString> getGlobalDictEntries(
const QString &prefixFilter =
"");
177 QVector<QString> getObjectDictEntries(
const QString &objectName,
const QString &prefixFilter =
"");
179 QString getVariableType(
const QString &varName);
181 void setDefaultConsoleWidget(QPlainTextEdit *consoleWidget);
182 void setDefaultConsoleWidget(QTextBrowser *consoleWidget);
184 void setConsoleWidget(QPlainTextEdit *consoleWidget);
185 void setConsoleWidget(QTextBrowser *consoleWidget);
187 void resetConsoleWidget();
189 void initConsoleOutput();
191 void loadTulipPythonPluginsFromDir();
193 QString getStandardOutput()
const;
195 QString getStandardErrorOutput()
const;
197 void clearOutputBuffers();
199 void setOutputEnabled(
const bool enableOutput);
201 bool outputEnabled()
const;
203 void sendOutputToConsole(
const QString &output,
bool stdErr);
205 QString readLineFromConsole();
207 PyObject* callPythonFunction(
const QString &module,
const QString &
function,
const tlp::DataSet ¶meters);
209 PyObject* evalPythonStatement(
const QString &pythonStatement);
213 #include "PythonInterpreter.cxx"