filter_struct(): Add support for invert arg
Added support for invert argument to filter_struct() function:
- invert: Invert the results. Instead of including only matching entries in
the results, include only NON-matching entries in the results.
Change-Id: If1e854f5f360384965917fde40f0036cfb22eba7
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/lib/var_funcs.py b/lib/var_funcs.py
index 889a5f7..35fb594 100644
--- a/lib/var_funcs.py
+++ b/lib/var_funcs.py
@@ -824,7 +824,7 @@
return True
-def filter_struct(structure, filter_dict, regex=False):
+def filter_struct(structure, filter_dict, regex=False, invert=False):
r"""
Filter the structure by removing any entries that do NOT contain the
keys/values specified in filter_dict and return the result.
@@ -888,6 +888,10 @@
regex Indicates whether the values in the
filter_dict should be interpreted as
regular expressions.
+ invert Invert the results. Instead of including
+ only matching entries in the results,
+ include only NON-matching entries in the
+ results.
"""
# Convert filter_dict from a string containing a python object definition
@@ -900,7 +904,7 @@
if type(structure) is list:
result = []
for element in structure:
- if match_struct(element, filter_dict, regex):
+ if match_struct(element, filter_dict, regex) != invert:
result.append(element)
else:
try:
@@ -908,7 +912,7 @@
except AttributeError:
result = DotDict()
for struct_key, struct_value in structure.items():
- if match_struct(struct_value, filter_dict, regex):
+ if match_struct(struct_value, filter_dict, regex) != invert:
result[struct_key] = struct_value
return result