aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-01-18 07:22:06 -0600
committerEugene Triguba <eugenetriguba@gmail.com>2020-01-18 07:22:06 -0600
commit8ebca5a2d34638efcf3866ea641bab854498c0e0 (patch)
treefa5ef49da1bdd1c76683f8d1fdd25eba7b207b00
parentAdd test for json parser (diff)
downloadconfig-file-8ebca5a2d34638efcf3866ea641bab854498c0e0.zip
Add start of json_parser implementaiton
-rw-r--r--config_file/parsers/json_parser.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py
new file mode 100644
index 0000000..504ff02
--- /dev/null
+++ b/config_file/parsers/json_parser.py
@@ -0,0 +1,43 @@
+import json
+
+from config_file.parsers.base_parser import BaseParser, ParsingError
+
+
+class JsonParser(BaseParser):
+ def __init__(self, file_contents: str):
+ """Reads in the file contents into the parser."""
+ super().__init__(file_contents)
+
+ def parse(self, file_contents: str):
+ try:
+ return json.loads(file_contents)
+ except json.decoder.JSONDecodeError as error:
+ raise ParsingError(error)
+
+ def get(self, section_key, parse_type=True):
+ pass
+
+ def set(self, section_key, value):
+ pass
+
+ def delete(self, section_key):
+ pass
+
+ def stringify(self) -> str:
+ 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