diff options
Diffstat (limited to 'config_file/utils.py')
| -rw-r--r-- | config_file/utils.py | 19 |
1 files changed, 17 insertions, 2 deletions
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 |