diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-06-20 15:35:05 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-06-20 15:35:05 -0500 |
| commit | 3fc0b5a1a4281b21132ceb2fb6b1859c55be6e04 (patch) | |
| tree | b1aed875812ce8bd99e3ef5d4c648ab4bc8f5963 | |
| parent | Revert Python 3.5 support (diff) | |
| download | config-file-3fc0b5a1a4281b21132ceb2fb6b1859c55be6e04.zip | |
Add more type hints to ConfigFile and lint all files
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | config_file/__init__.py | 2 | ||||
| -rw-r--r-- | config_file/config_file.py | 28 | ||||
| -rw-r--r-- | config_file/exceptions.py | 2 | ||||
| -rw-r--r-- | config_file/parsers/base_parser.py | 1 | ||||
| -rw-r--r-- | tests/test_ini_parser.py | 2 | ||||
| -rw-r--r-- | tests/test_json_parser.py | 2 |
7 files changed, 26 insertions, 19 deletions
@@ -11,7 +11,7 @@ ## About Config File -The Config File project is designed to allow you to easily manipulate your +The Config File project is designed to allow you to easily manipulate your configuration files with the same simple API whether they are in INI, JSON, YAML, or TOML. For the time being, it only supports INI and JSON. @@ -49,7 +49,7 @@ from config_file import ConfigFile ORIGINAL_CONFIG_PATH = Path("~/some-project/some-other-config-file.ini") CONFIG_PATH = Path("~/some-project/config.ini") -# Our path can be a string or a Path object. +# Our path can be a string or a Path object. # The "~" will be automatically expanded to the full path for us. config = ConfigFile(CONFIG_PATH) @@ -66,7 +66,7 @@ print(config.get("section")) print(config.get("section", parse_types=True)) >>> {'first_key': 5, 'second_key': 'blah', 'third_key': True} -# Sometimes we want to retrieve a key but don't know whether or not +# Sometimes we want to retrieve a key but don't know whether or not # it will be set. In that case, we can set a default. print(config.get("section.unknown", default=False)) >>> False @@ -96,7 +96,7 @@ print(config.restore_original(original_file_path=ORIGINAL_CONFIG_PATH)) >>> True # Otherwise, a config.original.ini file will automatically be looked for in the -# current directory (because our configuration file we passed in was +# current directory (because our configuration file we passed in was # named config.ini). print(config.restore_original()) >>> True diff --git a/config_file/__init__.py b/config_file/__init__.py index f37019a..5fb800a 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.base_parser import BaseParser -from config_file.exceptions import ParsingError, ConfigFileError __version__ = "0.10.0" diff --git a/config_file/config_file.py b/config_file/config_file.py index 3af1083..431521a 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -1,7 +1,7 @@ import inspect -from pathlib import Path +from pathlib import Path, PurePath from shutil import copyfile -from typing import Type, Union +from typing import Any, Type, Union from config_file.exceptions import ParsingError from config_file.parsers.base_parser import BaseParser @@ -12,7 +12,9 @@ from config_file.utils import read_file, split_on_dot class ConfigFile: def __init__( - self, file_path: Type[Union[str, Path]], parser: Type[BaseParser] = None + self, + file_path: Type[Union[str, Type[PurePath]]], + parser: Type[BaseParser] = None, ) -> None: """ Saves the config file path and expands it if needed, reads in @@ -33,7 +35,7 @@ class ConfigFile: self.__parser = self.__determine_parser(file_path, parser) @property - def path(self) -> Path: + def path(self) -> Type[PurePath]: return self.__path @property @@ -45,7 +47,7 @@ class ConfigFile: return self.__parser @staticmethod - def __create_config_path(file_path: Path, original: bool = False) -> str: + def __create_config_path(file_path: Type[PurePath], original: bool = False) -> str: if len(file_path.parts) >= 1 and file_path.parts[0] == "~": return file_path.expanduser() @@ -60,7 +62,7 @@ class ConfigFile: return file_path def __determine_parser( - self, file_path: Path, parser: Type[BaseParser] = None + self, file_path: Type[PurePath], parser: Type[BaseParser] = None ) -> BaseParser: if parser is not None and not inspect.isabstract(parser): return parser(self.__contents) @@ -75,7 +77,13 @@ class ConfigFile: f"File path contains an unrecognized file type: {file_path}" ) - def get(self, key: str, parse_types: bool = False, return_type=None, default=None): + def get( + self, + key: str, + parse_types: bool = False, + return_type: Any = None, + default: Any = None, + ) -> Any: """ Retrieve the value of a key. @@ -102,7 +110,7 @@ class ConfigFile: return return_type(key_value) if return_type else key_value - def set(self, key: str, value): + def set(self, key: str, value: Any) -> bool: """Sets the value of a key.""" return self.__parser.set(key, value) @@ -124,7 +132,7 @@ class ConfigFile: """ return self.__parser.has(section_key) - def restore_original(self, original_file_path=None): + def restore_original(self, original_file_path: Union[str, Type[PurePath]] = None): """ 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 @@ -154,7 +162,7 @@ class ConfigFile: return True - def save(self): + def save(self) -> bool: """ Save the configuration changes by writing the file out to the specified path :return: True if the save succeeded. diff --git a/config_file/exceptions.py b/config_file/exceptions.py index cd7e236..e1af755 100644 --- a/config_file/exceptions.py +++ b/config_file/exceptions.py @@ -1,6 +1,6 @@ class ConfigFileError(Exception): """A generic error from the package.""" + class ParsingError(ConfigFileError): """Unable to parse the configuration file.""" -
\ No newline at end of file diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py index 5fc47c2..bbdfd91 100644 --- a/config_file/parsers/base_parser.py +++ b/config_file/parsers/base_parser.py @@ -73,4 +73,3 @@ class BaseParser(ABC): if you're going to be using the super() constructor. """ raise NotImplementedError - diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py index 1b2fd3b..fb92373 100644 --- a/tests/test_ini_parser.py +++ b/tests/test_ini_parser.py @@ -1,7 +1,7 @@ import pytest -from config_file.parsers.ini_parser import IniParser from config_file.exceptions import ParsingError +from config_file.parsers.ini_parser import IniParser @pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"]) diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index 9a8ac09..42a0ba9 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -2,8 +2,8 @@ import json import pytest -from config_file.parsers.json_parser import JsonParser from config_file.exceptions import ParsingError +from config_file.parsers.json_parser import JsonParser @pytest.mark.parametrize("test_json", ["{'test': }", "[section]\nblah = value\n\n"]) |