blob: 4a83a40a1788b4c3025e1c89d27a95745e85c3d4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
from pathlib import Path, PurePath
from typing import Type, Union
def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False) -> 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.
:raises ValueError: if the line does not have a dot.
"""
if isinstance(line, PurePath):
line = str(line)
if "." not in 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, 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
|