summaryrefslogtreecommitdiff
path: root/python/vyos
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2021-09-06 12:09:57 +0200
committerChristian Poessinger <christian@poessinger.com>2021-09-06 12:09:57 +0200
commitacc6e461a92b14091fef9f49514f26364579391d (patch)
tree51ce0e7efd5c3e202157f3373cda50051508aa2e /python/vyos
parent9d0c37fbbc91acc9f2c0f2abaab360479e451f0f (diff)
downloadvyos-1x-acc6e461a92b14091fef9f49514f26364579391d.tar.gz
vyos-1x-acc6e461a92b14091fef9f49514f26364579391d.zip
vyos.util: add function to search a key recursively in a dictionary
data = { 'interfaces': {'dummy': {'dum0': {'address': ['192.0.2.17/29']}}, 'ethernet': {'eth0': {'address': ['2001:db8::1/64', '192.0.2.1/29'], 'description': 'Test123', 'duplex': 'auto', 'hw_id': '00:00:00:00:00:01', 'speed': 'auto'}, 'eth1': {'address': ['192.0.2.9/29'], 'description': 'Test456', 'duplex': 'auto', 'hw_id': '00:00:00:00:00:02', 'speed': 'auto'}}} } dict_search_recursive(data, 'hw_id') will yield both '00:00:00:00:00:01' and '00:00:00:00:00:02' as generator object.
Diffstat (limited to 'python/vyos')
-rw-r--r--python/vyos/util.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 18b7f5fcb..b41c5b346 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -724,6 +724,23 @@ def dict_search_args(dict_object, *path):
dict_object = dict_object[item]
return dict_object
+def dict_search_recursive(dict_object, key):
+ """ Traverse a dictionary recurisvely and return the value of the key
+ we are looking for.
+
+ Thankfully copied from https://stackoverflow.com/a/19871956
+ """
+ if isinstance(dict_object, list):
+ for i in dict_object:
+ for x in dict_search_recursive(i, key):
+ yield x
+ elif isinstance(dict_object, dict):
+ if key in dict_object:
+ yield dict_object[key]
+ for j in dict_object.values():
+ for x in dict_search_recursive(j, key):
+ yield x
+
def get_interface_config(interface):
""" Returns the used encapsulation protocol for given interface.
If interface does not exist, None is returned.