def load(self): """ Loads a configuration file. Any environment variables present in the XML file are substituted. """ EDVerbose.DEBUG("EDConfiguration.load: importing %s configuration file" % self.__strXmlFileName) from XSDataCommon import XSConfiguration strXMLConfiguration = EDUtilsFile.readFileAndParseVariables(self.__strXmlFileName) self.__xsConfiguration = XSConfiguration.parseString(strXMLConfiguration) self.__bLoaded = True
def convert(infile, outfile): dico = {"__extend__":[]} xml = XSConfiguration.parseFile(infile) for other in xml.XSImportConfiguration: print type(other.directory) if other.directory not in [None, "None"]: dico["__extend__"].append(os.path.join(other.directory, other.name)) else: dico["__extend__"].append(other.name) xsPluginList = xml.getXSPluginList() if xsPluginList is not None: for pluginItem in xsPluginList.getXSPluginItem(): plugin_conf = {} plugin_name = pluginItem.name paramList = pluginItem.getXSParamList() if paramList: for paramItem in paramList.getXSParamItem(): plugin_conf[paramItem.name] = bestType(paramItem.value) dico[plugin_name] = plugin_conf with open(outfile, "w") as f: f.write(json.dumps(dico, indent=4))
def addConfigurationFile(self, _strFileName, _bReplace=True): """Loads an XML/JSON config file into the dictionary if not already loaded""" strFileName = os.path.abspath(_strFileName) if not os.path.exists(strFileName): self.WARNING("Trying to add configuration file but file %s doesn't exist!" % _strFileName) else: if strFileName in self._dictConfigurationFiles: self.DEBUG("EDConfiguration.addConfigurationFile: File %s already parsed, in cache" % strFileName) else: self.DEBUG("EDConfiguration.addConfigurationFile: Parsing file %s" % strFileName) strConfiguration = EDUtilsFile.readFileAndParseVariables(strFileName) if strFileName.endswith(".xml"): xsConfiguration = XSConfiguration.parseString(strConfiguration) if xsConfiguration is not None : dictConfig = {"__extend__":[]} for other in xsConfiguration.XSImportConfiguration: if other.directory not in [None, "None"]: dictConfig["__extend__"].append(os.path.join(other.directory, other.name)) else: dictConfig["__extend__"].append(other.name) xsPluginList = xsConfiguration.getXSPluginList() if xsPluginList is not None: for pluginItem in xsPluginList.getXSPluginItem(): plugin_conf = {} plugin_name = pluginItem.name paramList = pluginItem.getXSParamList() if paramList: for paramItem in paramList.getXSParamItem(): plugin_conf[paramItem.name] = bestType(paramItem.value) dictConfig[plugin_name] = plugin_conf else: #JSON mode try: dictConfig = json.loads(strConfiguration) except ValueError, error: strError = "in file %s Json was %s" % (strFileName, error) self.error(strError) raise ValueError(strError) # Make sure we are thread safe when manipulating the cache with self.locked(): self._dictConfigurationFiles[strFileName] = dictConfig # First look for configuration imports for importConfiguration in dictConfig["__extend__"]: strImportPath = None if importConfiguration.startswith(os.sep): for ext in ["", ".json", ".xml" ]: if os.path.isfile(importConfiguration + ext): strImportPath = importConfiguration + ext break else: for ext in ["", ".json", ".xml" ]: path = os.path.join(os.path.dirname(strFileName), importConfiguration + ext) if os.path.isfile(path): strImportPath = path break if strImportPath is None: strErr = "FATAL: Configuration file %s was not found." % importConfiguration self.ERROR(strErr) raise RuntimeError(strErr) self.DEBUG("Importing configuration file : %s" % strImportPath) self.addConfigurationFile(strImportPath, _bReplace) #Was True, why? # Make sure we are thread safe when manipulating the cache with self.locked(): # Load configurations into the plugin dictionary for strPluginName in dictConfig: if strPluginName == "__extend__": continue if strPluginName in self._dictPluginConfiguration: if _bReplace: self.DEBUG("EDConfiguration.addConfigurationFile: plugin configuration for %s already exists and will be replaced." % strPluginName) self._dictPluginConfiguration[strPluginName] = dictConfig[strPluginName] else: self.DEBUG("EDConfiguration.addConfigurationFile: plugin configuration for %s already exists and will not be replaced." % strPluginName) else: self.DEBUG("EDConfiguration.addConfigurationFile: adding plugin configuration for %s." % strPluginName) self._dictPluginConfiguration[strPluginName] = dictConfig[strPluginName]
def addConfigurationFile(self, _strFileName, _bReplace=True): """Loads an XML/JSON config file into the dictionary if not already loaded""" strFileName = os.path.abspath(_strFileName) if not os.path.exists(strFileName): self.WARNING( "Trying to add configuration file but file %s doesn't exist!" % _strFileName) else: if strFileName in self._dictConfigurationFiles: self.DEBUG( "EDConfiguration.addConfigurationFile: File %s already parsed, in cache" % strFileName) else: self.DEBUG( "EDConfiguration.addConfigurationFile: Parsing file %s" % strFileName) strConfiguration = EDUtilsFile.readFileAndParseVariables( strFileName) if strFileName.endswith(".xml"): xsConfiguration = XSConfiguration.parseString( strConfiguration) if xsConfiguration is not None: dictConfig = {"__extend__": []} for other in xsConfiguration.XSImportConfiguration: if other.directory not in [None, "None"]: dictConfig["__extend__"].append( os.path.join(other.directory, other.name)) else: dictConfig["__extend__"].append(other.name) xsPluginList = xsConfiguration.getXSPluginList() if xsPluginList is not None: for pluginItem in xsPluginList.getXSPluginItem(): plugin_conf = {} plugin_name = pluginItem.name paramList = pluginItem.getXSParamList() if paramList: for paramItem in paramList.getXSParamItem( ): plugin_conf[paramItem.name] = bestType( paramItem.value) dictConfig[plugin_name] = plugin_conf else: #JSON mode dictConfig = json.loads(strConfiguration) # Make sure we are thread safe when manipulating the cache with self.locked(): self._dictConfigurationFiles[strFileName] = dictConfig # First look for configuration imports for importConfiguration in dictConfig["__extend__"]: if importConfiguration.startswith(os.sep): for ext in ["", ".json", ".xml"]: if os.path.isfile(importConfiguration + ext): strImportPath = importConfiguration + ext break else: for ext in ["", ".json", ".xml"]: path = os.path.join(os.path.dirname(strFileName), importConfiguration + ext) if os.path.isfile(path): strImportPath = path break self.DEBUG("Importing configuration file : %s" % strImportPath) self.addConfigurationFile(strImportPath, _bReplace) #Was True, why? # Make sure we are thread safe when manipulating the cache with self.locked(): # Load configurations into the plugin dictionary for strPluginName in dictConfig: if strPluginName == "__extend__": continue if strPluginName in self._dictPluginConfiguration: if _bReplace: self.DEBUG( "EDConfiguration.addConfigurationFile: plugin configuration for %s already exists and will be replaced." % strPluginName) self._dictPluginConfiguration[ strPluginName] = dictConfig[strPluginName] else: self.DEBUG( "EDConfiguration.addConfigurationFile: plugin configuration for %s already exists and will not be replaced." % strPluginName) else: self.DEBUG( "EDConfiguration.addConfigurationFile: adding plugin configuration for %s." % strPluginName) self._dictPluginConfiguration[ strPluginName] = dictConfig[strPluginName]