+std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
+ const std::string& aPluginScript,
+ const std::string& aPluginConf)
+{
+ PluginType aType = Config_ModuleReader::Binary;
+ std::string aPluginName;
+ if (!aPluginLibrary.empty()) {
+ aPluginName = aPluginLibrary;
+ if (aPluginConf.empty()) {
+ aType = Config_ModuleReader::Intrenal;
+ }
+ } else if (!aPluginScript.empty()) {
+ aPluginName = aPluginScript;
+ aType = Config_ModuleReader::Python;
+ }
+ if(!aPluginName.empty()) {
+ myPluginTypes[aPluginName] = aType;
+ }
+ addDependencyModule(aPluginName);
+ return aPluginName;
+}
+
+void Config_ModuleReader::loadPlugin(const std::string& thePluginName)
+{
+ PluginType aType = Config_ModuleReader::Binary;
+ if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
+ aType = myPluginTypes.at(thePluginName);
+ }
+ switch (aType) {
+ case Config_ModuleReader::Python:
+ loadScript(thePluginName);
+ break;
+ case Config_ModuleReader::Binary:
+ case Config_ModuleReader::Intrenal:
+ default:
+ loadLibrary(thePluginName);
+ break;
+ }
+}
+
+void Config_ModuleReader::loadScript(const std::string& theFileName)
+{
+ /* aquire python thread */
+ PyGILState_STATE gstate = PyGILState_Ensure();
+
+ PyObject* module = PyImport_ImportModule(theFileName.c_str());
+ if (!module) {
+ std::string anErrorMsg = "An error occured while importing " + theFileName;
+ //Get detailed error message:
+ if (PyErr_Occurred()) {
+ PyObject *pstr, *ptype, *pvalue, *ptraceback;
+ PyErr_Fetch(&ptype, &pvalue, &ptraceback);
+ PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
+ pstr = PyObject_Str(pvalue);
+ std::string aPyError = std::string(PyString_AsString(pstr));
+ if (!aPyError.empty()) {
+ anErrorMsg += ":\n" + aPyError;
+ }
+ Py_XDECREF(pstr);
+ Py_XDECREF(ptype);
+ Py_XDECREF(pvalue);
+ Py_XDECREF(ptraceback);
+ }
+ Events_Error::send(anErrorMsg);
+ }
+
+ /* release python thread */
+ PyGILState_Release(gstate);
+}
+
+void Config_ModuleReader::loadLibrary(const std::string& theLibName)
+{
+ std::string aFileName = library(theLibName);
+ if (aFileName.empty())
+ return;
+
+#ifdef WIN32
+ HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
+#else
+ void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
+#endif
+ if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
+ std::string anErrorMsg = "Failed to load " + aFileName;
+ #ifndef WIN32
+ anErrorMsg += ": " + std::string(dlerror());
+ #endif
+ Events_Error::send(anErrorMsg);
+ }
+}
+
+void Config_ModuleReader::addDependencyModule(const std::string& theModuleName)
+{
+ myDependencyModules.insert(normalize(theModuleName));
+}
+