aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-07-20 22:51:51 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-07-20 22:51:51 -0500
commit65d4715b0b2b9974d95ac36271caa8e177bc98fa (patch)
tree794b6338c8b0a654c4313a5da9713368fecfca7a
parentAdd toml and yaml parsers (diff)
downloadconfig-file-65d4715b0b2b9974d95ac36271caa8e177bc98fa.zip
Add BaseParser for all other parsers
This allows one implementation for all parsers, with the parsers overriding it for a custom one if needed for that parser.
-rw-r--r--config_file/parsers/base_parser.py86
-rw-r--r--config_file/parsers/ini_parser.py145
-rw-r--r--config_file/parsers/json_parser.py127
3 files changed, 130 insertions, 228 deletions
diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py
new file mode 100644
index 0000000..09fbbbf
--- /dev/null
+++ b/config_file/parsers/base_parser.py
@@ -0,0 +1,86 @@
+from typing import Any
+
+from config_file.exceptions import MissingKeyError, ParsingError
+from config_file.nested_lookup import get_occurrence_of_key
+from config_file.utils import split_on_dot
+
+from .abstract_parser import AbstractParser
+
+
+class BaseParser(AbstractParser):
+ def __init__(self, file_contents: str, module, decode_error):
+ super().__init__(file_contents)
+ self.__module = module
+ self.__decode_error = decode_error
+ self.parsed_content = self.parse_file_contents(module, decode_error)
+
+ def parse_file_contents(self, module, decode_error) -> Any:
+ try:
+ return module.loads(self.file_contents)
+ except decode_error as error:
+ raise ParsingError(error)
+
+ def reset_internal_contents(self, file_contents: str) -> None:
+ self.file_contents = file_contents
+ self.parsed_content = self.parse_file_contents(
+ self.__module, self.__decode_error
+ )
+
+ def get(self, search_key: str) -> Any:
+ if "." not in search_key:
+ result = self.parsed_content[search_key]
+ return result
+ else:
+ split_keys = split_on_dot(search_key)
+
+ result = self.parsed_content
+ key_causing_error = None
+ try:
+ for key in split_keys:
+ key_causing_error = key
+ result = result[key]
+ except TypeError:
+ raise ParsingError(
+ f"Cannot get {search_key} because "
+ f"{key_causing_error} is not subscriptable."
+ )
+
+ return result
+
+ def set(self, key: str, value: Any) -> None:
+ if "." not in key:
+ self.parsed_content[key] = value
+ return
+
+ keys = split_on_dot(key)
+ content_reference = self.parsed_content
+ for index, key in enumerate(keys):
+ if index == len(keys) - 1:
+ content_reference[key] = value
+ return
+
+ content_reference = content_reference[key]
+
+ def delete(self, key) -> None:
+ try:
+ if "." not in key:
+ del self.parsed_content[key]
+ else:
+ 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]
+ except KeyError:
+ raise MissingKeyError(f"The specified key '{key}' was not found.")
+
+ def stringify(self) -> str:
+ return self.__module.dumps(self.parsed_content)
+
+ def has(self, search_key: str) -> bool:
+ return get_occurrence_of_key(self.parsed_content, key=search_key) > 0
diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py
index 164ff6d..2a29ba7 100644
--- a/config_file/parsers/ini_parser.py
+++ b/config_file/parsers/ini_parser.py
@@ -1,124 +1,53 @@
import configparser
from io import StringIO
-from config_file.exceptions import ParsingError
-from config_file.parsers.abstract_parser import AbstractParser
-from config_file.parsers.parse_value import parse_value
-from config_file.utils import split_on_dot
+from .base_parser import BaseParser
-class IniParser(AbstractParser):
- def __init__(self, file_contents: str):
- """Reads in the file contents into the configparser."""
- super().__init__(file_contents)
-
- def parse(self, file_contents: str):
- try:
- parser = configparser.ConfigParser()
- parser.read_string(file_contents)
- return parser
- except configparser.Error as error:
- raise ParsingError(error)
-
- def get(self, section_key: str, parse_types: bool = False):
- """
- Read the value of `section.key` of the config file.
-
- :param section_key: The section and key to read from in the config file.
- e.g. 'section1.key2'
- :param parse_types: Coerces the return value to its native type.
- :return: The value of the key, parsed to its native type if parse_types is True.
-
- :raises ValueError: if the section_key given is not in a dot format. e.g.
- 'section1.key'
- :raises ParsingError: if the specified `section.key` is not found, in an
- invalid format, or if we are unable to coerce the return value to value_type.
- """
- if "." not in section_key:
- return self.__retrieve_section(section_key, parse_types)
-
- try:
- section, key = split_on_dot(section_key, only_last_dot=True)
- value = self.parsed_content.get(section, key)
- return parse_value(value) if parse_types else value
- except configparser.Error as error:
- raise ParsingError(error.message)
-
- def __retrieve_section(self, section, parse_types):
- items = dict(self.parsed_content.items(section))
-
- if not parse_types:
- return items
-
- for item in items:
- items[item] = parse_value(items[item])
-
- return items
-
- def set(self, section_key: str, value) -> bool:
- """
- Sets the value of 'section.key' of the config file. If the specified section
- is not in the configuration file, it will be created before adding the key
- to it.
+class IniParserCompatibility:
+ """
+ Provides compatibility with the json, yaml,
+ and toml API so we can use the BaseParser
+ easily, while still being able to leverage
+ the built-in configparser to parse the file.
- :param section_key: The key to set from the config file. e.g. 'section1.key'
+ It is used as the `module` parameter
+ for BaseParser, which requires us to have a
+ `dumps` and `loads` method.
+ """
- :param value: The value to set the key to. It can be any type that can
- be converted to a string.
+ @staticmethod
+ def loads(file_contents: str) -> dict:
+ parser = configparser.ConfigParser()
+ parser.read_string(file_contents)
- :return: True if the setting was successful.
+ return IniParserCompatibility.__create_configparser_dict(parser)
- :raises ValueError: If there is no dot (.) in section_key
- """
- section, key = split_on_dot(section_key, only_last_dot=True)
-
- if value is not None and not self.parsed_content.has_section(section):
- self.parsed_content.add_section(section)
-
- if not isinstance(value, str):
- value = str(value)
-
- self.parsed_content.set(section, key, value)
- return True
-
- def delete(self, section_key: str) -> bool:
- """
- Deletes a key or an entire section.
-
- :param section_key: The key to delete from the config file.
- e.g. 'ocr.engine'. If no dot (.) is present, it will assume you are trying
- to delete the entire section.
-
- :return: True if the deletion succeeded.
+ @staticmethod
+ def dumps(file_contents: dict) -> str:
+ parser = configparser.ConfigParser()
+ buffer = StringIO()
- :raise ValueError: If the section or key does not exist in the config file.
- """
- if "." not in section_key:
- if not self.parsed_content.has_section(section_key):
- raise ValueError(
- f"Cannot delete {section_key} because it is not in the config file."
- )
+ parser.read_dict(file_contents)
+ parser.write(buffer)
- self.parsed_content.remove_section(section_key)
- return True
+ return buffer.getvalue()
- section, key = split_on_dot(section_key, only_last_dot=True)
- if key not in self.parsed_content[section]:
- raise ValueError(
- f"Cannot delete {section}.{key} because {key} is not in {section}."
- )
+ @staticmethod
+ def __create_configparser_dict(parser: configparser.ConfigParser) -> dict:
+ result = {}
+ items = dict(parser.items())
- self.parsed_content.remove_option(section, key)
- return True
+ for section in parser.sections():
+ result.update({str(section): dict(items[section])})
- def stringify(self) -> str:
- buffer = StringIO()
- self.parsed_content.write(buffer)
- return buffer.getvalue()
+ return result
- def has(self, section_key: str) -> bool:
- if "." not in section_key:
- return self.parsed_content.has_section(section_key)
- section, key = split_on_dot(section_key, only_last_dot=True)
- return self.parsed_content.has_option(section, key)
+class IniParser(BaseParser):
+ def __init__(self, file_contents: str):
+ super().__init__(
+ file_contents=file_contents,
+ module=IniParserCompatibility,
+ decode_error=configparser.Error,
+ )
diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py
index 237c0b5..5314cd7 100644
--- a/config_file/parsers/json_parser.py
+++ b/config_file/parsers/json_parser.py
@@ -1,128 +1,15 @@
import json
-from config_file.exceptions import ParsingError
-from config_file.nested_lookup import (
- get_occurrence_of_key,
- nested_delete,
- nested_lookup,
- nested_update,
-)
-from config_file.parsers import AbstractParser, parse_value
-from config_file.utils import split_on_dot
+from .base_parser import BaseParser
-class JsonParser(AbstractParser):
- """
- The JsonParser allows use of json files with ConfigFile. The syntax it
- supports is a bit more flexible than the IniParser.
-
- By default, the dot syntax is used. However, if you'd like to get/set/delete all
- of a particular key, you can specify so with an optional parameter.
-
- 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', all=True)
- >>> [{ "key2": "foo", "key1": "bar" }, "bar"]
- """
-
+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, 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
-
- key_causing_error = None
- try:
- for key in split_keys:
- key_causing_error = key
- result = result[key]
- except TypeError:
- raise ParsingError(
- f"Cannot get {key} because {key_causing_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
+ super().__init__(
+ file_contents=file_contents,
+ module=json,
+ decode_error=json.decoder.JSONDecodeError,
+ )
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