diff options
Diffstat (limited to 'config_file')
| -rw-r--r-- | config_file/config_file_path.py | 10 | ||||
| -rw-r--r-- | config_file/parsers/parse_value.py | 17 |
2 files changed, 18 insertions, 9 deletions
diff --git a/config_file/config_file_path.py b/config_file/config_file_path.py index 0558faa..1daad7b 100644 --- a/config_file/config_file_path.py +++ b/config_file/config_file_path.py @@ -1,6 +1,5 @@ import os -from pathlib import Path as _Path_ -from pathlib import _posix_flavour, _windows_flavour +from pathlib import Path from typing import Type from config_file.parsers.abstract_parser import AbstractParser @@ -12,19 +11,14 @@ from config_file.parsers.yaml_parser import YamlParser from .utils import split_on_dot -class ConfigFilePath(_Path_): +class ConfigFilePath(type(Path())): """A ConfigFilePath object, which subclasses Path. It provides us easy ways to grab the original config file path, file extension, validate the path, or determine the parser that should be use for the given file type. - - See https://codereview.stackexchange.com/questions/162426/subclassing-pathlib-path - for more details on subclassing the pathlib.Path object. """ - _flavour = _windows_flavour if os.name == "nt" else _posix_flavour - @property def original_path(self) -> "ConfigFilePath": """Computes the original configuration file path. diff --git a/config_file/parsers/parse_value.py b/config_file/parsers/parse_value.py index 12032d6..b324a4b 100644 --- a/config_file/parsers/parse_value.py +++ b/config_file/parsers/parse_value.py @@ -1,7 +1,6 @@ """This module allows you to parse a string into its native type.""" import ast import re -from distutils.util import strtobool from typing import Any @@ -149,6 +148,22 @@ def can_be_parsed_as_bool(value: Any) -> bool: return value == "true" or value == "false" +# distutils strtobool backport: https://github.com/yukinarit/strtobool +def strtobool(val): + """Convert a string representation of truth to true (1) or false (0). + True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values + are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if + 'val' is anything else. + """ + val = val.lower() + if val in ('y', 'yes', 't', 'true', 'on', '1'): + return 1 + elif val in ('n', 'no', 'f', 'false', 'off', '0'): + return 0 + else: + raise ValueError("invalid truth value %r" % (val,)) + + def can_be_parsed_as_dict(value: Any) -> bool: """Checks whether a value can be parsed as a dictionary. |