Refactor update_yaml_file function to separate the logic for updating YAML data into a new function update_yaml().

This commit is contained in:
Sergey Krashevich 2023-07-03 07:22:15 +03:00
parent de95bcb087
commit 1926c1d645
No known key found for this signature in database
GPG Key ID: 625171324E7D3856

View File

@ -1235,6 +1235,13 @@ def update_yaml_file(file_path, key_path, new_value):
with open(file_path, "r") as f: with open(file_path, "r") as f:
data = yaml.load(f) data = yaml.load(f)
data = update_yaml(data, key_path, new_value)
with open(file_path, "w") as f:
yaml.dump(data, f)
def update_yaml(data, key_path, new_value):
temp = data temp = data
for key in key_path[:-1]: for key in key_path[:-1]:
if isinstance(key, tuple): if isinstance(key, tuple):
@ -1271,5 +1278,4 @@ def update_yaml_file(file_path, key_path, new_value):
else: else:
temp[last_key] = new_value temp[last_key] = new_value
with open(file_path, "w") as f: return data
yaml.dump(data, f)