def invalid_read_test(self): parser = create_parser() with self.assertRaises(ConfigurationFileError) as cm: read_config(parser, "nonexistent/path/to/file") self.assertEqual(cm.exception._filename, "nonexistent/path/to/file")
def load_product(self, config_path): """Load information about a product from the given configuration file. :param config_path: a path to a configuration file :raises: ConfigurationError if a product cannot be loaded """ # Set up the parser. parser = create_parser() self._create_section(parser, "Product") self._create_section(parser, "Base Product") # Read the product sections. read_config(parser, config_path) key = self._read_section(parser, "Product") base = self._read_section(parser, "Base Product") # Check the product. if not key.product_name: raise ConfigurationError("The product name is not specified.") if key in self._products: raise ConfigurationError( "The product {} was already loaded.".format(key)) # Check the base product. if not base.product_name: base = None # Add the product. log.info("Found %s at %s.", key, config_path) self._products[key] = ProductData(base, config_path)
def _compare_profile_files(self, file_name, other_file_name, ignored_sections=()): parser = create_parser() read_config(parser, os.path.join(PROFILE_DIR, file_name)) other_parser = create_parser() read_config(other_parser, os.path.join(PROFILE_DIR, other_file_name)) # Ignore the specified and profile-related sections. ignored_sections += ("Profile", "Profile Detection") sections = set(parser.sections()).difference(ignored_sections) other_sections = set( other_parser.sections()).difference(ignored_sections) # Otherwise, the defined sections should be the same. assert sections == other_sections for section in sections: # The defined options should be the same. assert parser.options(section) == other_parser.options(section) for key in parser.options(section): # The values of the options should be the same. assert parser.get(section, key) == other_parser.get(section, key)
def _compare_product_files(self, file_name, other_file_name, ignored_sections=()): parser = create_parser() read_config(parser, os.path.join(PRODUCT_DIR, file_name)) other_parser = create_parser() read_config(other_parser, os.path.join(PRODUCT_DIR, other_file_name)) # Ignore the specified and product-related sections. ignored_sections += ("Product", "Base Product") sections = set(parser.sections()).difference(ignored_sections) other_sections = set( other_parser.sections()).difference(ignored_sections) # Otherwise, the defined sections should be the same. self.assertEqual(sections, other_sections) for section in sections: # The defined options should be the same. self.assertEqual(parser.options(section), other_parser.options(section)) for key in parser.options(section): # The values of the options should be the same. self.assertEqual(parser.get(section, key), other_parser.get(section, key))
def test_invalid_read(self): parser = create_parser() with pytest.raises(ConfigurationFileError) as cm: read_config(parser, "nonexistent/path/to/file") assert cm.value._filename == "nonexistent/path/to/file"
def load_product(self, config_path): """Load information about a product from the given configuration file. :param config_path: a path to a configuration file :raises: ConfigurationError if a product cannot be loaded """ # Set up the parser. parser = create_parser() self._create_section(parser, "Product") self._create_section(parser, "Base Product") # Read the product sections. read_config(parser, config_path) key = self._read_section(parser, "Product") base = self._read_section(parser, "Base Product") # Check the product. if not key.product_name: raise ConfigurationError("The product name is not specified.") if key in self._products: raise ConfigurationError("The product {} was already loaded.".format(key)) # Check the base product. if not base.product_name: base = None # Add the product. log.info("Found %s at %s.", key, config_path) self._products[key] = ProductData(base, config_path)
def read(self, path): """Read a configuration file. :param path: a path to the file """ read_config(self._parser, path) self._sources.append(path)
def _read_content(self, parser): with tempfile.NamedTemporaryFile("w") as f: # Prepare the config file. f.write(self._content) f.flush() # Read the config file. read_config(parser, f.name) return parser
def load_from_file(self, config_path): """Load information about a profile from the given configuration file. :param config_path: a path to a configuration file :raises: ConfigurationError if a profile cannot be loaded """ # Set up the parser. parser = create_parser() self._create_profile_section(parser) self._create_profile_detection_section(parser) # Read the profile sections. self.config_path = config_path read_config(parser, config_path) self._read_profile_section(parser) self._read_profile_detection_section(parser) if not self.profile_id: raise ConfigurationError("The profile id is not specified!")
def _compare_product_files(self, file_name, other_file_name): parser = create_parser() read_config(parser, os.path.join(PRODUCT_DIR, file_name)) other_parser = create_parser() read_config(other_parser, os.path.join(PRODUCT_DIR, other_file_name)) # The defined sections should be the same. self.assertEqual(parser.sections(), other_parser.sections()) for section in parser.sections(): # Skip the product-related sections. if section in ("Product", "Base Product"): continue # The defined options should be the same. self.assertEqual(parser.options(section), other_parser.options(section)) for key in parser.options(section): # The values of the options should be the same. self.assertEqual(parser.get(section, key), other_parser.get(section, key))