-
Notifications
You must be signed in to change notification settings - Fork 6
Next round of refactor + typing fixes. #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,56 @@ | ||
| """Library wrapper for output parsing.""" | ||
| import re | ||
| from typing import Mapping, List, Union | ||
| from typing import Mapping, List, Union, Any | ||
| import jmespath | ||
| from .utils.jmspath_parsers import jmspath_value_parser, jmspath_refkey_parser | ||
| from .utils.filter_parsers import exclude_filter | ||
| from .utils.refkey import keys_cleaner, keys_values_zipper, associate_key_of_my_value | ||
| from .utils.flatten import flatten_list | ||
|
|
||
|
|
||
| def extract_values_from_output(value: Mapping, path: Mapping, exclude: List) -> Union[Mapping, List, int, str, bool]: | ||
| """Return data from output depending on the check path. See unit text for complete example.""" | ||
| # Get the wanted values to be evaluated if jmspath expression is defined, otherwise | ||
| # use the entire output if jmespath is not defined in check. This cover the "raw" diff type. | ||
| if path and not exclude: | ||
| wanted_value = jmespath.search(jmspath_value_parser(path), value) | ||
|
|
||
| elif path and exclude: | ||
| wanted_value = jmespath.search(jmspath_value_parser(path), value) | ||
| exclude_filter(wanted_value, exclude) | ||
| elif not path and exclude: | ||
| exclude_filter(value, exclude) | ||
| return value | ||
|
|
||
| # data type check | ||
| if path: | ||
| if not any(isinstance(i, list) for i in wanted_value): | ||
| return wanted_value | ||
|
|
||
| for element in wanted_value: | ||
| for item in element: | ||
| if isinstance(item, dict): | ||
| raise TypeError( | ||
| f'Must be list of lists i.e. [["Idle", 75759616], ["Idle", 75759620]]. You have {wanted_value}\'.' | ||
| ) | ||
| if isinstance(item, list): | ||
| wanted_value = flatten_list(wanted_value) | ||
| break | ||
|
|
||
| paired_key_value = associate_key_of_my_value(jmspath_value_parser(path), wanted_value) | ||
| else: | ||
| paired_key_value = value | ||
|
|
||
| if path and re.search(r"\$.*\$", path): | ||
| wanted_reference_keys = jmespath.search(jmspath_refkey_parser(path), value) | ||
| def extract_values_from_output(output: Union[Mapping, List], path: str, exclude: List = None) -> Any: | ||
| """Return data from output depending on the check path. See unit test for complete example. | ||
| Get the wanted values to be evaluated if JMESPath expression is defined, | ||
| otherwise use the entire output if jmespath is not defined in check. This covers the "raw" diff type. | ||
| Exclude data not desired to compare. | ||
| Notes: | ||
| https://jmespath.org/ shows how JMESPath works. | ||
| Args: | ||
| output: json data structure | ||
| path: JMESPath to extract specific values | ||
| exclude: list of keys to exclude | ||
| Returns: | ||
| Evaluated data, may be anything depending on JMESPath used. | ||
| """ | ||
| if exclude: | ||
| exclude_filter(output, exclude) # exclude unwanted elements | ||
|
|
||
| if not path: | ||
| return output # return if path is not specified | ||
|
|
||
| values = jmespath.search(jmspath_value_parser(path), output) | ||
|
|
||
| if not any(isinstance(i, list) for i in values): # check for multi-nested lists if not found return here | ||
| return values | ||
|
|
||
| for element in values: # process elements to check is lists should be flatten | ||
| for item in element: | ||
| if isinstance(item, dict): # raise if there is a dict, path must be more specific to extract data | ||
| raise TypeError( | ||
| f'Must be list of lists i.e. [["Idle", 75759616], ["Idle", 75759620]].' f"You have {values}'." | ||
| ) | ||
| if isinstance(item, list): | ||
| values = flatten_list(values) # flatten list and rewrite values | ||
| break # items are the same, need to check only first to see if this is a nested list | ||
|
|
||
| paired_key_value = associate_key_of_my_value(jmspath_value_parser(path), values) | ||
|
|
||
| if re.search(r"\$.*\$", path): # normalize | ||
| wanted_reference_keys = jmespath.search(jmspath_refkey_parser(path), output) | ||
| list_of_reference_keys = keys_cleaner(wanted_reference_keys) | ||
| return keys_values_zipper(list_of_reference_keys, paired_key_value) | ||
|
|
||
| return paired_key_value | ||
| return values | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.