Alex Goodman 1230650771
allow for java manifest data to be optional
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
2020-10-29 11:28:27 -04:00

22 lines
716 B
Python

def dig(target, *keys, **kwargs):
"""
Traverse a nested set of dictionaries, tuples, or lists similar to ruby's dig function.
"""
end_of_chain = target
for key in keys:
if isinstance(end_of_chain, dict) and key in end_of_chain:
end_of_chain = end_of_chain[key]
elif isinstance(end_of_chain, (list, tuple)) and isinstance(key, int):
end_of_chain = end_of_chain[key]
else:
if 'fail' in kwargs and kwargs['fail'] is True:
if isinstance(end_of_chain, dict):
raise KeyError
else:
raise IndexError
else:
return None
return end_of_chain