diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-16 21:35:56 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-16 21:35:56 -0500 |
| commit | 2d091dcc5b1250b2c9457f0ff34c0c997a6a0867 (patch) | |
| tree | 7d0bbde5193b4a298f3510cfda9cd48d6bca597a | |
| parent | Remove sphinx dev dependencies (diff) | |
| download | config-file-2d091dcc5b1250b2c9457f0ff34c0c997a6a0867.zip | |
Refactor parsers to enforce loads/dumps/decode_error requirement
| -rw-r--r-- | config_file/abstract_parser.py | 6 | ||||
| -rw-r--r-- | config_file/base_parser.py | 79 | ||||
| -rw-r--r-- | config_file/ini_parser.py | 37 | ||||
| -rw-r--r-- | config_file/ini_parser_compat.py | 42 | ||||
| -rw-r--r-- | config_file/json_parser.py | 20 | ||||
| -rw-r--r-- | config_file/toml_parser.py | 25 | ||||
| -rw-r--r-- | config_file/yaml_parser.py | 28 |
7 files changed, 139 insertions, 98 deletions
diff --git a/config_file/abstract_parser.py b/config_file/abstract_parser.py index 689fece..dcc2b0f 100644 --- a/config_file/abstract_parser.py +++ b/config_file/abstract_parser.py @@ -3,7 +3,8 @@ from typing import Any class AbstractParser(ABC): - """Abstract Parser is the API contract for all parsers. + """Abstract Parser is the API contract for a parser for + use in the ConfigFile object. Every parser in ConfigFile implements this parser, and any user created parsers must implement this parser in @@ -18,9 +19,6 @@ class AbstractParser(ABC): Args: file_contents: The stringified version of a file. - - Attributes: - file_contents: The stringified version of a file. """ @abstractmethod diff --git a/config_file/base_parser.py b/config_file/base_parser.py index a440dcb..ab98c8c 100644 --- a/config_file/base_parser.py +++ b/config_file/base_parser.py @@ -1,4 +1,4 @@ -from types import ModuleType +from abc import abstractmethod from typing import Any, Type from config_file.abstract_parser import AbstractParser @@ -8,32 +8,67 @@ from config_file.utils import split_on_dot class BaseParser(AbstractParser): - def __init__( - self, file_contents: str, module: ModuleType, decode_error: Type[Exception] - ): - """The BaseParser implements the AbstractParser for us by giving it any module. + def __init__(self, file_contents: str): + """The BaseParser implements the AbstractParser for us, as long as the + subclasses implement `loads`, `dumps`, and `decode_error` methods. - 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. + Internally, every parser uses the BaseParser so it does not + have to re-implement common dictionary manipulation logic. However, + there is nothing about the BaseParser that is a requirement for + using ConfigFile. 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) + self.parsed_content = self.parse_file_contents() + + @abstractmethod + def loads(self, contents: str) -> dict: + """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. + + Returns: + The contents of a particular file as a dictionary + we can manipulate. + """ + raise NotImplementedError + + @abstractmethod + def dumps(self, loaded_contents: dict) -> str: + """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. + + Raises: + self.decode_error: If there is a decoding error + while attempting to transform the dict back into a string. + + Returns: + The dictionary parsed back into a string. + """ + raise NotImplementedError + + @property + @abstractmethod + def decode_error(self) -> Type[Exception]: + raise NotImplementedError - def parse_file_contents(self, module: ModuleType, decode_error: Exception) -> dict: + def parse_file_contents(self) -> dict: """Parse the file contents by running the `loads` method on the module. Args: @@ -48,8 +83,8 @@ class BaseParser(AbstractParser): The parsed file as a dictionary. """ try: - return module.loads(self.file_contents) - except decode_error as error: + return self.loads(self.file_contents) + except self.decode_error as error: raise ParsingError(error) def reset_internal_contents(self, file_contents: str) -> None: @@ -62,9 +97,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.__module, self.__decode_error - ) + self.parsed_content = self.parse_file_contents() def get(self, search_key: str) -> Any: key_causing_error = None @@ -126,7 +159,7 @@ class BaseParser(AbstractParser): raise KeyError(f"The specified key '{key}' to delete was not found.") def stringify(self) -> str: - return self.__module.dumps(self.parsed_content) + return self.dumps(self.parsed_content) def has(self, search_key: str, wild: bool = False) -> bool: if wild: diff --git a/config_file/ini_parser.py b/config_file/ini_parser.py index 72974f1..d6905a9 100644 --- a/config_file/ini_parser.py +++ b/config_file/ini_parser.py @@ -1,13 +1,38 @@ import configparser +from io import StringIO +from typing import Type from .base_parser import BaseParser -from .ini_parser_compat import IniParserCompatibility class IniParser(BaseParser): def __init__(self, file_contents: str): - super().__init__( - file_contents=file_contents, - module=IniParserCompatibility, - decode_error=configparser.Error, - ) + super().__init__(file_contents) + + @property + def decode_error(self) -> Type[Exception]: + return configparser.Error + + def loads(self, contents: str) -> dict: + parser = configparser.ConfigParser() + parser.read_string(contents) + + return self.__create_configparser_dict(parser) + + def dumps(self, loaded_contents: dict) -> str: + parser = configparser.ConfigParser() + buffer = StringIO() + + parser.read_dict(loaded_contents) + parser.write(buffer) + + return buffer.getvalue() + + def __create_configparser_dict(self, parser: configparser.ConfigParser) -> dict: + result = {} + items = dict(parser.items()) + + for section in parser.sections(): + result.update({str(section): dict(items[section])}) + + return result diff --git a/config_file/ini_parser_compat.py b/config_file/ini_parser_compat.py deleted file mode 100644 index 9dfea2e..0000000 --- a/config_file/ini_parser_compat.py +++ /dev/null @@ -1,42 +0,0 @@ -import configparser -from io import StringIO - - -class IniParserCompatibility: - """ - Provides compatibility with the json, yaml, - and toml API so we can use the BaseParser - easily, while still being able to leverage - the built-in configparser to parse the file. - - It is used as the `module` parameter - for BaseParser, which requires us to have a - `dumps` and `loads` method. - """ - - @staticmethod - def loads(file_contents: str) -> dict: - parser = configparser.ConfigParser() - parser.read_string(file_contents) - - return IniParserCompatibility.__create_configparser_dict(parser) - - @staticmethod - def dumps(file_contents: dict) -> str: - parser = configparser.ConfigParser() - buffer = StringIO() - - parser.read_dict(file_contents) - parser.write(buffer) - - return buffer.getvalue() - - @staticmethod - def __create_configparser_dict(parser: configparser.ConfigParser) -> dict: - result = {} - items = dict(parser.items()) - - for section in parser.sections(): - result.update({str(section): dict(items[section])}) - - return result diff --git a/config_file/json_parser.py b/config_file/json_parser.py index 5314cd7..58924b4 100644 --- a/config_file/json_parser.py +++ b/config_file/json_parser.py @@ -1,15 +1,19 @@ import json +from typing import Type from .base_parser import BaseParser class JsonParser(BaseParser): def __init__(self, file_contents: str): - super().__init__( - file_contents=file_contents, - module=json, - decode_error=json.decoder.JSONDecodeError, - ) - - def stringify(self) -> str: - return json.dumps(self.parsed_content, indent=4) + super().__init__(file_contents) + + @property + def decode_error(self) -> Type[Exception]: + return json.decoder.JSONDecodeError + + def loads(self, contents: str) -> dict: + return json.loads(contents) + + def dumps(self, loaded_contents: dict) -> str: + return json.dumps(loaded_contents, indent=4) diff --git a/config_file/toml_parser.py b/config_file/toml_parser.py index d3cf226..de5226c 100644 --- a/config_file/toml_parser.py +++ b/config_file/toml_parser.py @@ -1,18 +1,31 @@ +from typing import Type + from .base_parser import BaseParser class TomlParser(BaseParser): def __init__(self, file_contents: str): try: - import tomlkit - from tomlkit.exceptions import ParseError - - super().__init__( - file_contents=file_contents, module=tomlkit, decode_error=ParseError, - ) + super().__init__(file_contents) except ImportError: raise ImportError( "It doesn't appear `tomlkit` is installed, but a toml " "file was attempted to be used. Install the `toml` " "extra first with `pip install config-file[toml]`." ) + + @property + def decode_error(self) -> Type[Exception]: + from tomlkit.exceptions import ParseError + + return ParseError + + def loads(self, contents: str) -> dict: + import tomlkit + + return tomlkit.loads(contents) + + def dumps(self, loaded_contents: dict) -> str: + import tomlkit + + return tomlkit.dumps(loaded_contents) diff --git a/config_file/yaml_parser.py b/config_file/yaml_parser.py index 8d88881..fc4239a 100644 --- a/config_file/yaml_parser.py +++ b/config_file/yaml_parser.py @@ -1,21 +1,31 @@ +from typing import Type + from .base_parser import BaseParser class YamlParser(BaseParser): def __init__(self, file_contents: str): try: - from ruamel.yaml import YAML, YAMLError, round_trip_dump, round_trip_load - - yaml = YAML(typ="safe") - yaml.loads = round_trip_load - yaml.dumps = round_trip_dump - - super().__init__( - file_contents=file_contents, module=yaml, decode_error=YAMLError - ) + super().__init__(file_contents) except ImportError: raise ImportError( "It doesn't appear `ruamel.yaml` is installed, but a yaml " "file was attempted to be used. Install the `yaml` " "extra first with `pip install config-file[yaml]`." ) + + @property + def decode_error(self) -> Type[Exception]: + from ruamel.yaml import YAMLError + + return YAMLError + + def loads(self, contents: str) -> dict: + from ruamel.yaml import round_trip_load + + return round_trip_load(contents) + + def dumps(self, loaded_contents: dict) -> str: + from ruamel.yaml import round_trip_dump + + return round_trip_dump(loaded_contents) |