diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-02-02 14:32:59 -0600 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-02-02 14:32:59 -0600 |
| commit | df765605ad8295776e710afd305f848813ab0e43 (patch) | |
| tree | 9d38205653c83dbba880572216e63b6524b12e95 | |
| parent | Add type hints + contents is now content in BaseParser (diff) | |
| download | config-file-df765605ad8295776e710afd305f848813ab0e43.zip | |
Add working JSON parser
| -rw-r--r-- | CHANGELOG.md | 15 | ||||
| -rw-r--r-- | README.md | 7 | ||||
| -rw-r--r-- | config_file/config_file.py | 3 | ||||
| -rw-r--r-- | config_file/parsers/json_parser.py | 106 | ||||
| -rw-r--r-- | tests/test_config_file.py | 17 | ||||
| -rw-r--r-- | tests/test_json_parser.py | 90 |
6 files changed, 221 insertions, 17 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 34aeb50..0f27c0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +Added + + - More type hints were added to the `ConfigFile` object and `IniParser`. + - `nested_lookup` dependency to help with modifying deeply nested structures + (JSON + YAML) + - `JsonParser` so you can now specify `.json` files to be parsed. + +Changed + + - The original content of the passed in file is now called `content` + instead of `contents`. This is for consistency since the parsed version is + called `parsed_content`. + ## 0.7.0 - 2020-01-27 Fixed @@ -8,10 +8,13 @@ [](https://travis-ci.com/eugenetriguba/config_file) [](https://codecov.io/gh/eugenetriguba/config_file) -> This python package is currently a work in progress and is in a pre-alpha phase. The API is liable to break until v1. +> This python package is currently in rapid development and is in a pre-alpha phase. +> The API is liable to break until v1 so if you're going to use it, pinning the version +> is recommended. All notable changes are kept track of in the changelog. Config File allows you to use the same simple API for manipulating INI, JSON, -YAML, and TOML configuration files. For the time being, it only supports INI. +YAML, and TOML configuration files. For the time being, it only supports INI and +JSON. ## Installation ```bash diff --git a/config_file/config_file.py b/config_file/config_file.py index d5e23b6..d9f9a2e 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -4,6 +4,7 @@ from shutil import copyfile from config_file.parsers.base_parser import BaseParser from config_file.parsers.ini_parser import IniParser +from config_file.parsers.json_parser import JsonParser from config_file.utils import split_on_dot @@ -53,6 +54,8 @@ class ConfigFile: file_type = split_on_dot(file_path, only_last_dot=True)[-1] if file_type == "ini": return IniParser(self.contents) + elif file_type == "json": + return JsonParser(self.contents) else: raise ValueError( f"File path contains an unsupported or " diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py index c7c20fb..344ae7f 100644 --- a/config_file/parsers/json_parser.py +++ b/config_file/parsers/json_parser.py @@ -1,6 +1,11 @@ import json -from nested_lookup import get_occurrence_of_key +from nested_lookup import ( + get_occurrence_of_key, + nested_delete, + nested_lookup, + nested_update, +) from config_file.parsers.base_parser import BaseParser, ParsingError from config_file.parsers.parse_value import parse_value @@ -8,6 +13,39 @@ from config_file.utils import split_on_dot class JsonParser(BaseParser): + """ + The JsonParser allows use of json files with ConfigFile. The syntax it + supports is a bit more flexible than the IniParser. + + Since you can have deeply nested structures with JSON, the JsonParser supports + a catch all where you can get/set/delete all occurrences of a particular + key. This is the default behavior if you only specify a single word key. + + If there is a '.' in the key, it is assumed you want to use the dot syntax + then. + + If no '.' is in the key parameter and we have specified we're only + trying to fetch a top level item with 'top_level_only=True', the parser + only retrieve the key in the top most level. + + 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"] + """ + def __init__(self, file_contents: str): """Reads in the file contents into the parser.""" super().__init__(file_contents) @@ -18,20 +56,68 @@ class JsonParser(BaseParser): except json.decoder.JSONDecodeError as error: raise ParsingError(error) - def get(self, section_key, parse_type=True): - split_keys = split_on_dot(section_key) + def get(self, key: str, parse_type: bool = True, retrieve_all: bool = False): + """ + Retrieve values using a dot syntax. + + :param key: The key to retrieve. e.g. 'foo.bar.boo' + :param parse_type: Whether you'd like the type parsed into its native one. + :param retrieve_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. - result = self.parsed_content - for key in split_keys: - result = result[key] + :return: the value of the given key + """ + if retrieve_all: + result = nested_lookup(key, self.parsed_content) + elif "." in key: + split_keys = split_on_dot(key) + result = self.parsed_content + for key in split_keys: + result = result[key] + else: + result = self.parsed_content[key] return parse_value(result) if parse_type else result - def set(self, section_key, value): - pass + def set(self, key: str, value, set_all: bool = False): + if set_all: + nested_update(self.parsed_content, key, value, in_place=True) + elif "." in key: + indexes = split_on_dot(key) + indexes_length = len(indexes) + + ref = self.parsed_content + for index, key in enumerate(indexes): + if index == indexes_length - 1: + ref[key] = value + break + + ref = ref[key] + else: + self.parsed_content[key] = value + + return True + + def delete(self, key, delete_all: bool = False): + if delete_all: + nested_delete(self.parsed_content, key, in_place=True) + elif "." in key: + indexes = split_on_dot(key) + indexes_length = len(indexes) + + ref = self.parsed_content + for index, key in enumerate(indexes): + if index == indexes_length - 1: + del ref[key] + break + + ref = ref[key] + else: + del self.parsed_content[key] - def delete(self, section_key): - pass + return True def stringify(self) -> str: return json.dumps(self.parsed_content) diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 0e42d19..f57bd69 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -1,3 +1,4 @@ +import json from pathlib import Path import pytest @@ -91,6 +92,13 @@ def test_that_config_file_can_save(tmpdir, file_contents, file_name): "config.ini", "[calendar]\nsunday_index = 0\n\n", ), + ( + "calendar.sunday_index", + 0, + False, + "config.json", + json.dumps({"calendar": {"sunday_index": 0}}), + ), ], ) def test_that_config_file_can_get( @@ -108,6 +116,9 @@ def test_that_config_file_can_get( ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.sunday_index", True), ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.blah", False), ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar", True), + ("json", json.dumps({"calendar": {"sunday_index": 0}}), "calendar", True), + ("json", json.dumps({"calendar": {"sunday_index": 0}}), "blah", False), + ("json", json.dumps({"calendar": {"sunday_index": 0}}), "sunday_index", True), ], ) def test_that_config_file_can_find_sections_and_keys( @@ -128,6 +139,12 @@ def test_that_config_file_can_find_sections_and_keys( "[calendar]\nsunday_index = 0\n\n", "[calendar]\n\n", ), + ( + "calendar.sunday_index", + "config.json", + json.dumps({"calendar": {"sunday_index": 0}}), + json.dumps({"calendar": {}}), + ), ], ) def test_that_config_file_can_delete( diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py index 8ce8187..fd715a8 100644 --- a/tests/test_json_parser.py +++ b/tests/test_json_parser.py @@ -2,7 +2,19 @@ import json import pytest -from config_file.parsers.json_parser import JsonParser +from config_file.parsers.json_parser import JsonParser, ParsingError + + +@pytest.mark.parametrize("test_json", ["{'test': }", "[section]\nblah = value\n\n"]) +def test_invalid_json(test_json): + with pytest.raises(ParsingError): + JsonParser(test_json) + + +def test_that_json_parser_can_stringify(): + json_str = '{"test": 5, "blah": true}' + parser = JsonParser(json_str) + assert parser.stringify() == json_str @pytest.mark.parametrize( @@ -112,7 +124,75 @@ def test_that_json_parser_can_get_keys( assert parser.get(section_key, parse_type=parse_type) == expected_result -def test_that_json_parser_can_stringify(): - json_str = '{"test": 5, "blah": true}' - parser = JsonParser(json_str) - assert parser.stringify() == json_str +@pytest.mark.parametrize( + "key, value", [("zip", "zipzip"), ("foo.bar.foobar", 5), ("foo.baz", "bar")] +) +def test_that_json_parser_can_set_keys(key, value): + test_json = { + "foo": { + "baz": 5, + "boo": "4.4", + "bar": {"test_key": "foobar", "foobar": {"test": True}}, + }, + "bar": {"baz": 10}, + "zip": "piz", + } + + parser = JsonParser(json.dumps(test_json)) + parser.set(key, value) + assert parser.get(key) == value + + +@pytest.mark.parametrize( + "key, test_json, expected_json", + [ + ("foo", {"foo": "bar", "baz": 5}, {"baz": 5}), + ("foo.bar", {"foo": {"bar": 5}}, {"foo": {}}), + ], +) +def test_that_json_parser_can_delete_keys(key, test_json, expected_json): + parser = JsonParser(json.dumps(test_json)) + parser.delete(key) + assert parser.stringify() == json.dumps(expected_json) + + +@pytest.mark.parametrize( + "test_json, key, expected_output", + [({"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}}, "bar", [5, 10, 15])], +) +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 + + +@pytest.mark.parametrize( + "test_json, key, value, expected_output", + [ + ( + {"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}}, + "bar", + "new_value", + ["new_value", "new_value", "new_value"], + ) + ], +) +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 + + +@pytest.mark.parametrize( + "test_json, key, expected_output", + [ + ( + {"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}}, + "bar", + {"foo": {"baz": {"bam": {}}}}, + ) + ], +) +def test_that_json_parser_can_delete_all_keys(test_json, key, expected_output): + parser = JsonParser(json.dumps(test_json)) + parser.delete(key, delete_all=True) + assert parser.stringify() == json.dumps(expected_output) |