+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
/*
* Config_ModuleReader.cpp
*
//Necessary for cerr
#include <iostream>
+#include <algorithm>
#ifdef WIN32
#include <windows.h>
+#pragma warning(disable : 4996) // for getenv
#else
#include <dlfcn.h>
#endif
std::map<std::string, Config_ModuleReader::PluginType> Config_ModuleReader::myPluginTypes;
+std::set<std::string> Config_ModuleReader::myDependencyModules;
Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
: Config_XMLReader(PLUGIN_FILE),
return myFeaturesInFiles;
}
-/*
+/*!
* Get module name from plugins.xml
* (property "module")
*/
return getProperty(aRoot, PLUGINS_MODULE);
}
-/*
- *
- */
void Config_ModuleReader::processNode(xmlNodePtr theNode)
{
if (isNode(theNode, NODE_PLUGIN, NULL)) {
+ if (!hasRequiredModules(theNode))
+ return;
std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
return isNode(theNode, NODE_PLUGINS, NULL);
}
+bool Config_ModuleReader::hasRequiredModules(xmlNodePtr theNode) const
+{
+ std::string aRequiredModule = normalize(getProperty(theNode, PLUGIN_DEPENDENCY));
+ if(aRequiredModule.empty())
+ return true;
+ std::set<std::string>::iterator it = myDependencyModules.begin();
+ for ( ; it != myDependencyModules.end(); it++ ) {
+ if (*it == aRequiredModule) return true;
+ }
+ return false;
+}
+
std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
const std::string& thePluginXmlConf)
{
const std::string& aPluginScript,
const std::string& aPluginConf)
{
- PluginType aType = PluginType::Binary;
+ PluginType aType = Config_ModuleReader::Binary;
std::string aPluginName;
if (!aPluginLibrary.empty()) {
aPluginName = aPluginLibrary;
if (aPluginConf.empty()) {
- aType = PluginType::Intrenal;
+ aType = Config_ModuleReader::Intrenal;
}
} else if (!aPluginScript.empty()) {
aPluginName = aPluginScript;
- aType = PluginType::Python;
+ aType = Config_ModuleReader::Python;
}
if(!aPluginName.empty()) {
myPluginTypes[aPluginName] = aType;
-
}
+ addDependencyModule(aPluginName);
return aPluginName;
}
-void Config_ModuleReader::loadPlugin(const std::string thePluginName)
+void Config_ModuleReader::loadPlugin(const std::string& thePluginName)
{
- PluginType aType = PluginType::Binary;
+ PluginType aType = Config_ModuleReader::Binary;
if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
aType = myPluginTypes.at(thePluginName);
}
switch (aType) {
- case PluginType::Python:
+ case Config_ModuleReader::Python:
loadScript(thePluginName);
break;
- case PluginType::Binary:
- case PluginType::Intrenal:
+ case Config_ModuleReader::Binary:
+ case Config_ModuleReader::Intrenal:
default:
loadLibrary(thePluginName);
break;
}
}
-void Config_ModuleReader::loadScript(const std::string theFileName)
+void Config_ModuleReader::loadScript(const std::string& theFileName)
{
- std::string aPythonFile = theFileName + ".py";
/* aquire python thread */
PyGILState_STATE gstate = PyGILState_Ensure();
- PyObject* module = PyImport_ImportModule(aPythonFile.c_str());
+
+ 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)
+void Config_ModuleReader::loadLibrary(const std::string& theLibName)
{
std::string aFileName = library(theLibName);
if (aFileName.empty())
}
}
+void Config_ModuleReader::addDependencyModule(const std::string& theModuleName)
+{
+ myDependencyModules.insert(normalize(theModuleName));
+}
+