aboutsummaryrefslogtreecommitdiffstats
path: root/config_file
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-07-29 23:53:15 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-07-29 23:53:15 -0500
commit3c1ebfaf0db4a29cc8ee9ba742c6d8925dc79cff (patch)
tree8cc6627d9d4c74bd804f452378346bf904efa31c /config_file
parentRevise `get` section for readme docs (diff)
downloadconfig-file-3c1ebfaf0db4a29cc8ee9ba742c6d8925dc79cff.zip
Allow string lists and dicts to be auto parsed
Diffstat (limited to 'config_file')
-rw-r--r--config_file/parsers/parse_value.py61
-rw-r--r--config_file/utils.py8
2 files changed, 57 insertions, 12 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)