def load_config(self, name, macros, is_component): """Loads the configuration from the specified folder. Args: name (string): The name of the configuration macros (dict): The BlockServer macros is_component (bool): Is it a component? """ configuration = Configuration(macros) if is_component: path = os.path.abspath(FILEPATH_MANAGER.get_component_path(name)) else: path = os.path.abspath(FILEPATH_MANAGER.get_config_path(name)) if not os.path.isdir(path): raise IOError("Configuration could not be found: " + name) # Create empty containers blocks = OrderedDict() groups = OrderedDict() components = OrderedDict() iocs = OrderedDict() # Make sure NONE group exists groups[GRP_NONE.lower()] = Group(GRP_NONE) config_files_missing = list() # Open the block file first blocks_path = os.path.join(path, FILENAME_BLOCKS) if os.path.isfile(blocks_path): root = ElementTree.parse(blocks_path).getroot() # Check against the schema - raises if incorrect self._check_againgst_schema(ElementTree.tostring(root, encoding="utf8"), FILENAME_BLOCKS) ConfigurationXmlConverter.blocks_from_xml(root, blocks, groups) else: config_files_missing.append(FILENAME_BLOCKS) # Import the groups groups_path = os.path.join(path, FILENAME_GROUPS) if os.path.isfile(groups_path): root = ElementTree.parse(groups_path).getroot() # Check against the schema - raises if incorrect self._check_againgst_schema(ElementTree.tostring(root, encoding="utf8"), FILENAME_GROUPS) ConfigurationXmlConverter.groups_from_xml(root, groups, blocks) else: config_files_missing.append(FILENAME_GROUPS) # Import the IOCs iocs_path = os.path.join(path, FILENAME_IOCS) if os.path.isfile(iocs_path): root = ElementTree.parse(iocs_path).getroot() # There was a historic bug where the simlevel was saved as 'None' rather than "none". # Correct that here correct_xml = ElementTree.tostring(root, encoding="utf8").replace('simlevel="None"', 'simlevel="none"') # Check against the schema - raises if incorrect self._check_againgst_schema(correct_xml, FILENAME_IOCS) ConfigurationXmlConverter.ioc_from_xml(root, iocs) else: config_files_missing.append(FILENAME_IOCS) # Import the components component_path = os.path.join(path, FILENAME_COMPONENTS) if os.path.isfile(component_path): root = ElementTree.parse(component_path).getroot() # Check against the schema - raises if incorrect self._check_againgst_schema(ElementTree.tostring(root, encoding="utf8"), FILENAME_COMPONENTS) ConfigurationXmlConverter.components_from_xml(root, components) elif not is_component: # It should be missing for a component config_files_missing.append(FILENAME_COMPONENTS) # Import the metadata meta = MetaData(name) meta_path = os.path.join(path, FILENAME_META) if os.path.isfile(meta_path): root = ElementTree.parse(meta_path).getroot() # Check against the schema - raises if incorrect self._check_againgst_schema(ElementTree.tostring(root, encoding="utf8"), FILENAME_META) ConfigurationXmlConverter.meta_from_xml(root, meta) else: config_files_missing.append(FILENAME_META) if len(config_files_missing) > 0: raise ConfigurationIncompleteException( "Files missing in " + name + " (%s)" % ",".join(list(config_files_missing)) ) # Set properties in the config configuration.blocks = blocks configuration.groups = groups configuration.iocs = iocs configuration.components = components configuration.meta = meta return configuration