I have a Python dict that comes from reading a YAML file using regular
yaml.load(stream)
I want to update the YAML file programmatically by specifying the path to update, for example:
group1, option1, option11, value
and save the resulting dict again as a yaml file. I ran into a dicntionary update problem, given that the path is dynamic (suppose the user can enter the path through a simple CLI that I created using Cmd).
Any ideas?
thank!
UPDATE
Let me clarify the question: the problem is updating the part of the dictionary where I do not know the structure in advance. I am working on a project where the entire configuration is stored in YAML files, and I want to add CLIs to avoid having to edit them manually. This is an example of a YAML file loaded into a dictionary (config-dict) using PyYaml:
config:
a-function: enable
b-function: disable
firewall:
NET:
A:
uplink: enable
downlink: enable
B:
uplink: enable
downlink: enable
subscriber-filter:
cancellation-timer: 180
service:
copy:
DS: enable
remark:
header-remark:
DSC: enable
remark-table:
port:
linkup-debounce: 300
p0:
mode: amode
p1:
mode: bmode
p2:
mode: amode
p3:
mode: bmode
I created a CLI with Cmd and it works great even with autocomplete. The user can provide a string, for example:
config port p1 mode amode
So I need to edit:
config-dict ['config'] ['port'] ['p1'] ['mode'] and set it to "amode". Then use yaml.dump () to create the file again. Another possible line:
config a-function enable
So config-dict ['config'] ['a-function'] must be set to 'enable'.
. Python , : dict , . , Cmd. , .
, !
.