Update merge_yaml.py

This commit is contained in:
2025-08-27 19:58:06 +00:00
parent 013518813f
commit a6a980f24c

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import yaml from ruamel.yaml import YAML
import sys import sys
import json import json
from collections.abc import Mapping from collections.abc import Mapping
@ -23,31 +23,19 @@ def main():
file1 = input_data["file1"] file1 = input_data["file1"]
file2 = input_data["file2"] file2 = input_data["file2"]
yaml = YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
with open(file1, "r") as f1, open(file2, "r") as f2: with open(file1, "r") as f1, open(file2, "r") as f2:
yaml1 = yaml.load(f1)
yaml2 = yaml.load(f2)
try: from io import StringIO
yaml1 = yaml.safe_load(f1) output = StringIO()
except yaml.YAMLError as e: yaml.dump(merged, output)
print("Erro ao carregar YAML do primeiro ficheiro:")
print(f1.read())
raise e
try:
yaml2 = yaml.safe_load(f2)
except yaml.YAMLError as e:
print("Erro ao carregar YAML do segundo ficheiro:")
print(f2.read())
raise e
merged = deep_merge_yaml(yaml1, yaml2)
print(json.dumps({ print(json.dumps({
"merged_yaml": yaml.dump( "merged_yaml": output.getvalue()
merged,
sort_keys=False,
default_flow_style=False,
indent=2
)
})) }))