Using python3.4 I'm trying to filter a firewall rulebase exported as JSON and converted to a dictionary. I want to filter based on user defined criteria but I can't seem to be able to do that without difficult to read constructs with a lot of loops and if statements.
The dict from my firewall entries is quite large so I've shortened them a litte.
2 examples:
entry1 = {'action': 'accept',
'dstintf': [{'name': 'ZN_HDW', 'q_origin_key': 'ZN_HDW'}],
'srcintf': [{'name': 'ZN_AUDIT', 'q_origin_key': 'ZN_AUDIT'}]
}
entry2 = {'action': 'accept',
'dstintf': [{'name': 'ZN_HDW', 'q_origin_key': 'ZN_HDW'}],
'srcintf': [{'name': 'ZN_DMZ', 'q_origin_key': 'ZN_DMZ'},
{'name': 'ZN_MGMT', 'q_origin_key': 'ZN_MGMT'},
{'name': 'ZN_AUDIT', 'q_origin_key': 'ZN_AUDIT'}]
}
I wanted to create a data structure to compare against the 2 examples and created to following:
filter = {'action': 'accept',
'srcintf': [{'name': 'ZN_AUDIT', 'q_origin_key': 'ZN_AUDIT'}]
}
After some searching on how to compare these structures I ended up with some readable code. My problem is it doesn't evaluate to true on entry2 (which has multiple source interfaces):
>>> filter.items() <= entry1.items()
True
>>> filter.items() <= entry2.items()
False
Any tips on how I should do this?
EDIT: Using the answer Eric Duminil below I'm able to create something (see below), although it's still not as readable as I want it. Any further tips?
example = entry2
# Compare entry to filter
noMatch = 0
for key in filter:
if isinstance(example[key], list):
# Convert list of dicts to list for easier comparing
tmpExample = [d['name'] for d in example[key]]
# Break if entry does not contain all criteria
if not all(value in tmpExample for value in filter[key]):
noMatch = 1
print("No match on: " + str(filter[key]))
break
elif filter[key] != example[key]:
# Simple string comparing
noMatch = 1
print("No match on: " + str(filter[key]))
break
if noMatch == 0:
print("Match")
else:
print(" No match")