diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-07-29 23:53:15 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-07-29 23:53:15 -0500 |
| commit | 3c1ebfaf0db4a29cc8ee9ba742c6d8925dc79cff (patch) | |
| tree | 8cc6627d9d4c74bd804f452378346bf904efa31c | |
| parent | Revise `get` section for readme docs (diff) | |
| download | config-file-3c1ebfaf0db4a29cc8ee9ba742c6d8925dc79cff.zip | |
Allow string lists and dicts to be auto parsed
| -rw-r--r-- | config_file/parsers/parse_value.py | 61 | ||||
| -rw-r--r-- | config_file/utils.py | 8 | ||||
| -rw-r--r-- | tests/test_parse_value.py | 5 |
3 files changed, 61 insertions, 13 deletions
diff --git a/config_file/parsers/parse_value.py b/config_file/parsers/parse_value.py index 5f6dfe3..aa8d1fe 100644 --- a/config_file/parsers/parse_value.py +++ b/config_file/parsers/parse_value.py @@ -19,20 +19,12 @@ Example: can_be_parsed_as_int("-5") -> True parse_value("-5") -> -5 """ +import ast import re from distutils.util import strtobool def parse_value(value): - if can_be_parsed_as_int(value): - return int(value) - - if can_be_parsed_as_float(value): - return float(value) - - if can_be_parsed_as_bool(value): - return value if type(value) is bool else bool(strtobool(value)) - if type(value) is dict: parsed = {} @@ -49,6 +41,21 @@ def parse_value(value): return parsed + if can_be_parsed_as_int(value): + return int(value) + + if can_be_parsed_as_float(value): + return float(value) + + if can_be_parsed_as_bool(value): + return value if type(value) is bool else bool(strtobool(value)) + + if can_be_parsed_as_dict(value): + return parse_value(ast.literal_eval(value)) + + if can_be_parsed_as_list(value): + return parse_value(ast.literal_eval(value)) + return value @@ -88,3 +95,39 @@ def can_be_parsed_as_bool(value) -> bool: return False return value.lower() == "true" or value.lower() == "false" + + +def can_be_parsed_as_dict(value) -> bool: + if type(value) is dict: + return True + + if type(value) is not str: + return False + + value = value.strip() + if value[0] == "{" and value[-1] == "}": + try: + ast.literal_eval(value) + return True + except SyntaxError: + return False + + return False + + +def can_be_parsed_as_list(value) -> bool: + if type(value) is dict: + return True + + if type(value) is not str: + return False + + value = value.strip() + if value[0] == "[" and value[-1] == "]": + try: + ast.literal_eval(value) + return True + except SyntaxError: + return False + + return False diff --git a/config_file/utils.py b/config_file/utils.py index de31c69..a75968c 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -6,10 +6,12 @@ def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[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. + Args: + line: The line ot split on. + only_last_dot: Only split on the last occurrence of the dot. - :raises ValueError: if the line does not have a dot. + Raises: + ValueError: if the line does not have a dot. """ if isinstance(line, Path): line = str(line) diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py index 81db472..63ce6bb 100644 --- a/tests/test_parse_value.py +++ b/tests/test_parse_value.py @@ -19,13 +19,16 @@ from config_file.parsers.parse_value import can_be_parsed_as_bool, parse_value (True, True), ({"blah": "5"}, {"blah": 5}), ( - {"blah": {"blah2": {"blah3": "false"}, "abc": "5.25", "boo": "tRuE"}}, + {"blah": {"blah2": {"blah3": "fAlSe"}, "abc": "5.25", "boo": "tRuE"}}, {"blah": {"blah2": {"blah3": False}, "abc": 5.25, "boo": True}}, ), ( ["5", "4.4", "faLSe", "trUE", "foo", "-5", "-5.2"], [5, 4.4, False, True, "foo", -5, -5.2], ), + ("[1, 2, 3]", [1, 2, 3]), + ("[True, [1, 5], {}, 5.5]", [True, [1, 5], {}, 5.5]), + ("{ 'key': '5', 'key2': 'fAlSE' }", {"key": 5, "key2": False}), ], ) def test_parse_value(test_input, expected): |