diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/vyos/util.py | 31 | 
1 files changed, 24 insertions, 7 deletions
| diff --git a/python/vyos/util.py b/python/vyos/util.py index 93a2f6640..b41c5b346 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -692,21 +692,21 @@ def find_device_file(device):      return None -def dict_search(path, my_dict): -    """ Traverse Python dictionary (my_dict) delimited by dot (.). +def dict_search(path, dict_object): +    """ Traverse Python dictionary (dict_object) delimited by dot (.).      Return value of key if found, None otherwise. -    This is faster implementation then jmespath.search('foo.bar', my_dict)""" -    if not isinstance(my_dict, dict) or not path: +    This is faster implementation then jmespath.search('foo.bar', dict_object)""" +    if not isinstance(dict_object, dict) or not path:          return None      parts = path.split('.')      inside = parts[:-1]      if not inside: -        if path not in my_dict: +        if path not in dict_object:              return None -        return my_dict[path] -    c = my_dict +        return dict_object[path] +    c = dict_object      for p in parts[:-1]:          c = c.get(p, {})      return c.get(parts[-1], None) @@ -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. | 
