diff options
Diffstat (limited to 'config_file')
| -rw-r--r-- | config_file/nested-lookup/.gitignore | 11 | ||||
| -rw-r--r-- | config_file/nested-lookup/.travis.yml | 9 | ||||
| -rw-r--r-- | config_file/nested-lookup/CONTRIBUTING.rst | 7 | ||||
| -rw-r--r-- | config_file/nested-lookup/nested_lookup/__init__.py | 8 | ||||
| -rw-r--r-- | config_file/nested-lookup/nested_lookup/lookup_api.py | 214 | ||||
| -rw-r--r-- | config_file/nested-lookup/nested_lookup/nested_lookup.py | 182 | ||||
| -rw-r--r-- | config_file/nested_lookup/.gitignore | 11 | ||||
| -rw-r--r-- | config_file/nested_lookup/.travis.yml | 9 | ||||
| -rw-r--r-- | config_file/nested_lookup/CONTRIBUTING.rst | 7 | ||||
| -rw-r--r-- | config_file/nested_lookup/__init__.py | 6 | ||||
| -rw-r--r-- | config_file/nested_lookup/lookup_api.py | 16 | ||||
| -rw-r--r-- | config_file/nested_lookup/nested_lookup.py | 8 | ||||
| -rw-r--r-- | config_file/nested_lookup/nested_lookup/__init__.py | 8 | ||||
| -rw-r--r-- | config_file/nested_lookup/nested_lookup/lookup_api.py | 214 | ||||
| -rw-r--r-- | config_file/nested_lookup/nested_lookup/nested_lookup.py | 182 | ||||
| -rw-r--r-- | config_file/parsers/json_parser.py | 12 | ||||
| -rw-r--r-- | config_file/parsers/toml_parser.py | 105 |
17 files changed, 126 insertions, 883 deletions
diff --git a/config_file/nested-lookup/.gitignore b/config_file/nested-lookup/.gitignore deleted file mode 100644 index c9bd7c4..0000000 --- a/config_file/nested-lookup/.gitignore +++ /dev/null @@ -1,11 +0,0 @@ -__pycache__* -.pytest_cache* -*.pyc -.coverage -build/* -dist/* -nested_lookup.egg-info/* -.cache -.vscode -git_commit.sh -venv diff --git a/config_file/nested-lookup/.travis.yml b/config_file/nested-lookup/.travis.yml deleted file mode 100644 index 5551955..0000000 --- a/config_file/nested-lookup/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -language: python -python: - - "2.7" - - "3.6" -# command to install dependencies -install: - - pip install . -# command to run tests -script: pytest
\ No newline at end of file diff --git a/config_file/nested-lookup/CONTRIBUTING.rst b/config_file/nested-lookup/CONTRIBUTING.rst deleted file mode 100644 index dde9f01..0000000 --- a/config_file/nested-lookup/CONTRIBUTING.rst +++ /dev/null @@ -1,7 +0,0 @@ -The ``nested-lookup`` library is public domain and open source. - -To contribute please make sure all tests pass. - -If you are adding new functionality please write a unit test. - -To avoid discussions regarding code formatting, please use `black <https://github.com/ambv/black>`_ with default settings. diff --git a/config_file/nested-lookup/nested_lookup/__init__.py b/config_file/nested-lookup/nested_lookup/__init__.py deleted file mode 100644 index 0840df6..0000000 --- a/config_file/nested-lookup/nested_lookup/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -from .nested_lookup import ( - nested_lookup, - get_all_keys, - get_occurrence_of_key, - get_occurrence_of_value, - get_occurrences_and_values -) -from .lookup_api import nested_update, nested_delete, nested_alter diff --git a/config_file/nested-lookup/nested_lookup/lookup_api.py b/config_file/nested-lookup/nested_lookup/lookup_api.py deleted file mode 100644 index a0af63a..0000000 --- a/config_file/nested-lookup/nested_lookup/lookup_api.py +++ /dev/null @@ -1,214 +0,0 @@ -import copy -import warnings -from six import iteritems -from nested_lookup import nested_lookup - - -def nested_delete(document, key, in_place=False): - if not in_place: - document = copy.deepcopy(document) - return _nested_delete(document=document, key=key) - - -def _nested_delete(document, key): - """ - Method to delete a key->value pair from a nested document - Args: - document: Might be List of Dicts (or) Dict of Lists (or) - Dict of List of Dicts etc... - key: Key to delete - Return: - Returns a document that includes everything but the given key - """ - if isinstance(document, list): - for list_items in document: - _nested_delete(document=list_items, key=key) - elif isinstance(document, dict): - if document.get(key): - del document[key] - for dict_key, dict_value in iteritems(document): - _nested_delete(document=dict_value, key=key) - return document - - -def nested_update(document, key, value, in_place=False, treat_as_element=True): - """ - Method to update a key->value pair in a nested document - Args: - document: Might be List of Dicts (or) Dict of Lists (or) - Dict of List of Dicts etc... - key: Key to update the value - value: Value to set - in_place (bool): - True: modify the dict in place; - False: create a deep copy of the dict and modify it - Defaults to False - treat_list_element (bool): - True: if a list is provided as "value", the function trys - to match the list elements to the occurences of the key. - If the key occures more often than the provided list has - elements, the first element gets recycled. - False: the provided list is treated as one scalar value and - will be set as value to every key that matches. - Defaults to True (because of backwards portability of the package). - Return: - Returns a document that has updated key, value pair. - """ - - # check if a list or scalar value is provided and create a list - # from the scalar value - # check the length of the list and provide it to _nested_update - if not treat_as_element and not isinstance(value, list): - raise Exception( - "The value must be a list when treat_as_element is False." - ) - elif treat_as_element: - value = [value] - - if not in_place: - document = copy.deepcopy(document) - return _nested_update(document=document, key=key, value=value) - - -def _nested_update(document, key, value): - """ - Method to update a key->value pair in a nested document. - If the number of passed values is less than the number of key matches - when scanning for updates, use last value as default. - Args: - document: Might be List of Dicts (or) Dict of Lists (or) - Dict of List of Dicts etc... - key (str): Key to update the value - value (list): value(s) which should be used for replacement purpouse - Return: - Returns a document that has updated key, value pair. - """ - if isinstance(document, list): - for list_items in document: - _nested_update( - document=list_items, key=key, value=value - ) - elif isinstance(document, dict): - for dict_key, dict_value in iteritems(document): - if dict_key == key: - document[key] = value[0] - if len(value) > 1: - value.pop(0) - _nested_update( - document=dict_value, key=key, value=value - ) - return document - - -def nested_alter( - document, - key, - callback_function=None, - function_parameters=None, - conversion_function=None, - wild_alter=False, - in_place=False, -): - """ - Method to alter all values of the occurences of the key "key". - The provided callback_function is used to alter the scalar values - Args: - document: Might be List of Dicts (or) Dict of Lists (or) - Dict of List of Dicts etc... - key: Key to update the value - callback_function :A callback function which alters a scalar value - HINT: You should be aware that not every element might be of - the same type, please check this in your function! - function_parameters (list): - If the callback_function has additional input arguments except - the scalar value, please specify those in this list. - conversion_function: A conversion function like str() which should be - applied to every found value before it is passed to the - "callback_function" - wild_alter: Find matching elements via wild-match by the given keys - and alter those. - HINT: Keep in mind that the wild-match might return unexpected types! - in_place (bool): - True: modify the dict in place; - False: create a deep copy of the dict and modify it - Defaults to False - Return: - Returns a document that has updated key, value pair. - """ - # check if a list or scalar value is provided and create a list from - # the scalar value - # check the length of the list and provide it to _nested_update - if isinstance(key, list): - key_len = len(key) - else: - key = [key] - key_len = len(key) - - if not in_place: - document = copy.deepcopy(document) - return _nested_alter( - document=document, - keys=key, - callback_function=callback_function, - function_parameters=function_parameters, - conversion_function=conversion_function, - wild_alter=wild_alter, - in_place=in_place, - key_len=key_len, - ) - - -def _call_callback( - value_list, callback_function, function_parameters, conversion_function -): - """ - internal helper to call the callback function - """ - return_list = [] - # loop over all values - for value in value_list: - # apply the conversion function - if conversion_function is not None: - value = conversion_function(value) - # if functions arguments are present, expand the list to variables - # via the magic operator * - if function_parameters: - trans_val = callback_function(value, *function_parameters) - else: - trans_val = callback_function(value) - # append the transformed element to the list - return_list.append(trans_val) - return return_list - - -def _nested_alter( - document, - keys, - callback_function, - function_parameters, - conversion_function, - wild_alter, - in_place, - key_len, -): - - # return data if no callback_function is provided - if callback_function is None: - warnings.warn("Please provide a callback_function to nested_alter().") - return document - - # iterate over all given keys in the list - for key in keys: - # try to find the key: - findings = nested_lookup(key, document, with_keys=True, wild=wild_alter) - for k, v in findings.items(): - trans_val = _call_callback( - v, callback_function, function_parameters, conversion_function - ) - # use the transformed value and apply the update to the key - # (dont treat the lists as elements here) - document = nested_update( - document, k, trans_val, in_place=in_place, treat_as_element=False - ) - - return document diff --git a/config_file/nested-lookup/nested_lookup/nested_lookup.py b/config_file/nested-lookup/nested_lookup/nested_lookup.py deleted file mode 100644 index 0b11b4d..0000000 --- a/config_file/nested-lookup/nested_lookup/nested_lookup.py +++ /dev/null @@ -1,182 +0,0 @@ -from six import iteritems - -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 iteritems(document): - 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 iteritems(document): - 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 iteritems(dictionary): - 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/nested_lookup/.gitignore b/config_file/nested_lookup/.gitignore deleted file mode 100644 index c9bd7c4..0000000 --- a/config_file/nested_lookup/.gitignore +++ /dev/null @@ -1,11 +0,0 @@ -__pycache__* -.pytest_cache* -*.pyc -.coverage -build/* -dist/* -nested_lookup.egg-info/* -.cache -.vscode -git_commit.sh -venv diff --git a/config_file/nested_lookup/.travis.yml b/config_file/nested_lookup/.travis.yml deleted file mode 100644 index 5551955..0000000 --- a/config_file/nested_lookup/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -language: python -python: - - "2.7" - - "3.6" -# command to install dependencies -install: - - pip install . -# command to run tests -script: pytest
\ No newline at end of file diff --git a/config_file/nested_lookup/CONTRIBUTING.rst b/config_file/nested_lookup/CONTRIBUTING.rst deleted file mode 100644 index dde9f01..0000000 --- a/config_file/nested_lookup/CONTRIBUTING.rst +++ /dev/null @@ -1,7 +0,0 @@ -The ``nested-lookup`` library is public domain and open source. - -To contribute please make sure all tests pass. - -If you are adding new functionality please write a unit test. - -To avoid discussions regarding code formatting, please use `black <https://github.com/ambv/black>`_ with default settings. diff --git a/config_file/nested_lookup/__init__.py b/config_file/nested_lookup/__init__.py index 0840df6..768facc 100644 --- a/config_file/nested_lookup/__init__.py +++ b/config_file/nested_lookup/__init__.py @@ -5,4 +5,8 @@ from .nested_lookup import ( get_occurrence_of_value, get_occurrences_and_values ) -from .lookup_api import nested_update, nested_delete, nested_alter +from .lookup_api import ( + nested_update, + nested_delete, + nested_alter +) diff --git a/config_file/nested_lookup/lookup_api.py b/config_file/nested_lookup/lookup_api.py index a0af63a..e103534 100644 --- a/config_file/nested_lookup/lookup_api.py +++ b/config_file/nested_lookup/lookup_api.py @@ -1,7 +1,7 @@ import copy import warnings -from six import iteritems -from nested_lookup import nested_lookup + +from .nested_lookup import nested_lookup def nested_delete(document, key, in_place=False): @@ -26,7 +26,7 @@ def _nested_delete(document, key): elif isinstance(document, dict): if document.get(key): del document[key] - for dict_key, dict_value in iteritems(document): + for dict_key, dict_value in document.items(): _nested_delete(document=dict_value, key=key) return document @@ -34,16 +34,21 @@ def _nested_delete(document, key): def nested_update(document, key, value, in_place=False, treat_as_element=True): """ Method to update a key->value pair in a nested document + Args: document: Might be List of Dicts (or) Dict of Lists (or) Dict of List of Dicts etc... + key: Key to update the value + value: Value to set + in_place (bool): True: modify the dict in place; False: create a deep copy of the dict and modify it Defaults to False - treat_list_element (bool): + + treat_as_element (bool): True: if a list is provided as "value", the function trys to match the list elements to the occurences of the key. If the key occures more often than the provided list has @@ -51,6 +56,7 @@ def nested_update(document, key, value, in_place=False, treat_as_element=True): False: the provided list is treated as one scalar value and will be set as value to every key that matches. Defaults to True (because of backwards portability of the package). + Return: Returns a document that has updated key, value pair. """ @@ -89,7 +95,7 @@ def _nested_update(document, key, value): document=list_items, key=key, value=value ) elif isinstance(document, dict): - for dict_key, dict_value in iteritems(document): + for dict_key, dict_value in document.items(): if dict_key == key: document[key] = value[0] if len(value) > 1: diff --git a/config_file/nested_lookup/nested_lookup.py b/config_file/nested_lookup/nested_lookup.py index 0b11b4d..9454a44 100644 --- a/config_file/nested_lookup/nested_lookup.py +++ b/config_file/nested_lookup/nested_lookup.py @@ -1,5 +1,3 @@ -from six import iteritems - from collections import defaultdict @@ -29,7 +27,7 @@ def _nested_lookup(key, document, wild=False, with_keys=False): yield result if isinstance(document, dict): - for k, v in iteritems(document): + for k, v in document.items(): if key == k or (wild and _is_case_insensitive_substring(key, k)): if with_keys: yield k, v @@ -61,7 +59,7 @@ def get_all_keys(dictionary): for list_items in document: recrusion(document=list_items) elif isinstance(document, dict): - for key, value in iteritems(document): + for key, value in document.items(): result_list.append(key) recrusion(document=value) return @@ -148,7 +146,7 @@ def _recursion(dictionary, item, keyword, occurrence, with_values=False): occurrence[0] += list(dictionary.values()).count(keyword) if with_values: values_list.append(dictionary) - for key, value in iteritems(dictionary): + for key, value in dictionary.items(): if isinstance(value, dict): _recursion(value, item, keyword, occurrence, with_values) elif isinstance(value, list): diff --git a/config_file/nested_lookup/nested_lookup/__init__.py b/config_file/nested_lookup/nested_lookup/__init__.py deleted file mode 100644 index 0840df6..0000000 --- a/config_file/nested_lookup/nested_lookup/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -from .nested_lookup import ( - nested_lookup, - get_all_keys, - get_occurrence_of_key, - get_occurrence_of_value, - get_occurrences_and_values -) -from .lookup_api import nested_update, nested_delete, nested_alter diff --git a/config_file/nested_lookup/nested_lookup/lookup_api.py b/config_file/nested_lookup/nested_lookup/lookup_api.py deleted file mode 100644 index a0af63a..0000000 --- a/config_file/nested_lookup/nested_lookup/lookup_api.py +++ /dev/null @@ -1,214 +0,0 @@ -import copy -import warnings -from six import iteritems -from nested_lookup import nested_lookup - - -def nested_delete(document, key, in_place=False): - if not in_place: - document = copy.deepcopy(document) - return _nested_delete(document=document, key=key) - - -def _nested_delete(document, key): - """ - Method to delete a key->value pair from a nested document - Args: - document: Might be List of Dicts (or) Dict of Lists (or) - Dict of List of Dicts etc... - key: Key to delete - Return: - Returns a document that includes everything but the given key - """ - if isinstance(document, list): - for list_items in document: - _nested_delete(document=list_items, key=key) - elif isinstance(document, dict): - if document.get(key): - del document[key] - for dict_key, dict_value in iteritems(document): - _nested_delete(document=dict_value, key=key) - return document - - -def nested_update(document, key, value, in_place=False, treat_as_element=True): - """ - Method to update a key->value pair in a nested document - Args: - document: Might be List of Dicts (or) Dict of Lists (or) - Dict of List of Dicts etc... - key: Key to update the value - value: Value to set - in_place (bool): - True: modify the dict in place; - False: create a deep copy of the dict and modify it - Defaults to False - treat_list_element (bool): - True: if a list is provided as "value", the function trys - to match the list elements to the occurences of the key. - If the key occures more often than the provided list has - elements, the first element gets recycled. - False: the provided list is treated as one scalar value and - will be set as value to every key that matches. - Defaults to True (because of backwards portability of the package). - Return: - Returns a document that has updated key, value pair. - """ - - # check if a list or scalar value is provided and create a list - # from the scalar value - # check the length of the list and provide it to _nested_update - if not treat_as_element and not isinstance(value, list): - raise Exception( - "The value must be a list when treat_as_element is False." - ) - elif treat_as_element: - value = [value] - - if not in_place: - document = copy.deepcopy(document) - return _nested_update(document=document, key=key, value=value) - - -def _nested_update(document, key, value): - """ - Method to update a key->value pair in a nested document. - If the number of passed values is less than the number of key matches - when scanning for updates, use last value as default. - Args: - document: Might be List of Dicts (or) Dict of Lists (or) - Dict of List of Dicts etc... - key (str): Key to update the value - value (list): value(s) which should be used for replacement purpouse - Return: - Returns a document that has updated key, value pair. - """ - if isinstance(document, list): - for list_items in document: - _nested_update( - document=list_items, key=key, value=value - ) - elif isinstance(document, dict): - for dict_key, dict_value in iteritems(document): - if dict_key == key: - document[key] = value[0] - if len(value) > 1: - value.pop(0) - _nested_update( - document=dict_value, key=key, value=value - ) - return document - - -def nested_alter( - document, - key, - callback_function=None, - function_parameters=None, - conversion_function=None, - wild_alter=False, - in_place=False, -): - """ - Method to alter all values of the occurences of the key "key". - The provided callback_function is used to alter the scalar values - Args: - document: Might be List of Dicts (or) Dict of Lists (or) - Dict of List of Dicts etc... - key: Key to update the value - callback_function :A callback function which alters a scalar value - HINT: You should be aware that not every element might be of - the same type, please check this in your function! - function_parameters (list): - If the callback_function has additional input arguments except - the scalar value, please specify those in this list. - conversion_function: A conversion function like str() which should be - applied to every found value before it is passed to the - "callback_function" - wild_alter: Find matching elements via wild-match by the given keys - and alter those. - HINT: Keep in mind that the wild-match might return unexpected types! - in_place (bool): - True: modify the dict in place; - False: create a deep copy of the dict and modify it - Defaults to False - Return: - Returns a document that has updated key, value pair. - """ - # check if a list or scalar value is provided and create a list from - # the scalar value - # check the length of the list and provide it to _nested_update - if isinstance(key, list): - key_len = len(key) - else: - key = [key] - key_len = len(key) - - if not in_place: - document = copy.deepcopy(document) - return _nested_alter( - document=document, - keys=key, - callback_function=callback_function, - function_parameters=function_parameters, - conversion_function=conversion_function, - wild_alter=wild_alter, - in_place=in_place, - key_len=key_len, - ) - - -def _call_callback( - value_list, callback_function, function_parameters, conversion_function -): - """ - internal helper to call the callback function - """ - return_list = [] - # loop over all values - for value in value_list: - # apply the conversion function - if conversion_function is not None: - value = conversion_function(value) - # if functions arguments are present, expand the list to variables - # via the magic operator * - if function_parameters: - trans_val = callback_function(value, *function_parameters) - else: - trans_val = callback_function(value) - # append the transformed element to the list - return_list.append(trans_val) - return return_list - - -def _nested_alter( - document, - keys, - callback_function, - function_parameters, - conversion_function, - wild_alter, - in_place, - key_len, -): - - # return data if no callback_function is provided - if callback_function is None: - warnings.warn("Please provide a callback_function to nested_alter().") - return document - - # iterate over all given keys in the list - for key in keys: - # try to find the key: - findings = nested_lookup(key, document, with_keys=True, wild=wild_alter) - for k, v in findings.items(): - trans_val = _call_callback( - v, callback_function, function_parameters, conversion_function - ) - # use the transformed value and apply the update to the key - # (dont treat the lists as elements here) - document = nested_update( - document, k, trans_val, in_place=in_place, treat_as_element=False - ) - - return document diff --git a/config_file/nested_lookup/nested_lookup/nested_lookup.py b/config_file/nested_lookup/nested_lookup/nested_lookup.py deleted file mode 100644 index 0b11b4d..0000000 --- a/config_file/nested_lookup/nested_lookup/nested_lookup.py +++ /dev/null @@ -1,182 +0,0 @@ -from six import iteritems - -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 iteritems(document): - 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 iteritems(document): - 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 iteritems(dictionary): - 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/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 |