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 /config_file | |
| parent | Add '-E yaml -E toml' to install instructions (diff) | |
| download | config-file-b1c23487c33fc2ad5503b6340b17980a326918c8.zip | |
Lint files
Diffstat (limited to 'config_file')
| -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 |
3 files changed, 19 insertions, 24 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 + ) |