diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-01 11:53:56 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-01 11:53:56 -0500 |
| commit | b1c23487c33fc2ad5503b6340b17980a326918c8 (patch) | |
| tree | db8fa8f1147ddcdd0df072431cfdb37e24976a1c | |
| parent | Add '-E yaml -E toml' to install instructions (diff) | |
| download | config-file-b1c23487c33fc2ad5503b6340b17980a326918c8.zip | |
Lint files
| -rw-r--r-- | config_file/config_file.py | 32 | ||||
| -rw-r--r-- | config_file/parsers/base_parser.py | 9 | ||||
| -rw-r--r-- | config_file/parsers/ini_parser.py | 2 | ||||
| -rw-r--r-- | tests/test_config_file.py | 41 | ||||
| -rw-r--r-- | tests/test_custom_parser_with_config_file.py | 31 | ||||
| -rw-r--r-- | tests/test_ini_parser.py | 2 | ||||
| -rw-r--r-- | tests/test_json_parser.py | 13 |
7 files changed, 60 insertions, 70 deletions
diff --git a/config_file/config_file.py b/config_file/config_file.py index 9b14072..adcfaf1 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -15,9 +15,7 @@ from config_file.utils import Default, create_config_path, read_file, split_on_d class ConfigFile: def __init__( - self, - file_path: Union[str, Path], - parser: Optional[Type[AbstractParser]] = None + self, file_path: Union[str, Path], parser: Optional[Type[AbstractParser]] = None ) -> None: """ Stores the config file path and expands it if needed, reads in @@ -93,10 +91,10 @@ class ConfigFile: key: The key to retrieve. parse_types: Automatically parse ints, floats, booleans, dicts, and lists. This recursively parses all types in whatever you're - retrieving, not just a single type. - - e.g. If you are retrieving a section, all values in that section - will be parsed. + retrieving, not just a single type. + + e.g. If you are retrieving a section, all values in that + section will be parsed. return_type: The type to coerce the return value to. default: The default value to return if the value of the key is empty. @@ -123,14 +121,14 @@ class ConfigFile: def set(self, key: str, value: Any) -> None: """Sets the value of a key. - If the given key does not exist, it will be automatically - created for you. That includes if there are multiple keys + If the given key does not exist, it will be automatically + created for you. That includes if there are multiple keys in a row that do not exist. e.g. set('exists.does_not.does_not.also_does_not', 5) - The behavior of how this is done, however, depends on the - file used. For example, with INI, subsections are not supported. - So it would create a key in the section `exists` called `does_not` + The behavior of how this is done, however, depends on the + file used. For example, with INI, subsections are not supported. + So it would create a key in the section `exists` called `does_not` and set it to the value {'does_not': {'also_does_not': 5}}. Args: @@ -144,7 +142,7 @@ class ConfigFile: Args: key: The section, sub-section, or key to delete. - + Raises: MissingKeyError: If a key is attempted to be deleted that does not exist. @@ -170,9 +168,9 @@ class ConfigFile: Args: key: The section, sub-section, or key to find. - wild: Whether or not to search everywhere. - - Without `wild`, a single word `key` without a `.` + wild: Whether or not to search everywhere. + + Without `wild`, a single word `key` without a `.` will look at the outer most heirarchy of the file for it. With `wild`, that single word `key` will be searched @@ -226,7 +224,7 @@ class ConfigFile: Save your configuration changes. This writes the file back out, including any changes - you've made, to the specified path given from this + you've made, to the specified path given from this object's constructor. """ with open(str(self.__path), "w") as config_file: diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py index 546450c..c2a7d71 100644 --- a/config_file/parsers/base_parser.py +++ b/config_file/parsers/base_parser.py @@ -1,5 +1,5 @@ -from typing import Any from types import ModuleType +from typing import Any from config_file.exceptions import MissingKeyError, ParsingError from config_file.nested_lookup import get_occurrence_of_key @@ -9,9 +9,7 @@ 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: Exception): super().__init__(file_contents) self.__module = module self.__decode_error = decode_error @@ -92,10 +90,9 @@ class BaseParser(AbstractParser): def has(self, search_key: str, wild: bool = False) -> bool: if wild: return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 - + try: self.get(search_key) return True except (KeyError, ParsingError): return False - diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 8dc7f47..72974f1 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -10,4 +10,4 @@ class IniParser(BaseParser): file_contents=file_contents, module=IniParserCompatibility, decode_error=configparser.Error, - )
\ No newline at end of file + ) diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 6718a94..d655397 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -169,9 +169,7 @@ def test_that_config_file_can_get( assert ret_val == value -@pytest.mark.parametrize( - "key", ["header_one.number_key", "header_one"] -) +@pytest.mark.parametrize("key", ["header_one.number_key", "header_one"]) def test_that_config_file_can_delete(templated_config_file, key): config = templated_config_file() @@ -180,9 +178,7 @@ def test_that_config_file_can_delete(templated_config_file, key): assert config.has(key) is False -@pytest.mark.parametrize( - "key", ["", 0, False, {}, "header_one.does_not_exist"] -) +@pytest.mark.parametrize("key", ["", 0, False, {}, "header_one.does_not_exist"]) def test_that_config_file_raises_missing_key_error_on_invalid_input_or_missing_key( templated_config_file, key ): @@ -190,36 +186,3 @@ def test_that_config_file_raises_missing_key_error_on_invalid_input_or_missing_k with pytest.raises(MissingKeyError): config.delete(key) - - -# def test_that_custom_parser_can_be_used(template_and_config_file): -# class CustomParser(AbstractParser): -# def __init__(self, file_contents): -# super().__init__(file_contents) -# -# def parse(self, file_contents: str): -# return file_contents -# -# def get(self, key, parse_types=True): -# return key -# -# def set(self, key, value): -# return key, value -# -# def delete(self, section_key): -# return section_key -# -# def has(self, section_key: str) -> bool: -# return True -# -# def stringify(self) -> str: -# return str(self.parsed_content) -# -# template_file, config = template_and_config_file(parser=CustomParser) -# -# assert isinstance(config._ConfigFile__parser, CustomParser) -# assert config.get("key") == "key" -# assert config.set("key", "value") == ("key", "value") -# assert config.delete("section_key") == "section_key" -# assert config.has("blah") -# assert config.stringify() == template_file.read_text() diff --git a/tests/test_custom_parser_with_config_file.py b/tests/test_custom_parser_with_config_file.py new file mode 100644 index 0000000..3c8bf98 --- /dev/null +++ b/tests/test_custom_parser_with_config_file.py @@ -0,0 +1,31 @@ +# def test_that_custom_parser_can_be_used(template_and_config_file): +# class CustomParser(AbstractParser): +# def __init__(self, file_contents): +# super().__init__(file_contents) +# +# def parse(self, file_contents: str): +# return file_contents +# +# def get(self, key, parse_types=True): +# return key +# +# def set(self, key, value): +# return key, value +# +# def delete(self, section_key): +# return section_key +# +# def has(self, section_key: str) -> bool: +# return True +# +# def stringify(self) -> str: +# return str(self.parsed_content) +# +# template_file, config = template_and_config_file(parser=CustomParser) +# +# assert isinstance(config._ConfigFile__parser, CustomParser) +# assert config.get("key") == "key" +# assert config.set("key", "value") == ("key", "value") +# assert config.delete("section_key") == "section_key" +# assert config.has("blah") +# assert config.stringify() == template_file.read_text() diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 45e82c3..46966c9 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,6 +1,6 @@ import pytest -from config_file.exceptions import ParsingError, MissingKeyError +from config_file.exceptions import MissingKeyError, ParsingError from config_file.parsers.ini_parser import IniParser diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index 514a640..f8200b0 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -1,9 +1,6 @@ -import json - import pytest from config_file.exceptions import ParsingError -from config_file.parsers.json_parser import JsonParser def test_invalid_json(templated_parser): @@ -45,8 +42,8 @@ def test_that_json_parser_can_find_wild_keys(key, templated_parser): [ "glossary", "glossary.title", - "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para" - ] + "glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para", + ], ) def test_that_json_parser_can_find_dot_keys(key, templated_parser): parser = templated_parser("json", "glossary") @@ -98,7 +95,11 @@ def test_that_json_parser_can_set_keys(templated_parser, key, value): @pytest.mark.parametrize( "key", - ["glossary", "glossary.dict_within_dict", "glossary.GlossDiv.GlossList.GlossEntry.Abbrev"], + [ + "glossary", + "glossary.dict_within_dict", + "glossary.GlossDiv.GlossList.GlossEntry.Abbrev", + ], ) def test_that_json_parser_can_delete_keys(template_and_parser, key): template, parser = template_and_parser("json", "glossary") |