aboutsummaryrefslogtreecommitdiffstats
path: root/config_file/parsers
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-07-19 13:17:09 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-07-19 13:17:09 -0500
commitbc1280444ea3d5d4e5b5afd4e39bfb9365689393 (patch)
treeff467903e14b9b66b4644fd86edc81d046187264 /config_file/parsers
parentAdd more info on installation with poetry and zsh (diff)
downloadconfig-file-bc1280444ea3d5d4e5b5afd4e39bfb9365689393.zip
Add nested-lookup to project
russellballestrini is a python project in the public domain. Rather than installing it as a dependency, this adds it as part of the config-file package now, which allows more control.
Diffstat (limited to 'config_file/parsers')
-rw-r--r--config_file/parsers/json_parser.py12
-rw-r--r--config_file/parsers/toml_parser.py105
2 files changed, 107 insertions, 10 deletions
diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py
index 584a2d0..13b84b5 100644
--- a/config_file/parsers/json_parser.py
+++ b/config_file/parsers/json_parser.py
@@ -1,6 +1,6 @@
import json
-from nested_lookup import (
+from config_file.nested_lookup import (
get_occurrence_of_key,
nested_delete,
nested_lookup,
@@ -8,8 +8,7 @@ from nested_lookup import (
)
from config_file.exceptions import ParsingError
-from config_file.parsers.base_parser import BaseParser
-from config_file.parsers.parse_value import parse_value
+from config_file.parsers import BaseParser, parse_value
from config_file.utils import split_on_dot
@@ -70,15 +69,14 @@ class JsonParser(BaseParser):
split_keys = split_on_dot(key)
result = self.parsed_content
- # Used so we can more precisely specify what key is causing the error.
- key_for_error = None
+ key_causing_error = None
try:
for key in split_keys:
- key_for_error = key
+ key_causing_error = key
result = result[key]
except TypeError:
raise ParsingError(
- f"Cannot get {key} because {key_for_error} is not subscriptable."
+ f"Cannot get {key} because {key_causing_error} is not subscriptable."
)
else:
result = self.parsed_content[key]
diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py
index 64ab216..ad3e7f4 100644
--- a/config_file/parsers/toml_parser.py
+++ b/config_file/parsers/toml_parser.py
@@ -1,6 +1,105 @@
-from config_file.parsers import BaseParser
-
import toml
+from config_file.nested_lookup import (
+ get_occurrence_of_key,
+ nested_delete,
+ nested_lookup,
+ nested_update,
+)
+from config_file.exceptions import ParsingError
+from config_file.parsers import BaseParser, parse_value
+from config_file.utils import split_on_dot
+
+
class TomlParser(BaseParser):
- pass \ No newline at end of file
+ """The TomlParser allows use of toml files with ConfigFile."""
+
+ 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 toml.loads(file_contents)
+ except toml.TomlDecodeError as error:
+ raise ParsingError(error)
+
+ def get(self, key: str, parse_types: bool = False, all: bool = False):
+ """
+ Retrieve values using a dot syntax.
+
+ :param key: The key to retrieve. e.g. 'foo.bar.boo'
+
+ :param parse_types: Whether you'd like the type parsed into its native one.
+
+ :param 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.
+
+ :return: the value of the given key
+ """
+ if all:
+ result = nested_lookup(key, self.parsed_content)
+ elif "." in key:
+ split_keys = split_on_dot(key)
+ result = self.parsed_content
+
+ # Used so we can more precisely specify what key is causing the error.
+ key_for_error = None
+ try:
+ for key in split_keys:
+ key_for_error = key
+ result = result[key]
+ except TypeError:
+ raise ParsingError(
+ f"Cannot get {key} because {key_for_error} is not subscriptable."
+ )
+ else:
+ result = self.parsed_content[key]
+
+ return parse_value(result) if parse_types else result
+
+ def set(self, key: str, value, all: bool = False):
+ if 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, all: bool = False):
+ if 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]
+
+ return True
+
+ def stringify(self) -> str:
+ return json.dumps(self.parsed_content, indent=4)
+
+ def has(self, search_key: str) -> bool:
+ return get_occurrence_of_key(self.parsed_content, key=search_key) > 0