From e4b5dc647cbc560f5e9e1fa492b5653ca37a241c Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 27 Jan 2020 20:34:16 -0600 Subject: 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. --- config_file/parsers/json_parser.py | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) (limited to 'config_file') 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 -- cgit v1.2.3