def save_config(self, configuration, is_component): """Saves the current configuration with the specified name. Args: configuration (Configuration): The actual configuration to save is_component (bool): Is it a component? """ if is_component: path = os.path.abspath(FILEPATH_MANAGER.get_component_path(configuration.get_name())) else: path = os.path.abspath(FILEPATH_MANAGER.get_config_path(configuration.get_name())) if not os.path.isdir(path): # Create the directory os.makedirs(path) blocks_xml = ConfigurationXmlConverter.blocks_to_xml(configuration.blocks, configuration.macros) groups_xml = ConfigurationXmlConverter.groups_to_xml(configuration.groups) iocs_xml = ConfigurationXmlConverter.iocs_to_xml(configuration.iocs) meta_xml = ConfigurationXmlConverter.meta_to_xml(configuration.meta) try: components_xml = ConfigurationXmlConverter.components_to_xml(configuration.components) except: # Is a component, so no components components_xml = ConfigurationXmlConverter.components_to_xml(dict()) # Save blocks with open(path + "/" + FILENAME_BLOCKS, "w") as f: f.write(blocks_xml) # Save groups with open(path + "/" + FILENAME_GROUPS, "w") as f: f.write(groups_xml) # Save IOCs with open(path + "/" + FILENAME_IOCS, "w") as f: f.write(iocs_xml) # Save components with open(path + "/" + FILENAME_COMPONENTS, "w") as f: f.write(components_xml) # Save meta with open(path + "/" + FILENAME_META, "w") as f: f.write(meta_xml)
def test_valid_components_xml_matches_schema(self): conf = Configuration(MACROS) conf.components["COMP1"] = "COMP1" conf.components["COMP2"] = "COMP2" xml = ConfigurationXmlConverter.components_to_xml(conf.components) try: ConfigurationSchemaChecker.check_xml_data_matches_schema(os.path.join(self.schema_dir, "components.xsd"), xml) except: self.fail()
def test_components_xml_does_not_match_schema_raises(self): conf = Configuration(MACROS) conf.components["COMP1"] = "COMP1" conf.components["COMP2"] = "COMP2" xml = ConfigurationXmlConverter.components_to_xml(conf.components) # Keep it valid XML but don't match schema xml = xml.replace("<component ", "<domponent ") self.assertRaises(ConfigurationInvalidUnderSchema, ConfigurationSchemaChecker.check_xml_data_matches_schema, os.path.join(self.schema_dir, "meta.xsd"), xml)
def save_config(self, configuration, root_path, config_name): self.blocks_xml = ConfigurationXmlConverter.blocks_to_xml(configuration.blocks, configuration.macros) self.groups_xml = ConfigurationXmlConverter.groups_to_xml(configuration.groups) self.iocs_xml = ConfigurationXmlConverter.iocs_to_xml(configuration.iocs) self.components_xml = ConfigurationXmlConverter.components_to_xml(configuration.components)