diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-05 20:39:28 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-05 20:39:28 -0500 |
| commit | 59eeb1321fbad41dce7c971778e37ac1943fda3b (patch) | |
| tree | 0ccb143e1094784e9b149977d6f0cabf6f6220b2 /config_file | |
| parent | Add ConfigFilePath to refactor out file specific code (diff) | |
| download | config-file-59eeb1321fbad41dce7c971778e37ac1943fda3b.zip | |
Redo package to have a flat package structure
By having a flat package structure like this, it'll
avoid exposing packages and things we don't want to
be exposed.
Diffstat (limited to 'config_file')
| -rw-r--r-- | config_file/__init__.py | 16 | ||||
| -rw-r--r-- | config_file/abstract_parser.py (renamed from config_file/parsers/abstract_parser.py) | 0 | ||||
| -rw-r--r-- | config_file/base_parser.py (renamed from config_file/parsers/base_parser.py) | 73 | ||||
| -rw-r--r-- | config_file/config_file.py | 6 | ||||
| -rw-r--r-- | config_file/config_file_path.py | 21 | ||||
| -rw-r--r-- | config_file/default.py | 14 | ||||
| -rw-r--r-- | config_file/exceptions.py | 2 | ||||
| -rw-r--r-- | config_file/ini_parser.py (renamed from config_file/parsers/ini_parser.py) | 0 | ||||
| -rw-r--r-- | config_file/ini_parser_compat.py (renamed from config_file/parsers/ini_parser_compat.py) | 0 | ||||
| -rw-r--r-- | config_file/json_parser.py (renamed from config_file/parsers/json_parser.py) | 0 | ||||
| -rw-r--r-- | config_file/nested_lookup.py (renamed from config_file/nested_lookup/lookup_api.py) | 190 | ||||
| -rw-r--r-- | config_file/nested_lookup/__init__.py | 19 | ||||
| -rw-r--r-- | config_file/nested_lookup/nested_lookup.py | 178 | ||||
| -rw-r--r-- | config_file/parse_value.py (renamed from config_file/parsers/parse_value.py) | 0 | ||||
| -rw-r--r-- | config_file/parsers/__init__.py | 0 | ||||
| -rw-r--r-- | config_file/toml_parser.py (renamed from config_file/parsers/toml_parser.py) | 0 | ||||
| -rw-r--r-- | config_file/utils.py | 16 | ||||
| -rw-r--r-- | config_file/yaml_parser.py (renamed from config_file/parsers/yaml_parser.py) | 3 |
18 files changed, 280 insertions, 258 deletions
diff --git a/config_file/__init__.py b/config_file/__init__.py index 3825140..d78baaf 100644 --- a/config_file/__init__.py +++ b/config_file/__init__.py @@ -1,21 +1,9 @@ -from config_file.config_file import ConfigFile -from config_file.exceptions import ( - ConfigFileError, - MissingDependencyError, - MissingKeyError, - ParsingError, - UnrecognizedFileError, -) -from config_file.parsers.abstract_parser import AbstractParser +from .abstract_parser import AbstractParser +from .config_file import ConfigFile __version__ = "0.10.0" __all__ = [ "ConfigFile", "AbstractParser", - "ConfigFileError", - "ParsingError", - "UnrecognizedFileError", - "MissingDependencyError", - "MissingKeyError", ] diff --git a/config_file/parsers/abstract_parser.py b/config_file/abstract_parser.py index ed9430b..ed9430b 100644 --- a/config_file/parsers/abstract_parser.py +++ b/config_file/abstract_parser.py diff --git a/config_file/parsers/base_parser.py b/config_file/base_parser.py index c2a7d71..a987a55 100644 --- a/config_file/parsers/base_parser.py +++ b/config_file/base_parser.py @@ -1,52 +1,93 @@ from types import ModuleType -from typing import Any +from typing import Any, Type +from config_file.abstract_parser import AbstractParser 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: ModuleType, decode_error: Exception): + def __init__( + self, file_contents: str, module: ModuleType, decode_error: Type[Exception] + ): + """The BaseParser implements the AbstractParser for us by giving it any module. + + As long as the module has a `loads` and `dumps` method, the BaseParser can use + it. Every file is then represented and worked with as a dictionary. + + Args: + file_contents: The contents of the file to parse. + + module: The module to use to parse the files. + Must have a `loads` and `dumps` method. + + decode_error: The error the module raises when the + file cannot be decoded. + + Raises: + ParsingError: If the decode_error is raised. + """ 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: + def parse_file_contents(self, module: ModuleType, decode_error: Exception) -> dict: + """Parse the file contents by running the `loads` method on the module. + + Args: + module: The module to use to parse the file. + decode_error: The error that is raised from the module if the file + cannot be decoded. + + Raises: + ParsingError: If the decode_error is raised while running `loads`. + + Returns: + The parsed file as a dictionary. + """ try: return module.loads(self.file_contents) except decode_error as error: raise ParsingError(error) def reset_internal_contents(self, file_contents: str) -> None: + """Reset the file contents and parsed contents of the parser. + + Args: + file_contents: The new file contents. + + Raises: + ParsingError: If we're unable to parse the new file contents. + """ 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: + if "." not in search_key: + key_causing_error = search_key + return self.parsed_content[search_key] + else: + split_keys = split_on_dot(search_key) + + content_reference = self.parsed_content for key in split_keys: key_causing_error = key - result = result[key] + content_reference = content_reference[key] + except (KeyError, TypeError): - raise ParsingError( - f"Cannot `get` {search_key} because " + raise KeyError( + f"cannot `get` {search_key} because " f"{key_causing_error} is not subscriptable." ) - return result + return content_reference def set(self, key: str, value: Any) -> None: if "." not in key: diff --git a/config_file/config_file.py b/config_file/config_file.py index 3e8a806..d047560 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -3,11 +3,11 @@ from pathlib import Path from shutil import copyfile from typing import Any, Optional, Type, Union +from config_file.abstract_parser import AbstractParser from config_file.config_file_path import ConfigFilePath -from config_file.default import Default 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.parse_value import parse_value +from config_file.utils import Default class ConfigFile: diff --git a/config_file/config_file_path.py b/config_file/config_file_path.py index a40cc40..9f2b87d 100644 --- a/config_file/config_file_path.py +++ b/config_file/config_file_path.py @@ -3,12 +3,13 @@ from pathlib import Path as _Path_ from pathlib import _posix_flavour, _windows_flavour from typing import Type -from .exceptions import UnrecognizedFileError -from .parsers.abstract_parser import AbstractParser -from .parsers.ini_parser import IniParser -from .parsers.json_parser import JsonParser -from .parsers.toml_parser import TomlParser -from .parsers.yaml_parser import YamlParser +from config_file.abstract_parser import AbstractParser +from config_file.ini_parser import IniParser +from config_file.json_parser import JsonParser +from config_file.toml_parser import TomlParser +from config_file.yaml_parser import YamlParser + +from .exceptions import UnsupportedFileTypeError from .utils import split_on_dot @@ -20,7 +21,7 @@ class ConfigFilePath(_Path_): that should be use for the given file type. See https://codereview.stackexchange.com/questions/162426/subclassing-pathlib-path - for most details on subclassing the pathlib.Path object. + for more details on subclassing the pathlib.Path object. """ _flavour = _windows_flavour if os.name == "nt" else _posix_flavour @@ -57,14 +58,14 @@ class ConfigFilePath(_Path_): """Determine what parser should be used for this file. Raises: - UnrecognizedFileError: If the extension of the file + UnsupportedFileTypeError: If the extension of the file is not recognized. Returns: The instantiated parser that should be used for this file. """ if self.extension == "": - raise UnrecognizedFileError( + raise UnsupportedFileTypeError( "Tried to determine a parser to use, but the file at " f"{self} does not have an extension." ) @@ -78,7 +79,7 @@ class ConfigFilePath(_Path_): elif self.extension == "toml": return TomlParser(self.contents) else: - raise UnrecognizedFileError( + raise UnsupportedFileTypeError( f"File path at `{self}` contains an unrecognized file type." ) diff --git a/config_file/default.py b/config_file/default.py deleted file mode 100644 index 529021c..0000000 --- a/config_file/default.py +++ /dev/null @@ -1,14 +0,0 @@ -class Default: - """ - Default is used for the `default` value in ConfigFile's `get`. - - Previously, the default value for `get` was None. However, then - the user cannot have `get` return a default value of None. So this - class is used instead to get around that limitation. - """ - - def __init__(self, value): - self.value = value - - def __repr__(self): - return "Default Value: {} ({})".format(self.value, type(self.value)) diff --git a/config_file/exceptions.py b/config_file/exceptions.py index d1b466b..22ee70c 100644 --- a/config_file/exceptions.py +++ b/config_file/exceptions.py @@ -9,7 +9,7 @@ class MissingDependencyError(ConfigFileError): """ -class UnrecognizedFileError(ConfigFileError): +class UnsupportedFileTypeError(ConfigFileError): """ A missing file extension or a file type not supported. """ diff --git a/config_file/parsers/ini_parser.py b/config_file/ini_parser.py index 72974f1..72974f1 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/ini_parser.py diff --git a/config_file/parsers/ini_parser_compat.py b/config_file/ini_parser_compat.py index 9dfea2e..9dfea2e 100644 --- a/config_file/parsers/ini_parser_compat.py +++ b/config_file/ini_parser_compat.py diff --git a/config_file/parsers/json_parser.py b/config_file/json_parser.py index 5314cd7..5314cd7 100644 --- a/config_file/parsers/json_parser.py +++ b/config_file/json_parser.py diff --git a/config_file/nested_lookup/lookup_api.py b/config_file/nested_lookup.py index 886e8bf..40da123 100644 --- a/config_file/nested_lookup/lookup_api.py +++ b/config_file/nested_lookup.py @@ -1,7 +1,195 @@ import copy import warnings +from collections import defaultdict -from .nested_lookup import nested_lookup +__all__ = [ + "get_all_keys", + "get_occurrences_and_values", + "get_occurrence_of_value", + "get_occurrence_of_key", + "nested_update", + "nested_delete", + "nested_alter", + "nested_lookup", +] + + +values_list = [] + + +def nested_lookup(key, document, wild=False, with_keys=False): + """Lookup a key in a nested document, return a list of values""" + if with_keys: + d = defaultdict(list) + for k, v in _nested_lookup(key, document, wild=wild, with_keys=with_keys): + d[k].append(v) + return d + return list(_nested_lookup(key, document, wild=wild, with_keys=with_keys)) + + +def _is_case_insensitive_substring(a, b): + """return True if `a` is a case insensitive substring of `b`, else False""" + return str(a).lower() in str(b).lower() + + +def _nested_lookup(key, document, wild=False, with_keys=False): + """Lookup a key in a nested document, yield a value""" + if isinstance(document, list): + for d in document: + for result in _nested_lookup(key, d, wild=wild, with_keys=with_keys): + yield result + + if isinstance(document, dict): + for k, v in document.items(): + if key == k or (wild and _is_case_insensitive_substring(key, k)): + if with_keys: + yield k, v + else: + yield v + if isinstance(v, dict): + for result in _nested_lookup(key, v, wild=wild, with_keys=with_keys): + yield result + elif isinstance(v, list): + for d in v: + for result in _nested_lookup( + key, d, wild=wild, with_keys=with_keys + ): + yield result + + +def get_all_keys(dictionary): + """ + Method to get all keys from a nested dictionary as a List + Args: + dictionary: Nested dictionary + Returns: + List of keys in the dictionary + """ + result_list = [] + + def recrusion(document): + if isinstance(document, list): + for list_items in document: + recrusion(document=list_items) + elif isinstance(document, dict): + for key, value in document.items(): + result_list.append(key) + recrusion(document=value) + return + + recrusion(document=dictionary) + return result_list + + +def get_occurrence_of_key(dictionary, key): + """ + Method to get occurrence of a key in a nested dictionary + + Args: + dictionary: Nested dictionary + key: Key to search for the occurrences + Return: + Number of occurrence (Integer) + """ + return _get_occurrence(dictionary=dictionary, item="key", keyword=key) + + +def get_occurrences_and_values(items, value): + """ + Method to get occurrence of a value in a nested list of dictionary + + Args: + items: list of dictionary: Nested dictionary + value: Value to search for the occurrences + + Return: + Dict where the key is the value arg and his value is a new + dict with occurrences and values + """ + occurrences = {} + occurrence = 0 + value_list = [] + + for item in items: + occurrence_result, values = _get_occurrence_with_values( + dictionary=item, item="value", keyword=value + ) + occurrence = occurrence + occurrence_result + if occurrence_result: + value_list.extend(values) + + occurrences[value] = {"occurrences": occurrence, "values": value_list} + + return occurrences + + +def _get_occurrence_with_values(dictionary, item, keyword): + occurrence = [0] + + result_recursion = _recursion(dictionary, item, keyword, occurrence, True) + + global values_list + values_list = [] + + return occurrence[0], result_recursion + + +def get_occurrence_of_value(dictionary, value): + """ + Method to get occurrence of a value in a nested dictionary + + Args: + dictionary: Nested dictionary + value: Value to search for the occurrences + Return: + Number of occurrence (Integer) + """ + return _get_occurrence(dictionary=dictionary, item="value", keyword=value) + + +def _recursion(dictionary, item, keyword, occurrence, with_values=False): + + global values_list + + if item == "key": + if dictionary.get(keyword) is not None: + occurrence[0] += 1 + elif keyword in list(dictionary.values()): + occurrence[0] += list(dictionary.values()).count(keyword) + if with_values: + values_list.append(dictionary) + for key, value in dictionary.items(): + if isinstance(value, dict): + _recursion(value, item, keyword, occurrence, with_values) + elif isinstance(value, list): + for list_items in value: + if hasattr(list_items, "items"): + _recursion(list_items, item, keyword, occurrence, with_values) + elif list_items == keyword: + occurrence[0] += 1 if item == "value" else 0 + + if values_list: + return values_list + + +def _get_occurrence(dictionary, item, keyword): + """ + Method to get occurrence of a key or value in a nested dictionary + + Args: + dictionary: Nested dictionary + item: Mostly (key or value) + keyword: key word to find occurrence + Return: + Number of occurrence of the given keyword in the dict + """ + occurrence = [0] + _recursion(dictionary, item, keyword, occurrence) + + global values_list + values_list = [] + + return occurrence[0] def nested_delete(document, key, in_place=False): diff --git a/config_file/nested_lookup/__init__.py b/config_file/nested_lookup/__init__.py deleted file mode 100644 index 4493bdf..0000000 --- a/config_file/nested_lookup/__init__.py +++ /dev/null @@ -1,19 +0,0 @@ -from .lookup_api import nested_alter, nested_delete, nested_update -from .nested_lookup import ( - get_all_keys, - get_occurrence_of_key, - get_occurrence_of_value, - get_occurrences_and_values, - nested_lookup, -) - -__all__ = [ - "get_all_keys", - "get_occurrences_and_values", - "get_occurrence_of_value", - "get_occurrence_of_key", - "nested_update", - "nested_delete", - "nested_alter", - "nested_lookup", -] diff --git a/config_file/nested_lookup/nested_lookup.py b/config_file/nested_lookup/nested_lookup.py deleted file mode 100644 index 6f2e7f2..0000000 --- a/config_file/nested_lookup/nested_lookup.py +++ /dev/null @@ -1,178 +0,0 @@ -from collections import defaultdict - -values_list = [] - - -def nested_lookup(key, document, wild=False, with_keys=False): - """Lookup a key in a nested document, return a list of values""" - if with_keys: - d = defaultdict(list) - for k, v in _nested_lookup(key, document, wild=wild, with_keys=with_keys): - d[k].append(v) - return d - return list(_nested_lookup(key, document, wild=wild, with_keys=with_keys)) - - -def _is_case_insensitive_substring(a, b): - """return True if `a` is a case insensitive substring of `b`, else False""" - return str(a).lower() in str(b).lower() - - -def _nested_lookup(key, document, wild=False, with_keys=False): - """Lookup a key in a nested document, yield a value""" - if isinstance(document, list): - for d in document: - for result in _nested_lookup(key, d, wild=wild, with_keys=with_keys): - yield result - - if isinstance(document, dict): - for k, v in document.items(): - if key == k or (wild and _is_case_insensitive_substring(key, k)): - if with_keys: - yield k, v - else: - yield v - if isinstance(v, dict): - for result in _nested_lookup(key, v, wild=wild, with_keys=with_keys): - yield result - elif isinstance(v, list): - for d in v: - for result in _nested_lookup( - key, d, wild=wild, with_keys=with_keys - ): - yield result - - -def get_all_keys(dictionary): - """ - Method to get all keys from a nested dictionary as a List - Args: - dictionary: Nested dictionary - Returns: - List of keys in the dictionary - """ - result_list = [] - - def recrusion(document): - if isinstance(document, list): - for list_items in document: - recrusion(document=list_items) - elif isinstance(document, dict): - for key, value in document.items(): - result_list.append(key) - recrusion(document=value) - return - - recrusion(document=dictionary) - return result_list - - -def get_occurrence_of_key(dictionary, key): - """ - Method to get occurrence of a key in a nested dictionary - - Args: - dictionary: Nested dictionary - key: Key to search for the occurrences - Return: - Number of occurrence (Integer) - """ - return _get_occurrence(dictionary=dictionary, item="key", keyword=key) - - -def get_occurrences_and_values(items, value): - """ - Method to get occurrence of a value in a nested list of dictionary - - Args: - items: list of dictionary: Nested dictionary - value: Value to search for the occurrences - - Return: - Dict where the key is the value arg and his value is a new - dict with occurrences and values - """ - occurrences = {} - occurrence = 0 - value_list = [] - - for item in items: - occurrence_result, values = _get_occurrence_with_values( - dictionary=item, item="value", keyword=value - ) - occurrence = occurrence + occurrence_result - if occurrence_result: - value_list.extend(values) - - occurrences[value] = {"occurrences": occurrence, "values": value_list} - - return occurrences - - -def _get_occurrence_with_values(dictionary, item, keyword): - occurrence = [0] - - result_recursion = _recursion(dictionary, item, keyword, occurrence, True) - - global values_list - values_list = [] - - return occurrence[0], result_recursion - - -def get_occurrence_of_value(dictionary, value): - """ - Method to get occurrence of a value in a nested dictionary - - Args: - dictionary: Nested dictionary - value: Value to search for the occurrences - Return: - Number of occurrence (Integer) - """ - return _get_occurrence(dictionary=dictionary, item="value", keyword=value) - - -def _recursion(dictionary, item, keyword, occurrence, with_values=False): - - global values_list - - if item == "key": - if dictionary.get(keyword) is not None: - occurrence[0] += 1 - elif keyword in list(dictionary.values()): - occurrence[0] += list(dictionary.values()).count(keyword) - if with_values: - values_list.append(dictionary) - for key, value in dictionary.items(): - if isinstance(value, dict): - _recursion(value, item, keyword, occurrence, with_values) - elif isinstance(value, list): - for list_items in value: - if hasattr(list_items, "items"): - _recursion(list_items, item, keyword, occurrence, with_values) - elif list_items == keyword: - occurrence[0] += 1 if item == "value" else 0 - - if values_list: - return values_list - - -def _get_occurrence(dictionary, item, keyword): - """ - Method to get occurrence of a key or value in a nested dictionary - - Args: - dictionary: Nested dictionary - item: Mostly (key or value) - keyword: key word to find occurrence - Return: - Number of occurrence of the given keyword in the dict - """ - occurrence = [0] - _recursion(dictionary, item, keyword, occurrence) - - global values_list - values_list = [] - - return occurrence[0] diff --git a/config_file/parsers/parse_value.py b/config_file/parse_value.py index 12032d6..12032d6 100644 --- a/config_file/parsers/parse_value.py +++ b/config_file/parse_value.py diff --git a/config_file/parsers/__init__.py b/config_file/parsers/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/config_file/parsers/__init__.py +++ /dev/null diff --git a/config_file/parsers/toml_parser.py b/config_file/toml_parser.py index 1c7f8a7..1c7f8a7 100644 --- a/config_file/parsers/toml_parser.py +++ b/config_file/toml_parser.py diff --git a/config_file/utils.py b/config_file/utils.py index c6c3d24..3554c0e 100644 --- a/config_file/utils.py +++ b/config_file/utils.py @@ -20,3 +20,19 @@ def split_on_dot(line: Union[str, Path], only_last_dot=False) -> List[str]: raise ValueError(f"The given string does not have a dot to split on: {line}") return line.rsplit(".", 1) if only_last_dot else line.split(".") + + +class Default: + """ + Default is used for the `default` value in ConfigFile's `get`. + + Previously, the default value for `get` was None. However, then + the user cannot have `get` return a default value of None. So this + class is used instead to get around that limitation. + """ + + def __init__(self, value): + self.value = value + + def __repr__(self): + return "Default Value: {} ({})".format(self.value, type(self.value)) diff --git a/config_file/parsers/yaml_parser.py b/config_file/yaml_parser.py index babe873..fb42e08 100644 --- a/config_file/parsers/yaml_parser.py +++ b/config_file/yaml_parser.py @@ -5,9 +5,8 @@ try: except ImportError: YAML_AVAILABLE = False -from config_file.exceptions import MissingDependencyError - from .base_parser import BaseParser +from .exceptions import MissingDependencyError class YamlParser(BaseParser): |