def deserializeMetadata(cls, serialized: str, container_id: str) -> List[Dict[str, Any]]: """Gets the metadata of an instance container from a serialised format. This parses the entire CFG document and only extracts the metadata from it. :param serialized: A CFG document, serialised as a string. :param container_id: The ID of the container to get the metadata of, as obtained from the file name. :return: A dictionary of metadata that was in the CFG document in a singleton list. If anything went wrong, this returns an empty list instead. """ serialized = cls._updateSerialized( serialized) # Update to most recent version. parser = FastConfigParser(serialized) metadata = {"id": container_id, "container_type": InstanceContainer} try: metadata["name"] = parser["general"]["name"] metadata["version"] = parser["general"]["version"] metadata["definition"] = parser["general"]["definition"] except KeyError as e: #One of the keys or the General section itself is missing. raise InvalidInstanceError( "Missing required fields: {error_msg}".format( error_msg=str(e))) if "metadata" in parser: metadata = {**metadata, **parser["metadata"]} return [metadata]
def _readAndValidateSerialized(cls, serialized: str) -> FastConfigParser: # Disable comments in the ini files, so text values can start with a ; # without being removed as a comment parser = FastConfigParser(serialized) has_general = "general" in parser has_version = has_general and "version" in parser["general"] has_definition = has_general and "definition" in parser["general"] if not has_general or not has_version or not has_definition: exception_string = "Missing the required" if not has_general: exception_string += " section 'general'" if not has_definition: exception_string += " property 'definition'" if not has_version: exception_string += " property 'version'" raise InvalidInstanceError(exception_string) return parser
def test_fuzz_configparser(self, test_name, contents): """ Fuzz testing to see if the result is equal to the normal ConfigParser. :param test_name: Just an identifier to recognise the test by. :param contents: The contents of a hypothetical file. """ parser = FastConfigParser(contents) # Our own implementation. ground_truth = configparser.ConfigParser(interpolation = None) ground_truth.read_string(contents) # Now see if the result is the same. for header in parser: assert header in ground_truth for header in ground_truth: if len(ground_truth[header]) == 0: # Don't need to also mirror empty headers (ConfigParser always has a "DEFAULT" header). continue assert header in parser for header in parser: for key in parser[header]: assert key in ground_truth[header] assert parser[header][key] == ground_truth[header][key] for key in ground_truth[header]: assert key in parser[header]
def test_settingValues(self, file_name, values): parser = FastConfigParser(self.data[file_name]) for header_key, setting_pairs in values.items(): header_data = parser[header_key] for key in setting_pairs: assert header_data[key] == setting_pairs[key]
def test_hasHeaders(self, file_name, header_list): parser = FastConfigParser(self.data[file_name]) for header in header_list: assert header in parser