aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-01-27 20:34:16 -0600
committerEugene Triguba <eugenetriguba@gmail.com>2020-01-27 20:35:36 -0600
commite4b5dc647cbc560f5e9e1fa492b5653ca37a241c (patch)
treedc3f0884b0869737d608c29bb7b937426a025df7
parentAdd start of json_parser implementaiton (diff)
downloadconfig-file-e4b5dc647cbc560f5e9e1fa492b5653ca37a241c.zip
Add nested_lookup, and get test working for json has() and stringify()
nested_lookup is a dependency that will help working with XML, YAML, and JSON since we can search deeply nested structures easily with it.
-rw-r--r--config_file/parsers/json_parser.py17
-rw-r--r--poetry.lock18
-rw-r--r--pyproject.toml3
-rw-r--r--tests/test_json_parser.py6
4 files changed, 27 insertions, 17 deletions
diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py
index 504ff02..c017b37 100644
--- a/config_file/parsers/json_parser.py
+++ b/config_file/parsers/json_parser.py
@@ -1,5 +1,7 @@
import json
+from nested_lookup import get_occurrence_of_key
+
from config_file.parsers.base_parser import BaseParser, ParsingError
@@ -27,17 +29,4 @@ class JsonParser(BaseParser):
return json.dumps(self.parsed_content)
def has(self, search_key: str) -> bool:
- return self._search(self.parsed_content, search_key)
-
- def _search(self, data: dict, search_item: str):
- for key, value in data.items():
- if search_item in data:
- return True
- elif not isinstance(value, dict):
- continue
- elif search_item in value:
- return True
- else:
- self._search(value, search_item)
-
- return False
+ return get_occurrence_of_key(self.parsed_content, key=search_key) > 0
diff --git a/poetry.lock b/poetry.lock
index af4cd16..87af5f0 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -230,6 +230,17 @@ python-versions = ">=3.5"
version = "8.1.0"
[[package]]
+category = "main"
+description = "Python functions for working with deeply nested documents (lists and dicts)"
+name = "nested-lookup"
+optional = false
+python-versions = "*"
+version = "0.2.19"
+
+[package.dependencies]
+six = "*"
+
+[[package]]
category = "dev"
description = "Node.js virtual environment builder"
name = "nodeenv"
@@ -397,7 +408,7 @@ version = "1.9.4"
"aspy.refactor-imports" = "*"
[[package]]
-category = "dev"
+category = "main"
description = "Python 2 and 3 compatibility utilities"
name = "six"
optional = false
@@ -440,7 +451,7 @@ more-itertools = "*"
docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"]
[metadata]
-content-hash = "b85256d1b940200e79b69f7af58bd0c8137a2942f8ff6772b5ac60dcf7555441"
+content-hash = "ab716116b837537d4ad85fb75967837df60685eca3f171d9d8745e34989a84dd"
python-versions = "^3.6"
[metadata.files]
@@ -559,6 +570,9 @@ more-itertools = [
{file = "more-itertools-8.1.0.tar.gz", hash = "sha256:c468adec578380b6281a114cb8a5db34eb1116277da92d7c46f904f0b52d3288"},
{file = "more_itertools-8.1.0-py3-none-any.whl", hash = "sha256:1a2a32c72400d365000412fe08eb4a24ebee89997c18d3d147544f70f5403b39"},
]
+nested-lookup = [
+ {file = "nested-lookup-0.2.19.tar.gz", hash = "sha256:5383ee1911a8257b217084eedfc31248f634c2c62b17bee8fc87fece1af3b2e4"},
+]
nodeenv = [
{file = "nodeenv-1.3.4-py2.py3-none-any.whl", hash = "sha256:561057acd4ae3809e665a9aaaf214afff110bbb6a6d5c8a96121aea6878408b3"},
]
diff --git a/pyproject.toml b/pyproject.toml
index 47f0c57..fb31e3a 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -20,6 +20,7 @@ classifiers = [
python = "^3.6"
toml = "^0.10.0"
pyyaml = "^5.2"
+nested-lookup = "^0.2.19"
[tool.poetry.dev-dependencies]
pre-commit = "^1.20"
@@ -43,7 +44,7 @@ include = '\.pyi?$'
line_length = 88
multi_line_output = 3
include_trailing_comma = true
-known_third_party = ["pytest"]
+known_third_party = ["nested_lookup", "pytest"]
known_first_party = ["config_file"]
[build-system]
diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py
index ad5c053..b829b7d 100644
--- a/tests/test_json_parser.py
+++ b/tests/test_json_parser.py
@@ -62,3 +62,9 @@ test_json = {
def test_that_json_parser_can_find_keys(test_input, expected_result):
parser = JsonParser(json.dumps(test_json))
assert parser.has(test_input) == expected_result
+
+
+def test_that_json_parser_can_stringify():
+ json_str = '{"test": 5, "blah": true}'
+ parser = JsonParser(json_str)
+ assert parser.stringify() == json_str