Debian Codegraph @

tests/utils.py

raw symbols SCIP
1 from __future__ import annotations
2
3 from packaging import version
4
5
6 def find_recursive(list_or_single_dict: list[dict] | dict, wanted: list[dict] | dict) -> bool:
7 """
8 Check if the 'wanted' item(s) sub-dictionary structure is/are present in any of the dictionaries in 'list_or_single_dict'.
9 'list_or_single_dict' can be either a list of dictionaries or a single dictionary.
10 """
11
12 def matches_subdict(haystack: dict, needle: dict) -> bool:
13 """
14 Check if all key-value pairs in needle are present in haystack.
15 This function recursively checks nested dictionaries and lists of dictionaries.
16 """
17 for key, needle_value in needle.items():
18 if key not in haystack:
19 return False
20
21 haystack_value = haystack[key]
22
23 if isinstance(needle_value, dict):
24 if not isinstance(haystack_value, dict) or not matches_subdict(haystack_value, needle_value):
25 return False
26 elif isinstance(needle_value, list):
27 if not all(
28 any(matches_subdict(haystack_item, needle_item) for haystack_item in haystack_value)
29 for needle_item in needle_value
30 ):
31 return False
32 else:
33 if haystack_value != needle_value:
34 return False
35
36 return True
37
38 # Turn both args into lists to handle single/multi lookup
39 if isinstance(list_or_single_dict, dict):
40 list_or_single_dict = [list_or_single_dict]
41
42 if isinstance(wanted, dict):
43 wanted = [wanted]
44
45 return all(any(matches_subdict(item, single_wanted) for item in list_or_single_dict) for single_wanted in wanted)
46
47
48 def should_skip(version_str: str, comparator: str, ref_version_str: str) -> bool:
49 v = version.parse(version_str)
50 ref_version = version.parse(ref_version_str)
51 if (
52 (comparator == "<=" and v > ref_version) # pylint: disable=too-many-boolean-expressions
53 or (comparator == ">=" and v < ref_version)
54 or (comparator == "<" and v >= ref_version)
55 or (comparator == ">" and v <= ref_version)
56 ):
57 print(f"Requires version {comparator} {ref_version_str}. Got {v}")
58 return True
59
60 return False