2020-02-04 09:33:20 +00:00
|
|
|
from benedict.utils import type_util
|
|
|
|
|
|
|
|
|
2020-09-21 14:41:44 +00:00
|
|
|
def _get_keylist_for_dict(d, parent_keys, indexes):
|
2020-02-04 11:26:10 +00:00
|
|
|
keylist = []
|
2020-09-21 14:41:44 +00:00
|
|
|
for key, value in d.items():
|
2020-02-04 09:33:20 +00:00
|
|
|
keys = parent_keys + [key]
|
2020-02-04 11:26:10 +00:00
|
|
|
keylist += [keys]
|
2020-09-21 14:41:44 +00:00
|
|
|
keylist += _get_keylist_for_value(value, keys, indexes)
|
2020-02-04 11:26:10 +00:00
|
|
|
return keylist
|
2020-02-04 09:33:20 +00:00
|
|
|
|
|
|
|
|
2022-03-24 12:26:18 +00:00
|
|
|
def _get_keylist_for_list(ls, parent_keys, indexes):
|
2020-09-21 14:41:44 +00:00
|
|
|
keylist = []
|
2022-03-24 12:26:18 +00:00
|
|
|
for key, value in enumerate(ls):
|
2020-09-21 14:41:44 +00:00
|
|
|
keys = list(parent_keys)
|
2022-02-18 00:02:02 +00:00
|
|
|
keys[-1] += f"[{key}]"
|
2020-09-21 14:41:44 +00:00
|
|
|
keylist += [keys]
|
|
|
|
keylist += _get_keylist_for_value(value, keys, indexes)
|
|
|
|
return keylist
|
|
|
|
|
|
|
|
|
|
|
|
def _get_keylist_for_value(value, parent_keys, indexes):
|
|
|
|
if type_util.is_dict(value):
|
|
|
|
return _get_keylist_for_dict(value, parent_keys, indexes)
|
|
|
|
elif type_util.is_list(value) and indexes:
|
|
|
|
return _get_keylist_for_list(value, parent_keys, indexes)
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
def keylists(d, indexes=False):
|
|
|
|
return _get_keylist_for_value(d, [], indexes)
|