aboutsummaryrefslogtreecommitdiffstats
path: root/config_file
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-02-02 14:32:59 -0600
committerEugene Triguba <eugenetriguba@gmail.com>2020-02-02 14:32:59 -0600
commitdf765605ad8295776e710afd305f848813ab0e43 (patch)
tree9d38205653c83dbba880572216e63b6524b12e95 /config_file
parentAdd type hints + contents is now content in BaseParser (diff)
downloadconfig-file-df765605ad8295776e710afd305f848813ab0e43.zip
Add working JSON parser
Diffstat (limited to 'config_file')
-rw-r--r--config_file/config_file.py3
-rw-r--r--config_file/parsers/json_parser.py106
2 files changed, 99 insertions, 10 deletions
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)