Salome HOME
Process validators on the first feature's creation using the Config_ValidatorReader
[modules/shaper.git] / src / Config / Config_ModuleReader.cpp
index 27a03156c95d0f55abd2fad3ff2469acf590b93d..41ceda577b62f7b4bb1eb58cc767cabcde8e86f2 100644 (file)
@@ -1,3 +1,5 @@
+// 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),
@@ -43,7 +48,12 @@ const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles()
   return myFeaturesInFiles;
 }
 
-/*
+const std::set<std::string>& Config_ModuleReader::modulePluginFiles() const
+{
+  return myPluginFiles;
+}
+
+/*!
  * Get module name from plugins.xml
  * (property "module")
  */
@@ -53,13 +63,13 @@ std::string Config_ModuleReader::getModuleName()
   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);
+    if (!aPluginConf.empty()) myPluginFiles.insert(aPluginConf);
     std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
     std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
     std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
@@ -77,6 +87,18 @@ bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
   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)
 {
@@ -109,12 +131,12 @@ std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
   }
   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 = Config_ModuleReader::Binary;
   if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
@@ -132,23 +154,25 @@ void Config_ModuleReader::loadPlugin(const std::string thePluginName)
   }
 }
 
-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 loading " + aPythonFile;
+    std::string anErrorMsg = "An error occured while importing " + theFileName;
     //Get detailed error message:
     if (PyErr_Occurred()) {
-      PyObject *ptype, *pvalue, *ptraceback;
+      PyObject *pstr, *ptype, *pvalue, *ptraceback;
       PyErr_Fetch(&ptype, &pvalue, &ptraceback);
-      std::string aPyError = std::string(PyString_AsString(pvalue));
+      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);
@@ -160,7 +184,7 @@ void Config_ModuleReader::loadScript(const std::string theFileName)
   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())
@@ -176,7 +200,13 @@ void Config_ModuleReader::loadLibrary(const std::string theLibName)
     #ifndef WIN32
     anErrorMsg += ": " + std::string(dlerror());
     #endif
+    std::cerr << anErrorMsg << std::endl;
     Events_Error::send(anErrorMsg);
   }
 }
 
+void Config_ModuleReader::addDependencyModule(const std::string& theModuleName)
+{
+  myDependencyModules.insert(normalize(theModuleName));
+}
+