From ea795c03f1580b979737efa475a1058a79007fef Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 18 Jan 2020 07:20:20 -0600 Subject: Add test for json parser --- tests/test_json_parser.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/test_json_parser.py diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py new file mode 100644 index 0000000..ad5c053 --- /dev/null +++ b/tests/test_json_parser.py @@ -0,0 +1,64 @@ +import json + +import pytest + +from config_file.parsers.json_parser import JsonParser + +test_json = { + "glossary": { + "title": "example glossary", + "GlossDiv": { + "title": "S", + "GlossList": { + "GlossEntry": { + "ID": "SGML", + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "GlossDef": { + "para": "A meta-markup language, used to create markup " + + "languages such as DocBook.", + "GlossSeeAlso": ["GML", "XML"], + }, + "GlossSee": "markup", + } + }, + }, + } +} + + +@pytest.mark.parametrize( + "test_input,expected_result", + [ + ("glossary", True), + ("title", True), + ("example glossary", False), + ("GlossDiv", True), + ("title", True), + ("S", False), + ("GlossList", True), + ("GlossEntry", True), + ("ID", True), + ("SGML", False), + ("GlossTerm", True), + ("Standard Generalized Markup Language", False), + ("Acronym", True), + ("ISO 8879:1986", False), + ("GlossDef", True), + ("para", True), + ( + "A meta-markup language, used to create markup languages such as DocBook.", + False, + ), + ("GlossSeeAlso", True), + ("GML", False), + ("XML", False), + ("GlossSee", True), + ("markup", False), + ], +) +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 -- cgit v1.2.3 From 8ebca5a2d34638efcf3866ea641bab854498c0e0 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 18 Jan 2020 07:22:06 -0600 Subject: Add start of json_parser implementaiton --- config_file/parsers/json_parser.py | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 config_file/parsers/json_parser.py 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 -- cgit v1.2.3 From 52b122d2207926c0ad5a2c0111dc5da70b233309 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 +++-------------- poetry.lock | 18 ++++++++++++++++-- pyproject.toml | 3 ++- tests/test_json_parser.py | 6 ++++++ 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 @@ -229,6 +229,17 @@ optional = false 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" @@ -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 -- cgit v1.2.3