aboutsummaryrefslogtreecommitdiffstats
path: root/config_file
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-03-29 19:53:20 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-03-29 19:53:20 -0500
commitbddd5d9cb7b81727fa6f8a8ff080d788fc5653af (patch)
treec25d3caaff68ba33f35ae3a721f5ff727d4e471a /config_file
parentFix lock file and add extras to CI (diff)
downloadconfig-file-bddd5d9cb7b81727fa6f8a8ff080d788fc5653af.zip
retrieve_all is now get_all and fixed bug with coerceing default return
type
Diffstat (limited to 'config_file')
-rw-r--r--config_file/config_file.py7
-rw-r--r--config_file/parsers/json_parser.py34
2 files changed, 21 insertions, 20 deletions
diff --git a/config_file/config_file.py b/config_file/config_file.py
index aae3b74..91202da 100644
--- a/config_file/config_file.py
+++ b/config_file/config_file.py
@@ -81,13 +81,14 @@ class ConfigFile:
:return: The value of the key.
"""
- if default is not None:
+ if default is None:
+ key_value = self.parser.get(key, parse_types=parse_types)
+ else:
try:
- return self.parser.get(key, parse_types=parse_types)
+ key_value = self.parser.get(key, parse_types=parse_types)
except ParsingError:
return default
- key_value = self.parser.get(key, parse_types=parse_types)
return return_type(key_value) if return_type else key_value
def set(self, key: str, value):
diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py
index 67ddc11..20eb085 100644
--- a/config_file/parsers/json_parser.py
+++ b/config_file/parsers/json_parser.py
@@ -22,20 +22,20 @@ class JsonParser(BaseParser):
Example:
- >>> json = {
- >>> "key1": {
- >>> "key2": "foo",
- >>> "key1": "bar"
- >>> },
- >>> "foo": "bar"
- >>> }
- >>> parser = JsonParser(json.dumps(json))
- >>> parser.get('key1')
- { "key2": "foo", "key1": "bar" }
- >>> parser.get('key1.key2')
- "foo"
- >>> parser.get('key1', retrieve_all=True)
- [{ "key2": "foo", "key1": "bar" }, "bar"]
+ json = {
+ "key1": {
+ "key2": "foo",
+ "key1": "bar"
+ },
+ "foo": "bar"
+ }
+ parser = JsonParser(json.dumps(json))
+ parser.get('key1')
+ >>> { "key2": "foo", "key1": "bar" }
+ parser.get('key1.key2')
+ >>> "foo"
+ parser.get('key1', get_all=True)
+ >>> [{ "key2": "foo", "key1": "bar" }, "bar"]
"""
def __init__(self, file_contents: str):
@@ -48,7 +48,7 @@ class JsonParser(BaseParser):
except json.decoder.JSONDecodeError as error:
raise ParsingError(error)
- def get(self, key: str, parse_types: bool = False, retrieve_all: bool = False):
+ def get(self, key: str, parse_types: bool = False, get_all: bool = False):
"""
Retrieve values using a dot syntax.
@@ -56,14 +56,14 @@ class JsonParser(BaseParser):
:param parse_types: Whether you'd like the type parsed into its native one.
- :param retrieve_all: Specify whether you'd like to recursively receive
+ :param get_all: Specify whether you'd like to recursively receive
all values that match the given key. Returned as a list.
e.g. 'foo' would retrieve all values that have the key 'foo', anywhere
in the json.
:return: the value of the given key
"""
- if retrieve_all:
+ if get_all:
result = nested_lookup(key, self.parsed_content)
elif "." in key:
split_keys = split_on_dot(key)