summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2020-08-23 13:05:03 +0200
committerChristian Poessinger <christian@poessinger.com>2020-08-23 13:05:03 +0200
commit02e995265a4548aac7a990ae3e176f993b2f5a17 (patch)
treecc01a8e27d36ccd542b2e22cab7f8453e6bdd8ec
parent86ed6b5ebfd43f3baac3becd0c0524577812d11d (diff)
downloadvyos-1x-02e995265a4548aac7a990ae3e176f993b2f5a17.tar.gz
vyos-1x-02e995265a4548aac7a990ae3e176f993b2f5a17.zip
vyos.util: T2755: add vyos_dict_search() to traverse a dictionary
This is faster implementation then using jmespath.search('foo.bar', dict).
-rw-r--r--python/vyos/util.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py
index d27a8a3e7..4cc25764b 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -672,3 +672,17 @@ def find_device_file(device):
return os.path.join(root, basename)
return None
+
+def vyos_dict_search(dict, path):
+ """ Traverse Python dictionary (dict) delimited by dot (.).
+ Return value of key if found, None otherwise.
+
+ This is faster implementation then jmespath.search('foo.bar', dict)"""
+ parts = path.split('.')
+ inside = parts[:-1]
+ if not inside:
+ return dict[path]
+ c = dict
+ for p in parts[:-1]:
+ c = c.get(p, {})
+ return c.get(parts[-1], None)