diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-06-20 15:13:53 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-06-20 15:56:19 -0500 |
| commit | 95c47a183409fabed81f078a70967f053679737e (patch) | |
| tree | 1d3376c3cb1e3dfebf8feb465ae52f24ab0ca6e4 /config_file/utils.py | |
| parent | Remove taskipy from tox & add default to parser in __determine_parser (diff) | |
| download | config-file-95c47a183409fabed81f078a70967f053679737e.zip | |
Change path, parser, and contents to be properties
Also, restore_original now raises a FileNotFoundError.
Diffstat (limited to 'config_file/utils.py')
| -rw-r--r-- | config_file/utils.py | 22 |
1 files changed, 14 insertions, 8 deletions
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() |