diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-07-19 15:02:13 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-07-19 15:02:13 -0500 |
| commit | 461cb78f0a56b9293b5ade10fe90ec4aa77d23ad (patch) | |
| tree | 8dc05802dd09b38b7766da886190871daf3b6667 | |
| parent | Remove zsh info and add extras sub header (diff) | |
| download | config-file-461cb78f0a56b9293b5ade10fe90ec4aa77d23ad.zip | |
Change name of BaseParser to AbstractParser
| -rw-r--r-- | config_file/__init__.py | 4 | ||||
| -rw-r--r-- | config_file/config_file.py | 12 | ||||
| -rw-r--r-- | config_file/parsers/__init__.py | 14 | ||||
| -rw-r--r-- | config_file/parsers/abstract_parser.py | 20 | ||||
| -rw-r--r-- | config_file/parsers/ini_parser.py | 4 | ||||
| -rw-r--r-- | config_file/parsers/json_parser.py | 10 | ||||
| -rw-r--r-- | config_file/parsers/toml_parser.py | 104 | ||||
| -rw-r--r-- | config_file/parsers/yaml_parser.py | 5 | ||||
| -rw-r--r-- | pyproject.toml | 2 | ||||
| -rw-r--r-- | tests/conftest.py | 8 | ||||
| -rw-r--r-- | tests/test_base_parser.py | 6 | ||||
| -rw-r--r-- | tests/test_config_file.py | 10 |
12 files changed, 51 insertions, 148 deletions
diff --git a/config_file/__init__.py b/config_file/__init__.py index 1b2094f..3c43d33 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,6 +1,6 @@ from config_file.config_file import ConfigFile from config_file.exceptions import ConfigFileError, ParsingError -from config_file.parsers import BaseParser +from config_file.parsers import AbstractParser __version__ = "0.10.0" @@ -8,5 +8,5 @@ __all__ = [ "ConfigFile", "ParsingError", "ConfigFileError", - "BaseParser", + "AbstractParser", ] diff --git a/config_file/config_file.py b/config_file/config_file.py index de8b851..34df561 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -4,7 +4,7 @@ from shutil import copyfile from typing import Any, Type, Union from config_file.exceptions import ParsingError -from config_file.parsers import BaseParser, IniParser, JsonParser +from config_file.parsers import AbstractParser, IniParser, JsonParser from config_file.utils import create_config_path, read_file, split_on_dot @@ -12,7 +12,7 @@ class ConfigFile: def __init__( self, file_path: Type[Union[str, Type[PurePath]]], - parser: Type[BaseParser] = None, + parser: Type[AbstractParser] = None, ) -> None: """ Saves the config file path and expands it if needed, reads in @@ -37,8 +37,8 @@ class ConfigFile: return self.__path def __determine_parser( - self, file_path: Type[PurePath], parser: Type[BaseParser] = None - ) -> Type[BaseParser]: + self, file_path: Type[PurePath], parser: Type[AbstractParser] = None + ) -> Type[AbstractParser]: if parser is not None and not inspect.isabstract(parser): return parser(self.__contents) @@ -108,7 +108,9 @@ class ConfigFile: """ return self.__parser.has(section_key) - def restore_original(self, original_file_path: Union[str, Type[PurePath]] = None) -> bool: + def restore_original( + self, original_file_path: Union[str, Type[PurePath]] = None + ) -> bool: """ Restores the original the config file by deleting it and copying the original back in its place. The internal contents of this config file object are then set diff --git a/config_file/parsers/__init__.py b/config_file/parsers/__init__.py index 8da520e..554b624 100644 --- a/config_file/parsers/__init__.py +++ b/config_file/parsers/__init__.py @@ -1,17 +1,17 @@ -from .base_parser import BaseParser +from .abstract_parser import AbstractParser from .ini_parser import IniParser from .json_parser import JsonParser -from .toml_parser import TomlParser -from .yaml_parser import YamlParser from .parse_value import ( - parse_value, can_be_parsed_as_bool, can_be_parsed_as_float, - can_be_parsed_as_int + can_be_parsed_as_int, + parse_value, ) +from .toml_parser import TomlParser +from .yaml_parser import YamlParser __all__ = [ - "BaseParser", + "AbstractParser", "IniParser", "JsonParser", "TomlParser", @@ -19,5 +19,5 @@ __all__ = [ "parse_value", "can_be_parsed_as_bool", "can_be_parsed_as_float", - "can_be_parsed_as_int" + "can_be_parsed_as_int", ] diff --git a/config_file/parsers/abstract_parser.py b/config_file/parsers/abstract_parser.py index da6e90a..8f1bee1 100644 --- a/config_file/parsers/abstract_parser.py +++ b/config_file/parsers/abstract_parser.py @@ -2,7 +2,7 @@ from abc import ABC, abstractmethod from typing import Any -class BaseParser(ABC): +class AbstractParser(ABC): """ The abstract base parser for all other parsers to implement. @@ -26,6 +26,14 @@ class BaseParser(ABC): self.parsed_content = self.parse(self.content) @abstractmethod + def parse(self, file_contents: str) -> Any: + """ + Parse the file_contents into an internal representation + the given parser can work with. + """ + raise NotImplementedError + + @abstractmethod def get(self, key: str, parse_types: bool = False, all: bool = False) -> Any: """ Retrieve the value of a key in its native type. @@ -62,14 +70,6 @@ class BaseParser(ABC): using a section.key format. Some formats, like JSON, do not have sections and therefore, - it would only be checking if - """ - raise NotImplementedError - - @abstractmethod - def parse(self, file_contents: str) -> Any: - """ - Parse the file_contents into an internal representation - the given parser can work with. + it would only be checking if a certain key exists. """ raise NotImplementedError diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 2db1ae8..164ff6d 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -2,12 +2,12 @@ import configparser from io import StringIO from config_file.exceptions import ParsingError -from config_file.parsers.base_parser import BaseParser +from config_file.parsers.abstract_parser import AbstractParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot -class IniParser(BaseParser): +class IniParser(AbstractParser): def __init__(self, file_contents: str): """Reads in the file contents into the configparser.""" super().__init__(file_contents) diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py index 13b84b5..237c0b5 100644 --- a/config_file/parsers/json_parser.py +++ b/config_file/parsers/json_parser.py @@ -1,18 +1,17 @@ import json +from config_file.exceptions import ParsingError from config_file.nested_lookup import ( get_occurrence_of_key, nested_delete, nested_lookup, nested_update, ) - -from config_file.exceptions import ParsingError -from config_file.parsers import BaseParser, parse_value +from config_file.parsers import AbstractParser, parse_value from config_file.utils import split_on_dot -class JsonParser(BaseParser): +class JsonParser(AbstractParser): """ The JsonParser allows use of json files with ConfigFile. The syntax it supports is a bit more flexible than the IniParser. @@ -76,7 +75,8 @@ class JsonParser(BaseParser): result = result[key] except TypeError: raise ParsingError( - f"Cannot get {key} because {key_causing_error} is not subscriptable." + f"Cannot get {key} because {key_causing_error} " + "is not subscriptable." ) else: result = self.parsed_content[key] diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py index ad3e7f4..4905aec 100644 --- a/config_file/parsers/toml_parser.py +++ b/config_file/parsers/toml_parser.py @@ -1,105 +1,5 @@ -import toml +from config_file.parsers import AbstractParser -from config_file.nested_lookup import ( - get_occurrence_of_key, - nested_delete, - nested_lookup, - nested_update, -) -from config_file.exceptions import ParsingError -from config_file.parsers import BaseParser, parse_value -from config_file.utils import split_on_dot - -class TomlParser(BaseParser): +class TomlParser(AbstractParser): """The TomlParser allows use of toml files with ConfigFile.""" - - def __init__(self, file_contents: str): - """Reads in the file contents into the parser.""" - super().__init__(file_contents) - - def parse(self, file_contents: str): - try: - return toml.loads(file_contents) - except toml.TomlDecodeError as error: - raise ParsingError(error) - - def get(self, key: str, parse_types: bool = False, all: bool = False): - """ - Retrieve values using a dot syntax. - - :param key: The key to retrieve. e.g. 'foo.bar.boo' - - :param parse_types: Whether you'd like the type parsed into its native one. - - :param all: Specify whether you'd like to recursively receive - all values that match the given key. Returned as a list. - e.g. 'foo' would retrieve all values that have the key 'foo', anywhere - in the json. - - :return: the value of the given key - """ - if all: - result = nested_lookup(key, self.parsed_content) - elif "." in key: - split_keys = split_on_dot(key) - result = self.parsed_content - - # Used so we can more precisely specify what key is causing the error. - key_for_error = None - try: - for key in split_keys: - key_for_error = key - result = result[key] - except TypeError: - raise ParsingError( - f"Cannot get {key} because {key_for_error} is not subscriptable." - ) - else: - result = self.parsed_content[key] - - return parse_value(result) if parse_types else result - - def set(self, key: str, value, all: bool = False): - if all: - nested_update(self.parsed_content, key, value, in_place=True) - elif "." in key: - indexes = split_on_dot(key) - indexes_length = len(indexes) - - ref = self.parsed_content - for index, key in enumerate(indexes): - if index == indexes_length - 1: - ref[key] = value - break - - ref = ref[key] - else: - self.parsed_content[key] = value - - return True - - def delete(self, key, all: bool = False): - if all: - nested_delete(self.parsed_content, key, in_place=True) - elif "." in key: - 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] - else: - del self.parsed_content[key] - - return True - - def stringify(self) -> str: - return json.dumps(self.parsed_content, indent=4) - - def has(self, search_key: str) -> bool: - return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py index 48722b5..a45d7f6 100644 --- a/config_file/parsers/yaml_parser.py +++ b/config_file/parsers/yaml_parser.py @@ -1,4 +1,5 @@ -from config_file.parsers import BaseParser +from config_file.parsers import AbstractParser -class YamlParser(BaseParser): + +class YamlParser(AbstractParser): pass diff --git a/pyproject.toml b/pyproject.toml index dc93234..4ac2043 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -59,7 +59,7 @@ include = '\.pyi?$' line_length = 88 multi_line_output = 3 include_trailing_comma = true -known_third_party = ["pytest", "pyyaml", "toml"] +known_third_party = ["pytest"] known_first_party = ["config_file"] [build-system] diff --git a/tests/conftest.py b/tests/conftest.py index bb16405..5fdf5a9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,7 +3,7 @@ from typing import Callable, Type import pytest -from config_file.parsers import BaseParser, IniParser, JsonParser +from config_file.parsers import AbstractParser, IniParser, JsonParser from config_file.utils import create_config_path @@ -47,7 +47,7 @@ def template_original_file() -> Callable[[Path], Path]: def template_and_parser(template_file: Callable[[str, str], Path]) -> tuple: def func( file_type: str, template_name: str = "default" - ) -> Callable[[str, str], Type[BaseParser]]: + ) -> Callable[[str, str], Type[AbstractParser]]: test_file = template_file(file_type, template_name) text = test_file.read_text(encoding="utf-8") @@ -60,8 +60,8 @@ def template_and_parser(template_file: Callable[[str, str], Path]) -> tuple: @pytest.fixture -def templated_parser(template_and_parser) -> Callable[[str, str], Type[BaseParser]]: - def func(file_type: str, template_name: str = "default") -> Type[BaseParser]: +def templated_parser(template_and_parser) -> Callable[[str, str], Type[AbstractParser]]: + def func(file_type: str, template_name: str = "default") -> Type[AbstractParser]: return template_and_parser(file_type, template_name)[1] return func diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py index 95c0c39..4eaa92f 100644 --- a/tests/test_base_parser.py +++ b/tests/test_base_parser.py @@ -1,9 +1,9 @@ import pytest -from config_file.parsers import BaseParser +from config_file.parsers import AbstractParser -class ConcreteParser(BaseParser): +class ConcreteParser(AbstractParser): def __init__(self, file_contents): super().__init__(file_contents) @@ -31,7 +31,7 @@ class ConcreteParser(BaseParser): def test_that_base_parser_can_not_be_instantiated(): with pytest.raises(TypeError): - BaseParser("") + AbstractParser("") @pytest.mark.parametrize( diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 899e644..5430fb8 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -4,14 +4,14 @@ from pathlib import Path import pytest from config_file.config_file import ConfigFile -from config_file.parsers import BaseParser +from config_file.parsers import AbstractParser SUPPORTED_FILE_TYPES = ["ini", "json"] @pytest.fixture(params=SUPPORTED_FILE_TYPES) def template_and_config_file(request, template_file): - def func(template_name: str = "default", parser: BaseParser = None): + def func(template_name: str = "default", parser: AbstractParser = None): template = template_file(request.param, template_name) return (template, ConfigFile(template, parser=parser)) @@ -20,7 +20,7 @@ def template_and_config_file(request, template_file): @pytest.fixture def templated_config_file(template_and_config_file): - def func(template_name: str = "default", parser: BaseParser = None): + def func(template_name: str = "default", parser: AbstractParser = None): return template_and_config_file(template_name, parser)[1] return func @@ -31,7 +31,7 @@ def test_that_config_file_is_initialized_correctly(template_and_config_file): assert config.path == template_file assert config.stringify() == template_file.read_text() - assert isinstance(config._ConfigFile__parser, BaseParser) + assert isinstance(config._ConfigFile__parser, AbstractParser) def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): @@ -180,7 +180,7 @@ def test_that_config_file_can_delete(templated_config_file): def test_that_custom_parser_can_be_used(template_and_config_file): - class CustomParser(BaseParser): + class CustomParser(AbstractParser): def __init__(self, file_contents): super().__init__(file_contents) |