summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2023-06-10 16:49:00 -0500
committerJohn Estabrook <jestabro@vyos.io>2023-06-10 22:39:56 -0500
commit795b259b95a4c11f92c6c8aaceb5003c869f1b1a (patch)
tree6d7e0ddb4643ae5403780db8b0004b15942f0009
parent0598c1db1114d921a04d8ba251a51112a0e274f0 (diff)
downloadvyos-1x-795b259b95a4c11f92c6c8aaceb5003c869f1b1a.tar.gz
vyos-1x-795b259b95a4c11f92c6c8aaceb5003c869f1b1a.zip
vyos.utils: T5248: add util function: dict of list[str]|str -> list of paths
-rw-r--r--python/vyos/utils/dict.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/python/vyos/utils/dict.py b/python/vyos/utils/dict.py
index 7c93deef6..3faf5c596 100644
--- a/python/vyos/utils/dict.py
+++ b/python/vyos/utils/dict.py
@@ -234,6 +234,27 @@ def dict_to_list(d, save_key_to=None):
return collect
+def dict_to_paths(d: dict) -> list:
+ """ Generator to return list of paths from dict of list[str]|str
+ """
+ def func(d, path):
+ if isinstance(d, dict):
+ if not d:
+ yield path
+ for k, v in d.items():
+ for r in func(v, path + [k]):
+ yield r
+ elif isinstance(d, list):
+ for i in d:
+ for r in func(i, path):
+ yield r
+ elif isinstance(d, str):
+ yield path + [d]
+ else:
+ raise ValueError('object is not a dict of strings/list of strings')
+ for r in func(d, []):
+ yield r
+
def check_mutually_exclusive_options(d, keys, required=False):
""" Checks if a dict has at most one or only one of
mutually exclusive keys.