diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-16 22:02:04 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-16 22:02:04 -0500 |
| commit | 954404e8a111d6445bf9313f0f27c205d88b550f (patch) | |
| tree | 2f91adf21529b401dd9d706019a1d097db8c867e /config_file | |
| parent | Use black isort profile (diff) | |
| download | config-file-954404e8a111d6445bf9313f0f27c205d88b550f.zip | |
Enforce `parsed_content` property in AbstractParser
It is used in the ConfigFile object to be able to index
into it.
Diffstat (limited to 'config_file')
| -rw-r--r-- | config_file/abstract_parser.py | 15 | ||||
| -rw-r--r-- | config_file/base_parser.py | 29 |
2 files changed, 34 insertions, 10 deletions
diff --git a/config_file/abstract_parser.py b/config_file/abstract_parser.py index dcc2b0f..460b238 100644 --- a/config_file/abstract_parser.py +++ b/config_file/abstract_parser.py @@ -25,6 +25,21 @@ class AbstractParser(ABC): def __init__(self, file_contents: str) -> None: self.file_contents = file_contents + @property + @abstractmethod + def parsed_content(self) -> dict: + """To be able to index into the ConfigFile object, + we have to expose the parsed dictionary object. + + Raises: + KeyError: If the key we are trying to index into + does not exist. + + Returns: + The internal contents parsed as a dictionary. + """ + raise NotImplementedError + @abstractmethod def reset_internal_contents(self, file_contents: str) -> None: """Reset the state of this parser to a new file. diff --git a/config_file/base_parser.py b/config_file/base_parser.py index ab98c8c..5063cc7 100644 --- a/config_file/base_parser.py +++ b/config_file/base_parser.py @@ -24,7 +24,7 @@ class BaseParser(AbstractParser): ParsingError: If the decode_error is raised. """ super().__init__(file_contents) - self.parsed_content = self.parse_file_contents() + self.__parsed_content = self.parse_file_contents() @abstractmethod def loads(self, contents: str) -> dict: @@ -66,8 +66,17 @@ class BaseParser(AbstractParser): @property @abstractmethod def decode_error(self) -> Type[Exception]: + """The decoding error raised by the subclass on loads/dumps. + + Returns: + The decoding error + """ raise NotImplementedError + @property + def parsed_content(self) -> dict: + return self.__parsed_content + def parse_file_contents(self) -> dict: """Parse the file contents by running the `loads` method on the module. @@ -97,7 +106,7 @@ class BaseParser(AbstractParser): ParsingError: If we're unable to parse the new file contents. """ self.file_contents = file_contents - self.parsed_content = self.parse_file_contents() + self.__parsed_content = self.parse_file_contents() def get(self, search_key: str) -> Any: key_causing_error = None @@ -105,11 +114,11 @@ class BaseParser(AbstractParser): try: if "." not in search_key: key_causing_error = search_key - return self.parsed_content[search_key] + return self.__parsed_content[search_key] else: split_keys = split_on_dot(search_key) - content_reference = self.parsed_content + content_reference = self.__parsed_content for key in split_keys: key_causing_error = key content_reference = content_reference[key] @@ -124,11 +133,11 @@ class BaseParser(AbstractParser): def set(self, key: str, value: Any) -> None: if "." not in key: - self.parsed_content[key] = value + self.__parsed_content[key] = value return keys = split_on_dot(key) - content_reference = self.parsed_content + content_reference = self.__parsed_content for index, key in enumerate(keys): if index == len(keys) - 1: content_reference[key] = value @@ -143,12 +152,12 @@ class BaseParser(AbstractParser): def delete(self, key: str) -> None: try: if "." not in key: - del self.parsed_content[key] + del self.__parsed_content[key] else: indexes = split_on_dot(key) indexes_length = len(indexes) - content_reference = self.parsed_content + content_reference = self.__parsed_content for index, key in enumerate(indexes): if index == indexes_length - 1: del content_reference[key] @@ -159,11 +168,11 @@ class BaseParser(AbstractParser): raise KeyError(f"The specified key '{key}' to delete was not found.") def stringify(self) -> str: - return self.dumps(self.parsed_content) + 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 + return get_occurrence_of_key(self.__parsed_content, key=search_key) > 0 try: self.get(search_key) |