aboutsummaryrefslogtreecommitdiffstats
path: root/config_file/base_parser.py
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-08-05 20:39:28 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-08-05 20:39:28 -0500
commit59eeb1321fbad41dce7c971778e37ac1943fda3b (patch)
tree0ccb143e1094784e9b149977d6f0cabf6f6220b2 /config_file/base_parser.py
parentAdd ConfigFilePath to refactor out file specific code (diff)
downloadconfig-file-59eeb1321fbad41dce7c971778e37ac1943fda3b.zip
Redo package to have a flat package structure
By having a flat package structure like this, it'll avoid exposing packages and things we don't want to be exposed.
Diffstat (limited to '')
-rw-r--r--config_file/base_parser.py (renamed from config_file/parsers/base_parser.py)73
1 files changed, 57 insertions, 16 deletions
diff --git a/config_file/parsers/base_parser.py b/config_file/base_parser.py
index c2a7d71..a987a55 100644
--- a/config_file/parsers/base_parser.py
+++ b/config_file/base_parser.py
@@ -1,52 +1,93 @@
from types import ModuleType
-from typing import Any
+from typing import Any, Type
+from config_file.abstract_parser import AbstractParser
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: ModuleType, decode_error: Exception):
+ def __init__(
+ self, file_contents: str, module: ModuleType, decode_error: Type[Exception]
+ ):
+ """The BaseParser implements the AbstractParser for us by giving it any module.
+
+ As long as the module has a `loads` and `dumps` method, the BaseParser can use
+ it. Every file is then represented and worked with as a dictionary.
+
+ Args:
+ file_contents: The contents of the file to parse.
+
+ module: The module to use to parse the files.
+ Must have a `loads` and `dumps` method.
+
+ decode_error: The error the module raises when the
+ file cannot be decoded.
+
+ Raises:
+ ParsingError: If the decode_error is raised.
+ """
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:
+ def parse_file_contents(self, module: ModuleType, decode_error: Exception) -> dict:
+ """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.
+
+ Raises:
+ ParsingError: If the decode_error is raised while running `loads`.
+
+ Returns:
+ The parsed file as a dictionary.
+ """
try:
return module.loads(self.file_contents)
except decode_error as error:
raise ParsingError(error)
def reset_internal_contents(self, file_contents: str) -> None:
+ """Reset the file contents and parsed contents of the parser.
+
+ Args:
+ file_contents: The new file contents.
+
+ Raises:
+ ParsingError: If we're unable to parse the new file contents.
+ """
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:
+ if "." not in search_key:
+ key_causing_error = search_key
+ return self.parsed_content[search_key]
+ else:
+ split_keys = split_on_dot(search_key)
+
+ content_reference = self.parsed_content
for key in split_keys:
key_causing_error = key
- result = result[key]
+ content_reference = content_reference[key]
+
except (KeyError, TypeError):
- raise ParsingError(
- f"Cannot `get` {search_key} because "
+ raise KeyError(
+ f"cannot `get` {search_key} because "
f"{key_causing_error} is not subscriptable."
)
- return result
+ return content_reference
def set(self, key: str, value: Any) -> None:
if "." not in key: