Salome HOME
Issue #608: Usage of OCCT in interface -- Wrap classes by SWIG
[modules/shaper.git] / src / Config / Config_ModuleReader.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * Config_ModuleReader.cpp
5  *
6  *  Created on: Mar 20, 2014
7  *      Author: sbh
8  */
9
10 #include <pyconfig.h>
11
12 #include <Config_Keywords.h>
13 #include <Config_Common.h>
14 #include <Config_ModuleReader.h>
15 #include <Config_FeatureReader.h>
16 #include <Events_Error.h>
17
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
20
21 // Have to be included before std headers
22 #include <Python.h>
23
24 //Necessary for cerr
25 #include <iostream>
26 #include <algorithm>
27
28 #ifdef WIN32
29 #include <windows.h>
30 #pragma warning(disable : 4996) // for getenv
31 #else
32 #include <dlfcn.h>
33 #endif
34
35 std::map<std::string, Config_ModuleReader::PluginType> Config_ModuleReader::myPluginTypes;
36 std::set<std::string> Config_ModuleReader::myDependencyModules;
37
38 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
39     : Config_XMLReader(PLUGIN_FILE),
40       myEventGenerated(theEventGenerated)
41 {
42 }
43
44 Config_ModuleReader::~Config_ModuleReader()
45 {
46 }
47
48 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
49 {
50   return myFeaturesInFiles;
51 }
52
53 const std::set<std::string>& Config_ModuleReader::modulePluginFiles() const
54 {
55   return myPluginFiles;
56 }
57
58 /*!
59  * Get module name from plugins.xml
60  * (property "module")
61  */
62 std::string Config_ModuleReader::getModuleName()
63 {
64   xmlNodePtr aRoot = findRoot();
65   return getProperty(aRoot, PLUGINS_MODULE);
66 }
67
68 void Config_ModuleReader::processNode(xmlNodePtr theNode)
69 {
70   if (isNode(theNode, NODE_PLUGIN, NULL)) {
71     if (!hasRequiredModules(theNode))
72       return;
73     std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
74     if (!aPluginConf.empty()) myPluginFiles.insert(aPluginConf);
75     std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
76     std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
77     std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
78
79     std::list<std::string> aFeatures = importPlugin(aPluginName, aPluginConf);
80     std::list<std::string>::iterator it = aFeatures.begin();
81     for (; it != aFeatures.end(); it++) {
82       myFeaturesInFiles[*it] = aPluginConf;
83     }
84   }
85 }
86
87 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
88 {
89   return isNode(theNode, NODE_PLUGINS, NULL);
90 }
91
92 bool Config_ModuleReader::hasRequiredModules(xmlNodePtr theNode) const
93 {
94   std::string aRequiredModule = normalize(getProperty(theNode, PLUGIN_DEPENDENCY));
95   if(aRequiredModule.empty())
96     return true;
97   std::set<std::string>::iterator it = myDependencyModules.begin();
98   for ( ; it != myDependencyModules.end(); it++ ) {
99     if (*it == aRequiredModule) return true;
100   }
101   return false;
102 }
103
104 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
105                                                          const std::string& thePluginXmlConf)
106 {
107   if (thePluginXmlConf.empty()) {  //probably a third party library
108     loadLibrary(thePluginLibrary);
109     return std::list<std::string>();
110   }
111
112   Config_FeatureReader aReader = Config_FeatureReader(thePluginXmlConf,
113                                                       thePluginLibrary,
114                                                       myEventGenerated);
115   aReader.readAll();
116   return aReader.features();
117 }
118
119 std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
120                                            const std::string& aPluginScript,
121                                            const std::string& aPluginConf)
122 {
123   PluginType aType = Config_ModuleReader::Binary;
124   std::string aPluginName;
125   if (!aPluginLibrary.empty()) {
126     aPluginName = aPluginLibrary;
127     if (aPluginConf.empty()) {
128       aType = Config_ModuleReader::Intrenal;
129     }
130   } else if (!aPluginScript.empty()) {
131     aPluginName = aPluginScript;
132     aType = Config_ModuleReader::Python;
133   }
134   if(!aPluginName.empty()) {
135     myPluginTypes[aPluginName] = aType;
136   }
137   addDependencyModule(aPluginName);
138   return aPluginName;
139 }
140
141 void Config_ModuleReader::loadPlugin(const std::string& thePluginName)
142 {
143   PluginType aType = Config_ModuleReader::Binary;
144   if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
145     aType = myPluginTypes.at(thePluginName);
146   }
147   switch (aType) {
148     case Config_ModuleReader::Python:
149       loadScript(thePluginName);
150       break;
151     case Config_ModuleReader::Binary:
152     case Config_ModuleReader::Intrenal:
153     default:
154       loadLibrary(thePluginName);
155       break;
156   }
157 }
158
159 void Config_ModuleReader::loadScript(const std::string& theFileName)
160 {
161   /* aquire python thread */
162   PyGILState_STATE gstate = PyGILState_Ensure();
163
164   PyObject* module = PyImport_ImportModule(theFileName.c_str());
165   if (!module) {
166     std::string anErrorMsg = "An error occured while importing " + theFileName;
167     //Get detailed error message:
168     if (PyErr_Occurred()) {
169       PyObject *pstr, *ptype, *pvalue, *ptraceback;
170       PyErr_Fetch(&ptype, &pvalue, &ptraceback);
171       PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
172       pstr = PyObject_Str(pvalue);
173       std::string aPyError = std::string(PyString_AsString(pstr));
174       if (!aPyError.empty()) {
175         anErrorMsg += ":\n" + aPyError;
176       }
177       Py_XDECREF(pstr);
178       Py_XDECREF(ptype);
179       Py_XDECREF(pvalue);
180       Py_XDECREF(ptraceback);
181     }
182     Events_Error::send(anErrorMsg);
183   }
184
185   /* release python thread */
186   PyGILState_Release(gstate);
187 }
188
189 void Config_ModuleReader::loadLibrary(const std::string& theLibName)
190 {
191   std::string aFileName = library(theLibName);
192   if (aFileName.empty())
193     return;
194
195   #ifdef WIN32
196   HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
197   #else
198   void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
199   #endif
200   if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
201     std::string anErrorMsg = "Failed to load " + aFileName;
202     #ifdef WIN32
203     DWORD   dwLastError = ::GetLastError();
204     LPSTR messageBuffer = NULL;
205     size_t size = ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
206                                  FORMAT_MESSAGE_FROM_SYSTEM | 
207                                  FORMAT_MESSAGE_IGNORE_INSERTS,
208                                  NULL, 
209                                  dwLastError, 
210                                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
211                                  (LPSTR)&messageBuffer, 0, NULL);
212     anErrorMsg += ": " +  std::string(messageBuffer, size);
213     #else
214     anErrorMsg += ": " + std::string(dlerror());
215     #endif
216     std::cerr << anErrorMsg << std::endl;
217     Events_Error::send(anErrorMsg);
218   }
219 }
220
221 void Config_ModuleReader::addDependencyModule(const std::string& theModuleName)
222 {
223   myDependencyModules.insert(normalize(theModuleName));
224 }
225