X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FConfig%2FConfig_ModuleReader.cpp;h=526b9965f51aebc1b97b482991fa5d2a1fa2da66;hb=c6a92152ecb67e86489d0a17c8df5fa2e5bcf84e;hp=83b16f27d5afe166f58d15d66e602cb4a6b8a494;hpb=c2c57385fd92d41daeef4b987269545c5e6a579f;p=modules%2Fshaper.git diff --git a/src/Config/Config_ModuleReader.cpp b/src/Config/Config_ModuleReader.cpp index 83b16f27d..526b9965f 100644 --- a/src/Config/Config_ModuleReader.cpp +++ b/src/Config/Config_ModuleReader.cpp @@ -1,3 +1,5 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + /* * Config_ModuleReader.cpp * @@ -19,9 +21,11 @@ //Necessary for cerr #include +#include #ifdef WIN32 #include +#pragma warning(disable : 4996) // for getenv #else #include #endif @@ -32,6 +36,13 @@ Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated) : Config_XMLReader(PLUGIN_FILE), myEventGenerated(theEventGenerated) { + myHaveSalome = false; + char* anEnv = getenv("SALOME_ROOT_DIR"); + std::string value = normalize(anEnv); + if (!value.empty()) { + myHaveSalome = true; + } + } Config_ModuleReader::~Config_ModuleReader() @@ -59,6 +70,9 @@ std::string Config_ModuleReader::getModuleName() void Config_ModuleReader::processNode(xmlNodePtr theNode) { if (isNode(theNode, NODE_PLUGIN, NULL)) { + bool isAvailable = isAvaliableOnThisPlatform(getProperty(theNode, PLUGIN_PLATFORM)); + if (!isAvailable) + return; std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG); std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY); std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT); @@ -96,48 +110,91 @@ std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary, const std::string& aPluginScript, const std::string& aPluginConf) { - PluginType aType = PluginType::Binary; + PluginType aType = Config_ModuleReader::Binary; std::string aPluginName; if (!aPluginLibrary.empty()) { aPluginName = aPluginLibrary; if (aPluginConf.empty()) { - aType = PluginType::Intrenal; + aType = Config_ModuleReader::Intrenal; } } else if (!aPluginScript.empty()) { aPluginName = aPluginScript; - aType = PluginType::Python; + aType = Config_ModuleReader::Python; } if(!aPluginName.empty()) { myPluginTypes[aPluginName] = aType; - } + return aPluginName; } void Config_ModuleReader::loadPlugin(const std::string thePluginName) { - PluginType aType = PluginType::Binary; + PluginType aType = Config_ModuleReader::Binary; if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) { aType = myPluginTypes.at(thePluginName); } switch (aType) { - case PluginType::Python: + case Config_ModuleReader::Python: loadScript(thePluginName); break; - case PluginType::Binary: - case PluginType::Intrenal: + case Config_ModuleReader::Binary: + case Config_ModuleReader::Intrenal: default: loadLibrary(thePluginName); break; } } +bool Config_ModuleReader::isAvaliableOnThisPlatform(const std::string& thePluginPlatform) +{ + bool result = true; + PluginPlatform aPlatform = All; + std::string aPlatformName = normalize(thePluginPlatform) ; + if (aPlatformName == PLUGIN_PLATFORM_SALOME) { + aPlatform = Salome; + } else if (aPlatformName == PLUGIN_PLATFORM_NEWGEOM) { + aPlatform = OpenParts; + } else if (!thePluginPlatform.empty()) { + Events_Error::send("Unknown platform: " + thePluginPlatform); + } + if (aPlatform == All) { + result = true; + } else if (myHaveSalome) { + result = aPlatform == Salome; + } else { + result = aPlatform == OpenParts; + } + return result; + +} + 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 importing " + theFileName; + //Get detailed error message: + if (PyErr_Occurred()) { + PyObject *pstr, *ptype, *pvalue, *ptraceback; + PyErr_Fetch(&ptype, &pvalue, &ptraceback); + 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); + } + Events_Error::send(anErrorMsg); + } + /* release python thread */ PyGILState_Release(gstate); }