Commit a9350f69 authored by Jan Möbius's avatar Jan Möbius

Automatically import python modules for openflipper plugins

parent 7d032d02
......@@ -10,15 +10,16 @@
or graphical functionality will be loaded. You can see on the command line (when "-c" is given),
which plugins are activated and which are skipped in batch mode.
\section python_scripting_datattypes DataTypes
\subsection python_scripting_matrixtype Matrix4x4 data type
The Matrix4x4T type used in the C++ code is mapped from python. Details can be found here:
\subpage python_scripting_matrix4x4_type
\section python_scripting_datattypes DataTypes
\subsection python_scripting_matrixtype Matrix4x4 data type
The Matrix4x4T type used in the C++ code is mapped from python. Details can be found here:
\subpage python_scripting_matrix4x4_type
\subsection python_scripting_vectortype 3 Dimensional vector type
The Vector type used in the C++ code is mapped from python. Details can be found here:
\subpage python_scripting_vector_type
\subsection python_scripting_vectortype 3 Dimensional vector type
The Vector type used in the C++ code is mapped from python. Details can be found here:
\subpage python_scripting_vector_type
*/
......@@ -39,6 +39,7 @@
* *
\*===========================================================================*/
#define PYTHON_DEBUG true
#include <pybind11/include/pybind11/pybind11.h>
#include <pybind11/include/pybind11/embed.h>
......@@ -54,6 +55,8 @@
#include "PythonTypeConversions.hh"
#include <OpenFlipper/BasePlugin/PythonFunctionsCore.hh>
namespace py = pybind11;
static Core* core_;
......@@ -112,40 +115,90 @@ void PythonInterpreter::initPython() {
std::cerr << "C" << std::endl;
if (!modulesInitialized()) {
std::cerr << "D" << std::endl;
#ifdef PYTHON_DEBUG
std::cerr << " Import __main__" ;
#endif
//dlopen("libpython3.5m.so.1.0", RTLD_LAZY | RTLD_GLOBAL);
py::module main{ py::module::import("__main__") };
// redirect python output
#ifdef PYTHON_DEBUG
std::cerr << " Done" << std::endl;
std::cerr << " Redirect Outputs ...";
#endif
// Redirect python output to integrated logger functions
tyti::pylog::redirect_stderr([this](const char*w) {this->pyError(w); });
tyti::pylog::redirect_stdout([this](const char* w) {this->pyOutput(w); });
std::cerr << "E" << std::endl;
#ifdef PYTHON_DEBUG
std::cerr << " Done" << std::endl;
std::cerr << " Get __dict__ from main namespace ...";
#endif
// add openflipper module
// =========================================================
// Add OpenFlipper Core module to main namespace
// =========================================================
py::object main_namespace = main.attr("__dict__");
std::cerr << "F" << std::endl;
#ifdef PYTHON_DEBUG
std::cerr << " Done" << std::endl;
std::cerr << "Importing OpenFlipper core module ...";
#endif
py::module of_module(py::module::import("openflipper"));
main_namespace["openflipper"] = of_module;
std::cerr << "G" << std::endl;
#ifdef PYTHON_DEBUG
std::cerr << " Done" << std::endl;
#endif
// =========================================================
// Import with python interface to main namespace
// =========================================================
QStringList pythonPlugins = getPythonPlugins();
for ( int i = 0 ; i < pythonPlugins.size() ; ++i ) {
#ifdef PYTHON_DEBUG
std::cerr << "Importing "+ pythonPlugins[i].toStdString() + " module ...";
#endif
py::module om_module(py::module::import(pythonPlugins[i].toStdString().c_str()));
main_namespace[pythonPlugins[i].toStdString().c_str()] = om_module;
#ifdef PYTHON_DEBUG
std::cerr << " Done" << std::endl;
#endif
}
#ifdef PYTHON_DEBUG
std::cerr << "Copy dict ...";
#endif
global_dict_clean_ = PyDict_Copy(PyModule_GetDict(main.ptr()));
std::cerr << "H" << std::endl;
#ifdef PYTHON_DEBUG
std::cerr << " Done" << std::endl;
std::cerr << "Set to validate state ...";
#endif
// set the main module member to a validate state, shows, that all modules are successfully initialized
mainModule_ = std::move(main);
std::cerr << "I" << std::endl;
#ifdef PYTHON_DEBUG
std::cerr << " Done" << std::endl;
std::cerr << "Save thread ...";
#endif
// Do not release the GIL until all modules are initalzed
PyEval_SaveThread();
std::cerr << "J" << std::endl;
#ifdef PYTHON_DEBUG
std::cerr << " Done" << std::endl;
#endif
}
......
......@@ -83,7 +83,7 @@ PythonWidget::PythonWidget(QWidget *parent )
#ifdef PYTHON_ENABLED
pythonInfo->setAlignment(Qt::AlignLeft);
pythonInfo->append("Plugins with Python support (use import to load the corresponding module!):\n");
pythonInfo->append("Plugins with Python support ( The plugin modules will be imported with their name shown below.):\n");
QStringList pythonPlugins = getPythonPlugins();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment