]> SALOME platform Git repositories - modules/shaper.git/blob - src/Config/Config_ModuleReader.cpp
Salome HOME
The ability to register python plugins added
[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 #include <Python.h>
18
19 //Necessary for cerr
20 #include <iostream>
21
22 #ifdef WIN32
23 #include <windows.h>
24 #else
25 #include <dlfcn.h>
26 #endif
27
28 std::map<std::string, Config_ModuleReader::PluginType> Config_ModuleReader::myPluginTypes;
29
30 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
31     : Config_XMLReader(PLUGIN_FILE),
32       myEventGenerated(theEventGenerated)
33 {
34 }
35
36 Config_ModuleReader::~Config_ModuleReader()
37 {
38 }
39
40 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
41 {
42   return myFeaturesInFiles;
43 }
44
45 /*
46  * Get module name from plugins.xml
47  * (property "module")
48  */
49 std::string Config_ModuleReader::getModuleName()
50 {
51   xmlNodePtr aRoot = findRoot();
52   return getProperty(aRoot, PLUGINS_MODULE);
53 }
54
55 /*
56  *
57  */
58 void Config_ModuleReader::processNode(xmlNodePtr theNode)
59 {
60   if (isNode(theNode, NODE_PLUGIN, NULL)) {
61     std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
62     std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
63     std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
64     std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
65
66     std::list<std::string> aFeatures = importPlugin(aPluginName, aPluginConf);
67     std::list<std::string>::iterator it = aFeatures.begin();
68     for (; it != aFeatures.end(); it++) {
69       myFeaturesInFiles[*it] = aPluginConf;
70     }
71   }
72 }
73
74 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
75 {
76   return isNode(theNode, NODE_PLUGINS, NULL);
77 }
78
79 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
80                                                          const std::string& thePluginXmlConf)
81 {
82   if (thePluginXmlConf.empty()) {  //probably a third party library
83     loadLibrary(thePluginLibrary);
84     return std::list<std::string>();
85   }
86
87   Config_FeatureReader aReader = Config_FeatureReader(thePluginXmlConf,
88                                                       thePluginLibrary,
89                                                       myEventGenerated);
90   aReader.readAll();
91   return aReader.features();
92 }
93
94 std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
95                                            const std::string& aPluginScript,
96                                            const std::string& aPluginConf)
97 {
98   PluginType aType = PluginType::Binary;
99   std::string aPluginName;
100   if (!aPluginLibrary.empty()) {
101     aPluginName = aPluginLibrary;
102     if (aPluginConf.empty()) {
103       aType = PluginType::Intrenal;
104     }
105   } else if (!aPluginScript.empty()) {
106     aPluginName = aPluginScript;
107     aType = PluginType::Python;
108   }
109   if(!aPluginName.empty()) {
110     myPluginTypes[aPluginName] = aType;
111
112   }
113   return aPluginName;
114 }
115
116 void Config_ModuleReader::loadPlugin(const std::string thePluginName)
117 {
118   PluginType aType = PluginType::Binary;
119   if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
120     aType = myPluginTypes.at(thePluginName);
121   }
122   switch (aType) {
123     case PluginType::Python:
124       loadScript(thePluginName);
125       break;
126     case PluginType::Binary:
127     case PluginType::Intrenal:
128     default:
129       loadLibrary(thePluginName);
130       break;
131   }
132 }
133
134 void Config_ModuleReader::loadScript(const std::string theFileName)
135 {
136   std::string aPythonFile = theFileName + ".py";
137   PyGILState_STATE gstate;
138
139   /* aquire python thread */
140   gstate = PyGILState_Ensure();
141
142   PyObject* module = PyImport_ImportModule(aPythonFile.c_str());
143
144   /* release python thread */
145   PyGILState_Release(gstate);
146 }
147
148 void Config_ModuleReader::loadLibrary(const std::string theLibName)
149 {
150   std::string aFileName = library(theLibName);
151   if (aFileName.empty())
152     return;
153
154 #ifdef WIN32
155   HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
156 #else
157   void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
158 #endif
159   if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
160     std::string anErrorMsg = "Failed to load " + aFileName;
161     #ifndef WIN32
162     anErrorMsg += ": " + std::string(dlerror());
163     #endif
164     Events_Error::send(anErrorMsg);
165   }
166 }
167