I am parsing a YAML file to search for values at any key. Currently I can parse any dict at the first level, but cannot parse nested dictionaries.
I have tried modifying the example at https://stackoverflow.com/a/55608627 in order to parse the dictionary inside of the list, however this results in an error:
AttributeError: 'CommentedSeq' object has no attribute 'items'
When looking at the canonical output from http://yaml-online-parser.appspot.com/ it shows there is a map and a sequence, which I have been unable to account for.
The unmodified parsing function does not output any errors, however it does not see anything inside of the list.
The modified parsing function returns the AttributeError above.
Example YAML file: https://pastebin.com/BhwyPa7V
Full project: https://github.com/Just-Insane/helm-vault/blob/master/vault.py
Parsing function (unmodified):
def dict_walker(node, pattern, path=None):
path = path if path is not None else ""
for key, value in node.items():
if isinstance(value, dict):
dict_walker(value, pattern=pattern, path=f"{path}/{key}")
elif value == pattern:
if action == "enc":
node[key] = input(f"Input a value for {path}/{key}: ")
vault_write(node[key], path, key)
elif (action == "dec") or (action == "view") or (action == "edit"):
value = vault_read(path, key)
node[key] = value
Parsing function (modified):
def dict_walker(node, pattern, path=None):
path = path if path is not None else ""
for key, value in node.items():
if isinstance(value, dict):
dict_walker(value, pattern=pattern, path=f"{path}/{key}")
elif isinstance(value, list):
for item in value:
for value in dict_walker(value, pattern=pattern, path=f"{path}/{key}"):
if value == pattern:
if action == "enc":
node[key] = input(f"Input a value for {path}/{key}: ")
vault_write(node[key], path, key)
elif (action == "dec") or (action == "view") or (action == "edit"):
value = vault_read(path, key)
node[key] = value
elif value == pattern:
if action == "enc":
node[key] = input(f"Input a value for {path}/{key}: ")
vault_write(node[key], path, key)
elif (action == "dec") or (action == "view") or (action == "edit"):
value = vault_read(path, key)
node[key] = value
Expected Results:
The nested dictionary is parsed and values inside are able to be modified successfully.
Actual Results:
Using the unmodified code, values inside the list are not seen at all.
Using the modified code, there is an attribute error caused by
CommentedSeq
. It is unclear why it is not being parsed as a list.