From 95c47a183409fabed81f078a70967f053679737e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:13:53 -0500 Subject: Change path, parser, and contents to be properties Also, restore_original now raises a FileNotFoundError. --- config_file/utils.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'config_file/utils.py') diff --git a/config_file/utils.py b/config_file/utils.py index 61f0ae0..05c3a7a 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -1,4 +1,8 @@ -def split_on_dot(line: str, only_last_dot=False): +from pathlib import Path +from typing import Union + + +def split_on_dot(line: Union[str, Path], only_last_dot=False): """ Split a string on the dot character (.). @@ -7,15 +11,17 @@ def split_on_dot(line: str, only_last_dot=False): :raises ValueError: if the line does not have a dot. """ + if isinstance(line, Path): + line = str(line) + if "." not in line: - # TODO: This is used in the parser for section.keys where a more specific - # error message would be nice, but it is only used more generically to, say, - # split a file path. We may just have to insert some try-catches to catch the - # ValueError and insert a more specific error message or create a custom error - # for this method? raise ValueError( - "section_key must contain the section and key separated by a dot. " - + "e.g. 'section.key'" + "The given string does not have a dot to split on: {}".format(line) ) return line.rsplit(".", 1) if only_last_dot else line.split(".") + + +def read_file(path: Union[str, Path]): + with open(path, "r") as file: + return file.read() -- cgit v1.2.3 From 58808235ea6880c3a0ecfa976a700c81431dc902 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 20 Jun 2020 15:30:11 -0500 Subject: Revert Python 3.5 support --- config_file/utils.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'config_file/utils.py') diff --git a/config_file/utils.py b/config_file/utils.py index 05c3a7a..9ed09b5 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -1,8 +1,8 @@ -from pathlib import Path -from typing import Union +from pathlib import PurePath +from typing import Type, Union -def split_on_dot(line: Union[str, Path], only_last_dot=False): +def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False): """ Split a string on the dot character (.). @@ -11,17 +11,15 @@ def split_on_dot(line: Union[str, Path], only_last_dot=False): :raises ValueError: if the line does not have a dot. """ - if isinstance(line, Path): + if isinstance(line, PurePath): line = str(line) if "." not in line: - raise ValueError( - "The given string does not have a dot to split on: {}".format(line) - ) + raise ValueError(f"The given string does not have a dot to split on: {line}") return line.rsplit(".", 1) if only_last_dot else line.split(".") -def read_file(path: Union[str, Path]): +def read_file(path: Union[str, Type[PurePath]]): with open(path, "r") as file: return file.read() -- cgit v1.2.3 From 2c293cc0ac6678253c6af8101ba93866785f64c0 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 21 Jun 2020 11:12:26 -0500 Subject: WIP for refactoring tests --- config_file/utils.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'config_file/utils.py') diff --git a/config_file/utils.py b/config_file/utils.py index 9ed09b5..4a83a40 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -1,8 +1,8 @@ -from pathlib import PurePath +from pathlib import Path, PurePath from typing import Type, Union -def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False): +def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False) -> str: """ Split a string on the dot character (.). @@ -23,3 +23,18 @@ def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False): def read_file(path: Union[str, Type[PurePath]]): with open(path, "r") as file: return file.read() + + +def create_config_path(file_path: Type[PurePath], original: bool = False) -> Path: + if len(file_path.parts) >= 1 and file_path.parts[0] == "~": + return file_path.expanduser() + + if original: + file_parts = split_on_dot(file_path, only_last_dot=True) + file_parts.insert(-1, "original") + file_path = Path(".".join(file_parts)) + + if file_path.is_dir(): + raise ValueError(f"The specified config file ({file_path}) is a directory.") + + return file_path -- cgit v1.2.3 From 9010575de1cd91c9fb4152835704931255e3f30d Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:54:03 -0500 Subject: Fix type hints and add Default class --- config_file/utils.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'config_file/utils.py') diff --git a/config_file/utils.py b/config_file/utils.py index 4a83a40..de31c69 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -1,8 +1,8 @@ -from pathlib import Path, PurePath -from typing import Type, Union +from pathlib import Path +from typing import List, Union -def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False) -> str: +def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[str]: """ Split a string on the dot character (.). @@ -11,7 +11,7 @@ def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False) -> str: :raises ValueError: if the line does not have a dot. """ - if isinstance(line, PurePath): + if isinstance(line, Path): line = str(line) if "." not in line: @@ -20,12 +20,12 @@ def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False) -> str: return line.rsplit(".", 1) if only_last_dot else line.split(".") -def read_file(path: Union[str, Type[PurePath]]): +def read_file(path: Union[str, Path]) -> str: with open(path, "r") as file: return file.read() -def create_config_path(file_path: Type[PurePath], original: bool = False) -> Path: +def create_config_path(file_path: Path, original: bool = False) -> Path: if len(file_path.parts) >= 1 and file_path.parts[0] == "~": return file_path.expanduser() @@ -38,3 +38,11 @@ def create_config_path(file_path: Type[PurePath], original: bool = False) -> Pat raise ValueError(f"The specified config file ({file_path}) is a directory.") return file_path + + +class Default: + def __init__(self, value): + self.value = value + + def __repr__(self): + return "Default Value: {} ({})".format(self.value, type(self.value)) -- cgit v1.2.3 From 3c1ebfaf0db4a29cc8ee9ba742c6d8925dc79cff Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 29 Jul 2020 23:53:15 -0500 Subject: Allow string lists and dicts to be auto parsed --- config_file/utils.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'config_file/utils.py') diff --git a/config_file/utils.py b/config_file/utils.py index de31c69..a75968c 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -6,10 +6,12 @@ def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[str]: """ Split a string on the dot character (.). - :param line: The line ot split on. - :param only_last_dot: Only split on the last occurrence of the dot. + Args: + line: The line ot split on. + only_last_dot: Only split on the last occurrence of the dot. - :raises ValueError: if the line does not have a dot. + Raises: + ValueError: if the line does not have a dot. """ if isinstance(line, Path): line = str(line) -- cgit v1.2.3 From 3fa2561f16f684bb83aa9b3dcacb44171b04ddc5 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 11:56:44 -0500 Subject: Raise FileNotFoundError if the config file does not exist --- config_file/utils.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'config_file/utils.py') diff --git a/config_file/utils.py b/config_file/utils.py index a75968c..4d8bb36 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -39,6 +39,11 @@ def create_config_path(file_path: Path, original: bool = False) -> Path: if file_path.is_dir(): raise ValueError(f"The specified config file ({file_path}) is a directory.") + if not file_path.exists(): + raise FileNotFoundError( + f"The specified config file ({file_path}) does not exist." + ) + return file_path -- cgit v1.2.3 From 032196104a59761bd75b5d594440d1e8ef16965a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 17:58:32 -0500 Subject: Add ConfigFilePath to refactor out file specific code --- config_file/utils.py | 33 --------------------------------- 1 file changed, 33 deletions(-) (limited to 'config_file/utils.py') diff --git a/config_file/utils.py b/config_file/utils.py index 4d8bb36..c6c3d24 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -20,36 +20,3 @@ def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[str]: raise ValueError(f"The given string does not have a dot to split on: {line}") return line.rsplit(".", 1) if only_last_dot else line.split(".") - - -def read_file(path: Union[str, Path]) -> str: - with open(path, "r") as file: - return file.read() - - -def create_config_path(file_path: Path, original: bool = False) -> Path: - if len(file_path.parts) >= 1 and file_path.parts[0] == "~": - return file_path.expanduser() - - if original: - file_parts = split_on_dot(file_path, only_last_dot=True) - file_parts.insert(-1, "original") - file_path = Path(".".join(file_parts)) - - if file_path.is_dir(): - raise ValueError(f"The specified config file ({file_path}) is a directory.") - - if not file_path.exists(): - raise FileNotFoundError( - f"The specified config file ({file_path}) does not exist." - ) - - return file_path - - -class Default: - def __init__(self, value): - self.value = value - - def __repr__(self): - return "Default Value: {} ({})".format(self.value, type(self.value)) -- cgit v1.2.3 From 59eeb1321fbad41dce7c971778e37ac1943fda3b Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 20:39:28 -0500 Subject: Redo package to have a flat package structure By having a flat package structure like this, it'll avoid exposing packages and things we don't want to be exposed. --- config_file/utils.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'config_file/utils.py') diff --git a/config_file/utils.py b/config_file/utils.py index c6c3d24..3554c0e 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -20,3 +20,19 @@ def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[str]: raise ValueError(f"The given string does not have a dot to split on: {line}") return line.rsplit(".", 1) if only_last_dot else line.split(".") + + +class Default: + """ + Default is used for the `default` value in ConfigFile's `get`. + + Previously, the default value for `get` was None. However, then + the user cannot have `get` return a default value of None. So this + class is used instead to get around that limitation. + """ + + def __init__(self, value): + self.value = value + + def __repr__(self): + return "Default Value: {} ({})".format(self.value, type(self.value)) -- cgit v1.2.3 From 32826f7d510ff4c469b2fa67d232325224a368eb Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 08:49:14 -0500 Subject: Add type hint to split_on_dot --- config_file/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'config_file/utils.py') diff --git a/config_file/utils.py b/config_file/utils.py index 3554c0e..cbbf7a7 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -2,7 +2,7 @@ from pathlib import Path from typing import List, Union -def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[str]: +def split_on_dot(line: Union[str, Path], only_last_dot: bool = False) -> List[str]: """ Split a string on the dot character (.). -- cgit v1.2.3