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