ctkAbstractPythonManager.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkAbstractPythonManager_h
  15. #define __ctkAbstractPythonManager_h
  16. // Qt includes
  17. #include <QObject>
  18. #include <QList>
  19. #include <QStringList>
  20. // PythonQt includes
  21. #include <PythonQtPythonInclude.h> // For PyObject
  22. // CTK includes
  23. #include "ctkScriptingPythonCoreExport.h"
  24. class ctkAbstractPythonManagerPrivate;
  25. class PythonQtForeignWrapperFactory;
  26. class PythonQtObjectPtr;
  27. /// \ingroup Scripting_Python_Core
  28. class CTK_SCRIPTING_PYTHON_CORE_EXPORT ctkAbstractPythonManager : public QObject
  29. {
  30. Q_OBJECT
  31. public:
  32. typedef QObject Superclass;
  33. ctkAbstractPythonManager(QObject* _parent=NULL);
  34. virtual ~ctkAbstractPythonManager();
  35. /// Calling this function after mainContext() has been called at least once is a no-op.
  36. /// If not overridden calling this function, the default initialization flags are
  37. /// PythonQt::IgnoreSiteModule and PythonQt::RedirectStdOut.
  38. /// \sa PythonQt::InitFlags
  39. void setInitializationFlags(int flags);
  40. /// \sa setInitializationFlags
  41. int initializationFlags()const;
  42. /// Initialize python context considering the initializationFlags.
  43. /// Return \a True if python has been successfully initialized.
  44. /// \sa setInitializationFlags, mainContext, isPythonInitialized
  45. /// \sa preInitialization, executeInitializationScripts, pythonPreInitialized, pythonInitialized
  46. bool initialize();
  47. /// Return a reference to the python main context.
  48. /// Calling this function implicitly call initialize() if it hasn't been done.
  49. PythonQtObjectPtr mainContext();
  50. void addObjectToPythonMain(const QString& name, QObject* obj);
  51. void addWrapperFactory(PythonQtForeignWrapperFactory* factory);
  52. void registerPythonQtDecorator(QObject* decorator);
  53. void registerClassForPythonQt(const QMetaObject* metaobject);
  54. void registerCPPClassForPythonQt(const char* name);
  55. /// \sa PythonQt::systemExitExceptionHandlerEnabled
  56. bool systemExitExceptionHandlerEnabled()const;
  57. /// \sa PythonQt::setSystemExitExceptionHandlerEnabled
  58. void setSystemExitExceptionHandlerEnabled(bool value);
  59. /// This enum maps to Py_eval_input, Py_file_input and Py_single_input
  60. /// \see http://docs.python.org/c-api/veryhigh.html#Py_eval_input
  61. /// \see http://docs.python.org/c-api/veryhigh.html#Py_file_input
  62. /// \see http://docs.python.org/c-api/veryhigh.html#Py_single_input
  63. enum ExecuteStringMode
  64. {
  65. EvalInput = 0,
  66. FileInput,
  67. SingleInput
  68. };
  69. /// Execute a python of python code (can be multiple lines separated with newline)
  70. /// and return the result as a QVariant.
  71. QVariant executeString(const QString& code, ExecuteStringMode mode = FileInput);
  72. /// Gets the value of the variable looking in the __main__ module.
  73. /// If the variable is not found returns a default initialized QVariant.
  74. QVariant getVariable(const QString& varName);
  75. /// Execute a python script with the given filename.
  76. void executeFile(const QString& filename);
  77. /// Set function that is initialized after preInitialization and before executeInitializationScripts
  78. /// \sa preInitialization executeInitializationScripts
  79. void setInitializationFunction(void (*initFunction)());
  80. /// Given a python variable name, lookup its attributes and return them in a string list.
  81. /// By default the attributes are looked up from \c __main__.
  82. /// If the argument \c appendParenthesis is set to True, "()" will be appended to attributes
  83. /// being Python callable.
  84. QStringList pythonAttributes(const QString& pythonVariableName,
  85. const QString& module = QLatin1String("__main__"),
  86. bool appendParenthesis = false) const;
  87. /// Given a string of the form "<modulename1>[.<modulenameN>...]" containing modules, return the final module as a PyObject*
  88. static PyObject* pythonModule(const QString &module);
  89. /// Given a string of the form "<modulename1>[.<modulenameN>...].correspondingObject, return the final object as a PyObject*
  90. /// \sa pythonModule
  91. static PyObject* pythonObject(const QString& variableNameAndFunction);
  92. /// Returns True if python is initialized
  93. /// \sa pythonInitialized
  94. bool isPythonInitialized()const;
  95. /// Returns True if a python error occured.
  96. /// \sa PythonQt::hadError()
  97. bool pythonErrorOccured()const;
  98. /// Reset error flag
  99. /// \sa PythonQt::clearError()
  100. void resetErrorFlag();
  101. Q_SIGNALS:
  102. /// This signal is emitted after python is pre-initialized. Observers can listen
  103. /// for this signal to handle additional initialization steps.
  104. /// \sa preInitialization
  105. void pythonPreInitialized();
  106. /// This signal is emitted after python is initialized and scripts are executed
  107. /// \sa preInitialization
  108. /// \sa executeScripts
  109. void pythonInitialized();
  110. //! emitted when both custom SystemExit exception handler is enabled and a SystemExit
  111. //! exception is raised.
  112. //! \sa setSystemExitExceptionHandlerEnabled(bool), PythonQt::systemExitExceptionRaised(int)
  113. void systemExitExceptionRaised(int exitCode);
  114. protected Q_SLOTS:
  115. void printStderr(const QString&);
  116. void printStdout(const QString&);
  117. protected:
  118. void initPythonQt(int flags);
  119. virtual QStringList pythonPaths();
  120. /// Overload this function to load Decorator and pythonQt wrapper at initialization time
  121. virtual void preInitialization();
  122. /// Overload this function to execute script at initialization time
  123. virtual void executeInitializationScripts();
  124. protected:
  125. QScopedPointer<ctkAbstractPythonManagerPrivate> d_ptr;
  126. private:
  127. Q_DECLARE_PRIVATE(ctkAbstractPythonManager);
  128. Q_DISABLE_COPY(ctkAbstractPythonManager);
  129. };
  130. #endif