aboutsummaryrefslogtreecommitdiffstats
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
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
-rw-r--r--CHANGELOG.md15
-rw-r--r--config_file/config_file.py7
-rw-r--r--config_file/parsers/json_parser.py34
-rw-r--r--tests/test_json_parser.py4
4 files changed, 38 insertions, 22 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 901cd17..03402ea 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,21 @@ Changed
- `toml` and `pyyaml` are now optional extra dependencies. This allows you to
not have to install them if you aren't using them.
+
+ - `retrieve_all` in `JsonParser`'s `get` method is now called `get_all` for
+ consistency. It isn't publicly available yet. The thought is to write a custom
+ ini parser first that supports multiple of the same keys and subsections.
+ This way you can have the `get_all`, `set_all`, `delete_all`, etc. methods
+ for all file type parsers. I would have to weight if that added complexity
+ of not using the built-in configparser and roll our own for some added
+ features is worth it or see if there are ways to work around it with
+ configparser that are ideal.
+
+Fixed
+
+ - You can now specify a default and still coerce your return type. Previously,
+ if you specified a default, there was no logic in that branch to coerce your
+ return type as well.
## 0.9.0 - 2020-02-09
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)
diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py
index 0e91395..a5e38a1 100644
--- a/tests/test_json_parser.py
+++ b/tests/test_json_parser.py
@@ -162,7 +162,7 @@ def test_that_json_parser_can_delete_keys(key, test_json, expected_json):
)
def test_that_json_parser_can_retrieve_all_keys(test_json, key, expected_output):
parser = JsonParser(json.dumps(test_json))
- assert parser.get(key, retrieve_all=True) == expected_output
+ assert parser.get(key, get_all=True) == expected_output
@pytest.mark.parametrize(
@@ -179,7 +179,7 @@ def test_that_json_parser_can_retrieve_all_keys(test_json, key, expected_output)
def test_that_json_parser_can_set_all_keys(test_json, key, value, expected_output):
parser = JsonParser(json.dumps(test_json))
parser.set(key, value, set_all=True)
- assert parser.get(key, retrieve_all=True) == expected_output
+ assert parser.get(key, get_all=True) == expected_output
@pytest.mark.parametrize(