diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-07-29 21:31:35 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-07-29 21:31:35 -0500 |
| commit | 5d78f7e5e8f24476ec21cf2b01c88a62e3acc9ba (patch) | |
| tree | 60c5f34383e99e4bfd84a5fe6341859ba1738b38 | |
| parent | Fix CI dependency installs (diff) | |
| download | config-file-5d78f7e5e8f24476ec21cf2b01c88a62e3acc9ba.zip | |
Revise docstrings, add `wild` to `has` method
| -rw-r--r-- | config_file/config_file.py | 96 |
1 files changed, 63 insertions, 33 deletions
diff --git a/config_file/config_file.py b/config_file/config_file.py index 3ac4b91..9b14072 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -17,25 +17,26 @@ class ConfigFile: def __init__( self, file_path: Union[str, Path], - parser: Optional[Type[AbstractParser]] = None, + parser: Optional[Type[AbstractParser]] = None ) -> None: """ - Saves the config file path and expands it if needed, reads in + Stores the config file path and expands it if needed, reads in the file contents, and determines what parser should be used for the given file. - :param file_path: The path to your configuration file. - :param parser: A custom parser you'd like used for your config file. + Args: + file_path: The path to your configuration file. + parser: A custom parser you'd like used for your config file. - :raises ValueError: If the specified file path does not have an extension - that is supported or it is a directory. + Raises: + ValueError: If the specified file path does not have an extension + that is supported or it is a directory. """ if not isinstance(file_path, Path): file_path = Path(file_path) self.__path = create_config_path(file_path) - self.__contents = read_file(self.__path) - self.__parser = self.__determine_parser(file_path, parser) + self.__parser = self.__determine_parser(self.__path, parser) @property def path(self) -> Path: @@ -44,8 +45,10 @@ class ConfigFile: def __determine_parser( self, file_path: Path, parser: Optional[Type[AbstractParser]] = None ) -> Type[AbstractParser]: + file_contents = read_file(file_path) + if parser is not None and not inspect.isabstract(parser): - return parser(self.__contents) + return parser(file_contents) try: file_type = split_on_dot(file_path, only_last_dot=True)[-1] @@ -56,7 +59,7 @@ class ConfigFile: ) return self.__find_parser_by_file_type( - file_type=file_type, file_contents=self.__contents, file_path=self.__path + file_type=file_type, file_contents=file_contents, file_path=file_path ) @staticmethod @@ -86,18 +89,23 @@ class ConfigFile: """ Retrieve the value of a key. - :param key: The key to retrieve. - :param 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 with be - parsed. - :param return_type: The type to coerce the return value to. - :param default: The default value to return if the value of the key is empty. + Args: + 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. - :return: The value of the key. + return_type: The type to coerce the return value to. + default: The default value to return if the value of the key is empty. - :raises ValueError: If the value is not able to be coerced into return_type + Returns: + The value of the key. + + Raises: + ValueError: If the value is not able to be coerced into return_type. """ try: key_value = self.__parser.get(key) @@ -115,16 +123,33 @@ 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 + 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` + and set it to the value {'does_not': {'also_does_not': 5}}. + + Args: + key: The section, sub-section, or key to delete. + value: The value to set the key to. """ self.__parser.set(key, value) - def delete(self, section_key: str) -> None: + def delete(self, key: str) -> None: """Deletes a section or key. Args: - section_key: + key: The section, sub-section, or key to delete. + + Raises: + MissingKeyError: If a key is attempted to be deleted that + does not exist. """ - self.__parser.delete(section_key) + self.__parser.delete(key) def stringify(self) -> str: """Retrieves file contents as a string. @@ -135,7 +160,7 @@ class ConfigFile: """ return self.__parser.stringify() - def has(self, section_key: str) -> bool: + def has(self, key: str, wild: bool = False) -> bool: """ Check if a section, sub-section, or key exists. @@ -144,12 +169,19 @@ class ConfigFile: key exists. Args: - section_key: + key: The section, sub-section, or key to find. + 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 + for throughout the entire file. Returns: - True if + True if the key exists. False otherwise. """ - return self.__parser.has(section_key) + return self.__parser.has(key, wild=wild) def restore_original( self, original_file_path: Union[str, Path, None] = None @@ -187,17 +219,15 @@ class ConfigFile: self.__path.expanduser().unlink() copyfile(original_file_path, self.__path) - - self.__contents = read_file(self.__path) - self.__parser.reset_internal_contents(self.__contents) + self.__parser.reset_internal_contents(read_file(self.__path)) def save(self) -> None: """ Save your configuration changes. This writes the file back out, including any changes - you've made, to the specified path when this object - was created. + you've made, to the specified path given from this + object's constructor. """ with open(str(self.__path), "w") as config_file: - config_file.write(self.__parser.stringify()) + config_file.write(self.stringify()) |