itom 1.2.0
D:/git-itom/sources/itom/Qitom/python/pythonEngine.h
00001 /* ********************************************************************
00002     itom software
00003     URL: http://www.uni-stuttgart.de/ito
00004     Copyright (C) 2013, Institut fuer Technische Optik (ITO),
00005     Universitaet Stuttgart, Germany
00006 
00007     This file is part of itom.
00008   
00009     itom is free software; you can redistribute it and/or modify it
00010     under the terms of the GNU Library General Public Licence as published by
00011     the Free Software Foundation; either version 2 of the Licence, or (at
00012     your option) any later version.
00013 
00014     itom is distributed in the hope that it will be useful, but
00015     WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library
00017     General Public Licence for more details.
00018 
00019     You should have received a copy of the GNU Library General Public License
00020     along with itom. If not, see <http://www.gnu.org/licenses/>.
00021 *********************************************************************** */
00022 
00023 #ifndef PYTHONENGINE_H
00024 #define PYTHONENGINE_H
00025 
00026 /*if you add any include to this file you will DIE an immediate, horrible, painful death*/
00027 
00028 #include <string>
00029 //#ifndef Q_MOC_RUN
00030 //    #define PY_ARRAY_UNIQUE_SYMBOL itom_ARRAY_API
00031 //    #define NO_IMPORT_ARRAY
00032 //#endif
00033 
00034 #ifndef ITOM_NPDATAOBJECT
00035     #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION //see comment in pythonNpDataObject.cpp
00036 #endif
00037 
00038 #ifndef Q_MOC_RUN
00039     //python
00040     // see http://vtk.org/gitweb?p=VTK.git;a=commitdiff;h=7f3f750596a105d48ea84ebfe1b1c4ca03e0bab3
00041     #if (defined _DEBUG) && (!defined linux)
00042         #undef _DEBUG
00043         #if (defined linux) | (defined CMAKE)
00044             #include "Python.h"
00045             #include "node.h"
00046             #include "numpy/arrayobject.h"
00047         #else
00048             #include "Python.h"
00049             #include "node.h"
00050             #include "../Lib/site-packages/numpy/core/include/numpy/arrayobject.h" //for numpy arrays
00051         #endif
00052         #define _DEBUG
00053     #else
00054     #ifdef linux
00055         #include "Python.h"
00056         #include "node.h"
00057         #include "numpy/arrayobject.h"
00058     #else
00059         #include "Python.h"
00060         #include "node.h"
00061         #include "../Lib/site-packages/numpy/core/include/numpy/arrayobject.h" //for numpy arrays
00062     #endif
00063     #endif
00064 #endif // Q_MOC_RUN
00065 
00066 /* includes */
00067 
00068 #include "pythonNpDataObject.h"
00069 #include "pythonItom.h"
00070 
00071 /*
00072 #ifdef linux
00073     #include "frameobject.h"
00074     #include "traceback.h"
00075 #else
00076     #include "include/frameobject.h" //!< for traceback
00077     #include "include/traceback.h"
00078 #endif
00079 */
00080 
00081 #include "../models/breakPointModel.h"
00082 #include "../../common/sharedStructuresQt.h"
00083 #include "../../common/addInInterface.h"
00084 
00085 #include "pythonWorkspace.h"
00086 
00087 #include <qstringlist.h>
00088 #include <qqueue.h>
00089 #include <qset.h>
00090 #include <qpointer.h>
00091 
00092 
00093 /* definition and macros */
00094 
00095 /* global variables (avoid) */
00096 
00097 /* content */
00098 
00099 class QDesktopWidget;
00100 class QTimer;
00101 
00102 namespace ito
00103 {
00104 
00105 class PythonEngine : public QObject
00106 {
00107     Q_OBJECT
00108 
00109 public:
00110     PythonEngine();                                 //constructor
00111     ~PythonEngine();                                //destructor
00112 
00113     Q_INVOKABLE void pythonSetup(ito::RetVal *retValue);               //setup
00114     Q_INVOKABLE ito::RetVal scanAndRunAutostartFolder(QString currentDirAfterScan = QString() );
00115     Q_INVOKABLE ito::RetVal pythonShutdown(ItomSharedSemaphore *aimWait = NULL);            //shutdown
00116 
00117     Q_INVOKABLE ito::RetVal stringEncodingChanged();
00118 
00119     inline ito::BreakPointModel *getBreakPointModel() const { return bpModel; }
00120     inline bool isPythonBusy() const { return pythonState != ito::pyStateIdle; }
00121     inline bool isPythonDebugging() const { return (pythonState == ito::pyStateDebuggingWaitingButBusy || pythonState == ito::pyStateDebugging || pythonState == ito::pyStateDebuggingWaiting); }
00122     inline bool isPythonDebuggingAndWaiting() const { return pythonState == ito::pyStateDebuggingWaiting; }
00123     inline bool execInternalCodeByDebugger() const { return m_executeInternalPythonCodeInDebugMode; }
00124     inline void setExecInternalCodeByDebugger(bool value) { m_executeInternalPythonCodeInDebugMode = value; }
00125 
00126     ito::RetVal checkForPyExceptions();
00127     void printPythonErrorWithoutTraceback();
00128 
00129     void pythonDebugFunction(PyObject *callable, PyObject *argTuple);
00130     void pythonRunFunction(PyObject *callable, PyObject *argTuple);
00131 
00132     inline PyObject *getGlobalDictionary()  { return globalDictionary;  }  
00134     inline bool pySyntaxCheckAvailable() const { return (m_pyModSyntaxCheck != NULL); }
00135 
00136     static const PythonEngine *getInstance();
00137 
00138     QList<int> parseAndSplitCommandInMainComponents(const char *str, QByteArray &encoding) const; //can be directly called from different thread
00139 
00140 protected:
00141     //RetVal syntaxCheck(char* pythonFileName);       // syntaxCheck for file with filename pythonFileName
00142     ito::RetVal runPyFile(const QString &pythonFileName);         // run file pythonFileName
00143     ito::RetVal debugFile(const QString &pythonFileName);         // debug file pythonFileName
00144     ito::RetVal runString(const QString &command);          // run string command
00145     ito::RetVal debugString(const QString &command);        // debug string command
00146     ito::RetVal debugFunction(PyObject *callable, PyObject *argTuple);
00147     ito::RetVal runFunction(PyObject *callable, PyObject *argTuple);
00148 
00149     ito::RetVal modifyTracebackDepth(int NrOfLevelsToPopAtFront = -1, bool showTraceback = true);
00150 
00151 private:
00152     static PythonEngine *getInstanceInternal();
00153 
00154     inline PyObject *getLocalDictionary() { return localDictionary; } 
00156     PyObject *getPyObjectByFullName(bool globalNotLocal, QStringList &fullName);
00157 
00158     void setGlobalDictionary(PyObject* mainDict = NULL);
00159     void setLocalDictionary(PyObject* localDict);
00160 
00161     void emitPythonDictionary(bool emitGlobal, bool emitLocal, PyObject* globalDict, PyObject* localDict);
00162 
00163     ito::RetVal pickleDictionary(PyObject *dict, QString filename);
00164     ito::RetVal unpickleDictionary(PyObject *destinationDict, QString filename, bool overwrite);
00165     ito::RetVal saveDictAsMatlab(PyObject *dict, QString filename);
00166     ito::RetVal loadMatlabToDict(PyObject *destinationDict, QString filename);
00167 
00168     //methods for maintaining python functionality
00169     ito::RetVal addMethodToModule(PyMethodDef* def);
00170     ito::RetVal delMethodFromModule(const char* ml_name);
00171     ito::RetVal pythonAddBuiltinMethods();
00172 
00173     //methods for debugging
00174     void enqueueDbgCmd(ito::tPythonDbgCmd dbgCmd);
00175     ito::tPythonDbgCmd dequeueDbgCmd();
00176     bool DbgCommandsAvailable();
00177     void clearDbgCmdLoop();
00178 
00179     ito::RetVal pythonStateTransition(tPythonTransitions transition);
00180 
00181     //methods for breakpoint
00182     ito::RetVal pythonAddBreakpoint(const QString &filename, const int lineno, const bool enabled, const bool temporary, const QString &condition, const int ignoreCount, int &pyBpNumber);
00183     ito::RetVal pythonEditBreakpoint(const int pyBpNumber, const QString &filename, const int lineno, const bool enabled, const bool temporary, const QString &condition, const int ignoreCount);
00184     ito::RetVal pythonDeleteBreakpoint(const int pyBpNumber);
00185 
00186     //member variables
00187     bool started;
00188     QString m_itomMemberClasses;
00189 
00190     //PyGILState_STATE threadState;
00191 
00192     QMutex dbgCmdMutex;
00193     QMutex pythonStateChangeMutex;
00194     QMutex dictChangeMutex;
00195     QDesktopWidget *m_pDesktopWidget;
00196     QQueue<ito::tPythonDbgCmd> debugCommandQueue;
00197     ito::tPythonDbgCmd debugCommand;
00198     
00199     ito::tPythonState pythonState;
00200     
00201     ito::BreakPointModel *bpModel;
00202 
00203     PyObject* mainModule;          
00204     PyObject* mainDictionary;      
00205     PyObject* localDictionary;     
00206     PyObject* globalDictionary;    
00207     PyObject *itomDbgModule;       
00208     PyObject *itomDbgInstance;     
00209     PyObject *itomModule;          
00210     PyObject *itomFunctions;       
00211     PyObject *m_pyModGC;
00212     PyObject *m_pyModSyntaxCheck;
00213     //PyObject *itomReturnException; //!< if this exception is thrown, the execution of the main application is stopped
00214 
00215     PyObject *dictUnicode;
00216 
00217     QSet<ito::PyWorkspaceContainer*> m_mainWorkspaceContainer;
00218     QSet<ito::PyWorkspaceContainer*> m_localWorkspaceContainer;
00219     QHash<QString, QPair<PyObject*,PyObject*> > m_pyFuncWeakRefHashes; 
00220     int m_pyFuncWeakRefHashesAutoInc;
00221     bool m_executeInternalPythonCodeInDebugMode; 
00222     PyMethodDef* PythonAdditionalModuleITOM;
00223 
00224     // decides if itom is automatically included in every source file before itīs handed to the syntax checker
00225     bool m_includeItom;
00226 
00228     static PyMethodDef PyMethodItomDbg[];
00229     static PyModuleDef PyModuleItomDbg;
00230     static PyObject* PyInitItomDbg(void);
00231     static PyObject* PyDbgCommandLoop(PyObject *pSelf, PyObject *pArgs);
00232 
00233     //helper methods
00234     //static PyObject* checkForTimeoutHelper(ItomSharedSemaphore* semaphore, int timeout, PyObject *retValueOk);
00235 
00236     //other static members
00237     static QMutex instatiated;
00238     static QMutex instancePtrProtection;
00239 
00240     static PythonEngine* instance;
00241 
00242     // friend class
00243     friend class PythonDataObject;
00244     friend class ito::PythonItom;
00245 
00246 signals:
00247     void pythonDebugPositionChanged(QString filename, int lineNo);
00248     void pythonStateChanged(tPythonTransitions pyTransition);
00249     void pythonModifyLocalDict(PyObject* localDict, ItomSharedSemaphore* semaphore);
00250     void pythonModifyGlobalDict(PyObject* globalDict, ItomSharedSemaphore* semaphore);
00251     void pythonAddToolbarButton(QString toolbarName, QString buttonName, QString buttonIconFilename, QString pythonCode);
00252     void pythonRemoveToolbarButton(QString toolbarName, QString buttonName);
00253     void pythonAddMenuElement(int typeID, QString key, QString name, QString code, QString icon);
00254     void pythonRemoveMenuElement(QString key);
00255     void pythonCurrentDirChanged();
00256     void updateCallStack(QStringList filenames, IntList lines, QStringList methods);
00257     void deleteCallStack();
00258 
00259     void pythonSetCursor(const Qt::CursorShape cursor);
00260     void pythonResetCursor();
00261 
00262 public slots:
00263     void pythonRunString(QString cmd);
00264     void pythonDebugString(QString cmd);
00265     void pythonExecStringFromCommandLine(QString cmd);
00266     void pythonRunFile(QString filename);
00267     void pythonDebugFile(QString filename);
00268     void pythonRunStringOrFunction(QString cmdOrFctHash);
00269     void pythonDebugStringOrFunction(QString cmdOrFctHash);
00270     void pythonInterruptExecution() const;
00271     void pythonDebugCommand(tPythonDbgCmd cmd);
00272 
00273     // Settings are neccesary for automatic itom inclusion and syntax check
00274     void readSettings();
00275     void propertiesChanged();
00276 
00277     void pythonSyntaxCheck(const QString &code, QPointer<QObject> sender);
00278 
00279     void pythonGenericSlot(PyObject* callable, PyObject *argumentTuple);
00280 
00282     void breakPointAdded(BreakPointItem bp, int row);
00283     void breakPointDeleted(QString filename, int lineNo, int pyBpNumber);
00284     void breakPointChanged(BreakPointItem oldBp, BreakPointItem newBp);
00285     ito::RetVal setupBreakPointDebugConnections();
00286     ito::RetVal shutdownBreakPointDebugConnections();
00287 
00288     bool renameVariable(bool globalNotLocal, QString oldKey, QString newKey, ItomSharedSemaphore *semaphore = NULL);
00289     bool deleteVariable(bool globalNotLocal, QStringList keys, ItomSharedSemaphore *semaphore = NULL);
00290     ito::RetVal pickleVariables(bool globalNotLocal, QString filename, QStringList varNames, ItomSharedSemaphore *semaphore = NULL);
00291     ito::RetVal unpickleVariables(bool globalNotLocal, QString filename, ItomSharedSemaphore *semaphore = NULL);
00292     ito::RetVal saveMatlabVariables(bool globalNotLocal, QString filename, QStringList varNames, ItomSharedSemaphore *semaphore = NULL);
00293     ito::RetVal loadMatlabVariables(bool globalNotLocal, QString filename, ItomSharedSemaphore *semaphore = NULL);
00294     ito::RetVal registerAddInInstance(QString varname, ito::AddInBase *instance, ItomSharedSemaphore *semaphore = NULL);
00295     ito::RetVal getSysModules(QSharedPointer<QStringList> modNames, QSharedPointer<QStringList> modFilenames, QSharedPointer<IntList> modTypes, ItomSharedSemaphore *semaphore = NULL);
00296     ito::RetVal reloadSysModules(QSharedPointer<QStringList> modNames, ItomSharedSemaphore *semaphore = NULL);
00297 
00298     void registerWorkspaceContainer(PyWorkspaceContainer *container, bool registerNotUnregister, bool globalNotLocal);
00299     void workspaceGetChildNode(PyWorkspaceContainer *container, QString fullNameParentItem);
00300     void workspaceGetValueInformation(PyWorkspaceContainer *container, QString fullItemName, QSharedPointer<QString> extendedValue, ItomSharedSemaphore *semaphore = NULL);
00301 
00302     void putParamsToWorkspace(bool globalNotLocal, QStringList names, QVector<SharedParamBasePointer > values, ItomSharedSemaphore *semaphore = NULL);
00303     void getParamsFromWorkspace(bool globalNotLocal, QStringList names, QVector<int> paramBaseTypes, QSharedPointer<SharedParamBasePointerVector > values, ItomSharedSemaphore *semaphore = NULL);
00304 
00305 private slots:
00306 
00307 };
00308 
00309 } //end namespace ito
00310 
00311 
00312 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends