aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-08-16 22:02:04 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-08-16 22:02:04 -0500
commit954404e8a111d6445bf9313f0f27c205d88b550f (patch)
tree2f91adf21529b401dd9d706019a1d097db8c867e
parentUse black isort profile (diff)
downloadconfig-file-954404e8a111d6445bf9313f0f27c205d88b550f.zip
Enforce `parsed_content` property in AbstractParser
It is used in the ConfigFile object to be able to index into it.
-rw-r--r--config_file/abstract_parser.py15
-rw-r--r--config_file/base_parser.py29
-rw-r--r--tests/conftest.py3
-rw-r--r--tests/test_abstract_parser.py4
4 files changed, 39 insertions, 12 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)
diff --git a/tests/conftest.py b/tests/conftest.py
index 3ec303e..6eb351c 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -3,7 +3,6 @@ from typing import Callable, Tuple, Type, Union
import pytest
-from config_file import ConfigFile
from config_file.abstract_parser import AbstractParser
from config_file.config_file_path import ConfigFilePath
@@ -55,7 +54,7 @@ def template_and_parser(
file_type: str, template_name: str = "default"
) -> Tuple[Path, Type[AbstractParser]]:
test_file = template_file(file_type, template_name)
- return test_file, ConfigFile(test_file)._ConfigFile__parser
+ return test_file, ConfigFilePath(test_file).parser
return func
diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py
index 0012127..f52ab45 100644
--- a/tests/test_abstract_parser.py
+++ b/tests/test_abstract_parser.py
@@ -31,6 +31,9 @@ class ConcreteParser(AbstractParser):
def reset_internal_contents(self, file_contents: str) -> None:
super().reset_internal_contents(file_contents)
+ def parsed_content(self) -> dict:
+ super().parsed_content()
+
def test_that_abstract_parser_can_not_be_instantiated():
"""
@@ -52,6 +55,7 @@ def test_that_abstract_parser_can_not_be_instantiated():
(ConcreteParser("").stringify, None),
(ConcreteParser("").has, ("",)),
(ConcreteParser("").reset_internal_contents, ("",)),
+ (ConcreteParser("").parsed_content, None),
],
)
def test_that_abstract_parser_raises_not_implemented_errors(function, args):