authentik.lib.utils.dict
1from typing import Any 2 3 4def get_path_from_dict(root: dict, path: str, sep=".", default=None) -> Any: 5 """Recursively walk through `root`, checking each part of `path` separated by `sep`. 6 If at any point a dict does not exist, return default""" 7 walk: Any = root 8 for comp in path.split(sep): 9 if walk and comp in walk: 10 walk = walk.get(comp) 11 else: 12 return default 13 return walk 14 15 16def set_path_in_dict(root: dict, path: str, value: Any, sep="."): 17 """Recursively walk through `root`, checking each part of `path` separated by `sep` 18 and setting the last value to `value`""" 19 # Walk each component of the path 20 path_parts = path.split(sep) 21 for comp in path_parts[:-1]: 22 if comp not in root: 23 root[comp] = {} 24 root = root.get(comp, {}) 25 root[path_parts[-1]] = value 26 27 28def delete_path_in_dict(root: dict, path: str, sep="."): 29 """Recursively walk through `root`, checking each part of `path` separated by `sep` 30 and delete the last value""" 31 # Walk each component of the path 32 path_parts = path.split(sep) 33 for comp in path_parts[:-1]: 34 if comp not in root: 35 return 36 root = root.get(comp, {}) 37 last_path_part = path_parts[-1] 38 if last_path_part in root: 39 del root[last_path_part]
def
get_path_from_dict(root: dict, path: str, sep='.', default=None) -> Any:
5def get_path_from_dict(root: dict, path: str, sep=".", default=None) -> Any: 6 """Recursively walk through `root`, checking each part of `path` separated by `sep`. 7 If at any point a dict does not exist, return default""" 8 walk: Any = root 9 for comp in path.split(sep): 10 if walk and comp in walk: 11 walk = walk.get(comp) 12 else: 13 return default 14 return walk
Recursively walk through root, checking each part of path separated by sep.
If at any point a dict does not exist, return default
def
set_path_in_dict(root: dict, path: str, value: Any, sep='.'):
17def set_path_in_dict(root: dict, path: str, value: Any, sep="."): 18 """Recursively walk through `root`, checking each part of `path` separated by `sep` 19 and setting the last value to `value`""" 20 # Walk each component of the path 21 path_parts = path.split(sep) 22 for comp in path_parts[:-1]: 23 if comp not in root: 24 root[comp] = {} 25 root = root.get(comp, {}) 26 root[path_parts[-1]] = value
Recursively walk through root, checking each part of path separated by sep
and setting the last value to value
def
delete_path_in_dict(root: dict, path: str, sep='.'):
29def delete_path_in_dict(root: dict, path: str, sep="."): 30 """Recursively walk through `root`, checking each part of `path` separated by `sep` 31 and delete the last value""" 32 # Walk each component of the path 33 path_parts = path.split(sep) 34 for comp in path_parts[:-1]: 35 if comp not in root: 36 return 37 root = root.get(comp, {}) 38 last_path_part = path_parts[-1] 39 if last_path_part in root: 40 del root[last_path_part]
Recursively walk through root, checking each part of path separated by sep
and delete the last value