Salome HOME
Resolve cast and import errors in Box.py
[modules/shaper.git] / src / Config / Config_ModuleReader.cpp
1 /*
2  * Config_ModuleReader.cpp
3  *
4  *  Created on: Mar 20, 2014
5  *      Author: sbh
6  */
7
8 #include <Config_Keywords.h>
9 #include <Config_Common.h>
10 #include <Config_ModuleReader.h>
11 #include <Config_FeatureReader.h>
12 #include <Events_Error.h>
13
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
16
17 // Have to be included before std headers
18 #include <Python.h>
19
20 //Necessary for cerr
21 #include <iostream>
22
23 #ifdef WIN32
24 #include <windows.h>
25 #else
26 #include <dlfcn.h>
27 #endif
28
29 std::map<std::string, Config_ModuleReader::PluginType> Config_ModuleReader::myPluginTypes;
30
31 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
32     : Config_XMLReader(PLUGIN_FILE),
33       myEventGenerated(theEventGenerated)
34 {
35 }
36
37 Config_ModuleReader::~Config_ModuleReader()
38 {
39 }
40
41 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
42 {
43   return myFeaturesInFiles;
44 }
45
46 /*
47  * Get module name from plugins.xml
48  * (property "module")
49  */
50 std::string Config_ModuleReader::getModuleName()
51 {
52   xmlNodePtr aRoot = findRoot();
53   return getProperty(aRoot, PLUGINS_MODULE);
54 }
55
56 /*
57  *
58  */
59 void Config_ModuleReader::processNode(xmlNodePtr theNode)
60 {
61   if (isNode(theNode, NODE_PLUGIN, NULL)) {
62     std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
63     std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
64     std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
65     std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
66
67     std::list<std::string> aFeatures = importPlugin(aPluginName, aPluginConf);
68     std::list<std::string>::iterator it = aFeatures.begin();
69     for (; it != aFeatures.end(); it++) {
70       myFeaturesInFiles[*it] = aPluginConf;
71     }
72   }
73 }
74
75 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
76 {
77   return isNode(theNode, NODE_PLUGINS, NULL);
78 }
79
80 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
81                                                          const std::string& thePluginXmlConf)
82 {
83   if (thePluginXmlConf.empty()) {  //probably a third party library
84     loadLibrary(thePluginLibrary);
85     return std::list<std::string>();
86   }
87
88   Config_FeatureReader aReader = Config_FeatureReader(thePluginXmlConf,
89                                                       thePluginLibrary,
90                                                       myEventGenerated);
91   aReader.readAll();
92   return aReader.features();
93 }
94
95 std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
96                                            const std::string& aPluginScript,
97                                            const std::string& aPluginConf)
98 {
99   PluginType aType = Config_ModuleReader::Binary;
100   std::string aPluginName;
101   if (!aPluginLibrary.empty()) {
102     aPluginName = aPluginLibrary;
103     if (aPluginConf.empty()) {
104       aType = Config_ModuleReader::Intrenal;
105     }
106   } else if (!aPluginScript.empty()) {
107     aPluginName = aPluginScript;
108     aType = Config_ModuleReader::Python;
109   }
110   if(!aPluginName.empty()) {
111     myPluginTypes[aPluginName] = aType;
112
113   }
114   return aPluginName;
115 }
116
117 void Config_ModuleReader::loadPlugin(const std::string thePluginName)
118 {
119   PluginType aType = Config_ModuleReader::Binary;
120   if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
121     aType = myPluginTypes.at(thePluginName);
122   }
123   switch (aType) {
124     case Config_ModuleReader::Python:
125       loadScript(thePluginName);
126       break;
127     case Config_ModuleReader::Binary:
128     case Config_ModuleReader::Intrenal:
129     default:
130       loadLibrary(thePluginName);
131       break;
132   }
133 }
134
135 void Config_ModuleReader::loadScript(const std::string theFileName)
136 {
137   /* aquire python thread */
138   PyGILState_STATE gstate = PyGILState_Ensure();
139   PyObject* module = PyImport_ImportModule(theFileName.c_str());
140
141   if (!module) {
142     std::string anErrorMsg = "An error occured while importing " + theFileName;
143     //Get detailed error message:
144     if (PyErr_Occurred()) {
145       PyObject *ptype, *pvalue, *ptraceback;
146       PyErr_Fetch(&ptype, &pvalue, &ptraceback);
147       std::string aPyError = std::string(PyString_AsString(pvalue));
148       if (!aPyError.empty()) {
149         anErrorMsg += ":\n" + aPyError;
150       }
151       Py_XDECREF(ptype);
152       Py_XDECREF(pvalue);
153       Py_XDECREF(ptraceback);
154     }
155     Events_Error::send(anErrorMsg);
156   }
157
158   /* release python thread */
159   PyGILState_Release(gstate);
160 }
161
162 void Config_ModuleReader::loadLibrary(const std::string theLibName)
163 {
164   std::string aFileName = library(theLibName);
165   if (aFileName.empty())
166     return;
167
168 #ifdef WIN32
169   HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
170 #else
171   void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
172 #endif
173   if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
174     std::string anErrorMsg = "Failed to load " + aFileName;
175     #ifndef WIN32
176     anErrorMsg += ": " + std::string(dlerror());
177     #endif
178     Events_Error::send(anErrorMsg);
179   }
180 }
181