diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-07-20 22:51:51 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-07-20 22:51:51 -0500 |
| commit | 65d4715b0b2b9974d95ac36271caa8e177bc98fa (patch) | |
| tree | 794b6338c8b0a654c4313a5da9713368fecfca7a /config_file/parsers/base_parser.py | |
| parent | Add toml and yaml parsers (diff) | |
| download | config-file-65d4715b0b2b9974d95ac36271caa8e177bc98fa.zip | |
Add BaseParser for all other parsers
This allows one implementation for all parsers,
with the parsers overriding it for a custom one
if needed for that parser.
Diffstat (limited to 'config_file/parsers/base_parser.py')
| -rw-r--r-- | config_file/parsers/base_parser.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py new file mode 100644 index 0000000..09fbbbf --- /dev/null +++ b/config_file/parsers/base_parser.py @@ -0,0 +1,86 @@ +from typing import Any + +from config_file.exceptions import MissingKeyError, ParsingError +from config_file.nested_lookup import get_occurrence_of_key +from config_file.utils import split_on_dot + +from .abstract_parser import AbstractParser + + +class BaseParser(AbstractParser): + def __init__(self, file_contents: str, module, decode_error): + super().__init__(file_contents) + self.__module = module + self.__decode_error = decode_error + self.parsed_content = self.parse_file_contents(module, decode_error) + + def parse_file_contents(self, module, decode_error) -> Any: + try: + return module.loads(self.file_contents) + except decode_error as error: + raise ParsingError(error) + + def reset_internal_contents(self, file_contents: str) -> None: + self.file_contents = file_contents + self.parsed_content = self.parse_file_contents( + self.__module, self.__decode_error + ) + + def get(self, search_key: str) -> Any: + if "." not in search_key: + result = self.parsed_content[search_key] + return result + else: + split_keys = split_on_dot(search_key) + + result = self.parsed_content + key_causing_error = None + try: + for key in split_keys: + key_causing_error = key + result = result[key] + except TypeError: + raise ParsingError( + f"Cannot get {search_key} because " + f"{key_causing_error} is not subscriptable." + ) + + return result + + def set(self, key: str, value: Any) -> None: + if "." not in key: + self.parsed_content[key] = value + return + + keys = split_on_dot(key) + content_reference = self.parsed_content + for index, key in enumerate(keys): + if index == len(keys) - 1: + content_reference[key] = value + return + + content_reference = content_reference[key] + + def delete(self, key) -> None: + try: + if "." not in key: + del self.parsed_content[key] + else: + indexes = split_on_dot(key) + indexes_length = len(indexes) + + ref = self.parsed_content + for index, key in enumerate(indexes): + if index == indexes_length - 1: + del ref[key] + break + + ref = ref[key] + except KeyError: + raise MissingKeyError(f"The specified key '{key}' was not found.") + + def stringify(self) -> str: + return self.__module.dumps(self.parsed_content) + + def has(self, search_key: str) -> bool: + return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 |