diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-12-21 22:31:42 -0600 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2023-11-05 15:31:22 -0500 |
| commit | 3f874d1ba218a82ac354af748be73bbbf6e0d985 (patch) | |
| tree | 132cf9a69e7e4f725ad9366424791e24b0ea7a84 /config_file/parsers/base_parser.py | |
| parent | Fix readme badge (diff) | |
| download | config-file-3f874d1ba218a82ac354af748be73bbbf6e0d985.zip | |
Remove depreciated stringify() method
Diffstat (limited to 'config_file/parsers/base_parser.py')
| -rw-r--r-- | config_file/parsers/base_parser.py | 83 |
1 files changed, 51 insertions, 32 deletions
diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py index ea94164..b1ef1c2 100644 --- a/config_file/parsers/base_parser.py +++ b/config_file/parsers/base_parser.py @@ -9,7 +9,8 @@ from config_file.utils import split_on_dot class BaseParser(AbstractParser): def __init__(self, file_contents: str): - """The BaseParser implements the AbstractParser for us, as long as the + """ + The BaseParser implements the AbstractParser for us, as long as the subclasses implement `loads`, `dumps`, and `decode_error` methods. Internally, every parser uses the BaseParser so it does not @@ -28,14 +29,15 @@ class BaseParser(AbstractParser): @abstractmethod def loads(self, contents: str) -> dict: - """Transforms the file contents into a dictionary. + """ + Transforms the file contents into a dictionary. Args: contents: The file contents to transform. Raises: self.decode_error: If there is a decoding error - while attempting to parse the string. + while attempting to parse the string. Returns: The contents of a particular file as a dictionary @@ -45,18 +47,18 @@ class BaseParser(AbstractParser): @abstractmethod def dumps(self, loaded_contents: dict) -> str: - """Transform the parsed contents back into a string. + """ + Transform the parsed contents back into a string. Args: loaded_contents: The file contents from a self.loads call. - - It doesn't necessary need to be from a loads call, could just - be a valid dictionary for the given format, but that is the - typical usecase. + It doesn't necessary need to be from a loads call, could just + be a valid dictionary for the given format, but that is the + typical usecase. Raises: self.decode_error: If there is a decoding error - while attempting to transform the dict back into a string. + while attempting to transform the dict back into a string. Returns: The dictionary parsed back into a string. @@ -66,7 +68,8 @@ class BaseParser(AbstractParser): @property @abstractmethod def decode_error(self) -> Type[Exception]: - """The decoding error raised by the subclass on loads/dumps. + """ + The decoding error raised by the subclass on loads/dumps. Returns: The decoding error @@ -77,13 +80,17 @@ class BaseParser(AbstractParser): def parsed_content(self) -> dict: return self.__parsed_content + def __str__(self) -> str: + return self.dumps(self.__parsed_content) + def parse_file_contents(self) -> dict: - """Parse the file contents by running the `loads` method on the module. + """ + Parse the file contents by running the `loads` method on the module. Args: module: The module to use to parse the file. decode_error: The error that is raised from the module if the file - cannot be decoded. + cannot be decoded. Raises: ParsingError: If the decode_error is raised while running `loads`. @@ -97,7 +104,8 @@ class BaseParser(AbstractParser): raise ParsingError(error) def reset_internal_contents(self, file_contents: str) -> None: - """Reset the file contents and parsed contents of the parser. + """ + Reset the file contents and parsed contents of the parser. Args: file_contents: The new file contents. @@ -109,27 +117,41 @@ class BaseParser(AbstractParser): self.__parsed_content = self.parse_file_contents() def get(self, search_key: str) -> Any: - key_causing_error = None + """ + Retrieve a key from the parsed content. + + Args: + search_key: The key to search from in the + parsed content in a "dot" syntax. + i.e. "section.key" + + Raises: + KeyError: If the key or one of the sections + we're subscripting into does not exist. + + Returns: + The value of the key we're searching for. + """ + error_key = None try: if "." not in search_key: - key_causing_error = search_key + error_key = search_key return self.__parsed_content[search_key] else: split_keys = split_on_dot(search_key) - content_reference = self.__parsed_content + parsed_content = self.__parsed_content for key in split_keys: - key_causing_error = key - content_reference = content_reference[key] - + error_key = key + parsed_content = parsed_content[key] except (KeyError, TypeError): raise KeyError( f"cannot `get` {search_key} because " - f"{key_causing_error} is not subscriptable." + f"{error_key} is not subscriptable." ) - return content_reference + return parsed_content def set(self, key: str, value: Any) -> None: if "." not in key: @@ -137,17 +159,17 @@ class BaseParser(AbstractParser): return keys = split_on_dot(key) - content_reference = self.__parsed_content + parsed_content = self.__parsed_content for index, key in enumerate(keys): if index == len(keys) - 1: - content_reference[key] = value + parsed_content[key] = value return try: - content_reference = content_reference[key] + parsed_content = parsed_content[key] except KeyError: - content_reference[key] = {} - content_reference = content_reference[key] + parsed_content[key] = {} + parsed_content = parsed_content[key] def delete(self, key: str) -> None: try: @@ -157,19 +179,16 @@ class BaseParser(AbstractParser): indexes = split_on_dot(key) indexes_length = len(indexes) - content_reference = self.__parsed_content + parsed_content = self.__parsed_content for index, key in enumerate(indexes): if index == indexes_length - 1: - del content_reference[key] + del parsed_content[key] break - content_reference = content_reference[key] + parsed_content = parsed_content[key] except (KeyError, TypeError): raise KeyError(f"The specified key '{key}' to delete was not found.") - def stringify(self) -> str: - return self.dumps(self.__parsed_content) - def has(self, search_key: str, wild: bool = False) -> bool: if wild: return get_occurrence_of_key(self.__parsed_content, key=search_key) > 0 |