1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * Config_ModuleReader.cpp
6 * Created on: Mar 20, 2014
10 #include <Config_Keywords.h>
11 #include <Config_Common.h>
12 #include <Config_ModuleReader.h>
13 #include <Config_FeatureReader.h>
14 #include <Events_Error.h>
16 #include <libxml/parser.h>
17 #include <libxml/tree.h>
19 // Have to be included before std headers
28 #pragma warning(disable : 4996) // for getenv
33 std::map<std::string, Config_ModuleReader::PluginType> Config_ModuleReader::myPluginTypes;
35 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
36 : Config_XMLReader(PLUGIN_FILE),
37 myEventGenerated(theEventGenerated)
40 char* anEnv = getenv("SALOME_ROOT_DIR");
41 std::string value = normalize(anEnv);
48 Config_ModuleReader::~Config_ModuleReader()
52 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
54 return myFeaturesInFiles;
58 * Get module name from plugins.xml
61 std::string Config_ModuleReader::getModuleName()
63 xmlNodePtr aRoot = findRoot();
64 return getProperty(aRoot, PLUGINS_MODULE);
70 void Config_ModuleReader::processNode(xmlNodePtr theNode)
72 if (isNode(theNode, NODE_PLUGIN, NULL)) {
73 bool isAvailable = isAvaliableOnThisPlatform(getProperty(theNode, PLUGIN_PLATFORM));
76 std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
77 std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
78 std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
79 std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
81 std::list<std::string> aFeatures = importPlugin(aPluginName, aPluginConf);
82 std::list<std::string>::iterator it = aFeatures.begin();
83 for (; it != aFeatures.end(); it++) {
84 myFeaturesInFiles[*it] = aPluginConf;
89 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
91 return isNode(theNode, NODE_PLUGINS, NULL);
94 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
95 const std::string& thePluginXmlConf)
97 if (thePluginXmlConf.empty()) { //probably a third party library
98 loadLibrary(thePluginLibrary);
99 return std::list<std::string>();
102 Config_FeatureReader aReader = Config_FeatureReader(thePluginXmlConf,
106 return aReader.features();
109 std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
110 const std::string& aPluginScript,
111 const std::string& aPluginConf)
113 PluginType aType = Config_ModuleReader::Binary;
114 std::string aPluginName;
115 if (!aPluginLibrary.empty()) {
116 aPluginName = aPluginLibrary;
117 if (aPluginConf.empty()) {
118 aType = Config_ModuleReader::Intrenal;
120 } else if (!aPluginScript.empty()) {
121 aPluginName = aPluginScript;
122 aType = Config_ModuleReader::Python;
124 if(!aPluginName.empty()) {
125 myPluginTypes[aPluginName] = aType;
131 void Config_ModuleReader::loadPlugin(const std::string thePluginName)
133 PluginType aType = Config_ModuleReader::Binary;
134 if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
135 aType = myPluginTypes.at(thePluginName);
138 case Config_ModuleReader::Python:
139 loadScript(thePluginName);
141 case Config_ModuleReader::Binary:
142 case Config_ModuleReader::Intrenal:
144 loadLibrary(thePluginName);
149 bool Config_ModuleReader::isAvaliableOnThisPlatform(const std::string& thePluginPlatform)
152 PluginPlatform aPlatform = All;
153 std::string aPlatformName = normalize(thePluginPlatform) ;
154 if (aPlatformName == PLUGIN_PLATFORM_SALOME) {
156 } else if (aPlatformName == PLUGIN_PLATFORM_NEWGEOM) {
157 aPlatform = OpenParts;
158 } else if (!thePluginPlatform.empty()) {
159 Events_Error::send("Unknown platform: " + thePluginPlatform);
161 if (aPlatform == All) {
163 } else if (myHaveSalome) {
164 result = aPlatform == Salome;
166 result = aPlatform == OpenParts;
172 void Config_ModuleReader::loadScript(const std::string theFileName)
174 /* aquire python thread */
175 PyGILState_STATE gstate = PyGILState_Ensure();
176 PyObject* module = PyImport_ImportModule(theFileName.c_str());
179 std::string anErrorMsg = "An error occured while importing " + theFileName;
180 //Get detailed error message:
181 if (PyErr_Occurred()) {
182 PyObject *ptype, *pvalue, *ptraceback;
183 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
184 std::string aPyError = std::string(PyString_AsString(pvalue));
185 if (!aPyError.empty()) {
186 anErrorMsg += ":\n" + aPyError;
190 Py_XDECREF(ptraceback);
192 Events_Error::send(anErrorMsg);
195 /* release python thread */
196 PyGILState_Release(gstate);
199 void Config_ModuleReader::loadLibrary(const std::string theLibName)
201 std::string aFileName = library(theLibName);
202 if (aFileName.empty())
206 HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
208 void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
210 if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
211 std::string anErrorMsg = "Failed to load " + aFileName;
213 anErrorMsg += ": " + std::string(dlerror());
215 Events_Error::send(anErrorMsg);