diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-06-21 11:12:26 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-06-21 11:12:26 -0500 |
| commit | 2c293cc0ac6678253c6af8101ba93866785f64c0 (patch) | |
| tree | 04e74630e02fadac0bdcbe15d51975a1fe0197b6 /config_file/utils.py | |
| parent | Use lowercase action name (diff) | |
| download | config-file-2c293cc0ac6678253c6af8101ba93866785f64c0.zip | |
WIP for refactoring tests
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 |