templates-zabbix-itguys/repair_yaml_indentation.py

71 lines
2.8 KiB
Python

import sys
TARGET_FILE = r"C:\Users\joao.goncalves\Desktop\zabbix-itguys\templates_gold\windows_active_agent\template_windows_gold_ptbr.yaml"
def repair_indentation():
print(f"Reading {TARGET_FILE}...")
try:
with open(TARGET_FILE, 'r', encoding='utf-8') as f:
lines = f.readlines()
except Exception as e:
print(f"Error reading: {e}")
return
fixed_lines = []
i = 0
while i < len(lines):
line = lines[i].rstrip('\n') # Keep indentation? No, rstrip end
# Check if this line is a "description" line that needs merging
if "description: ⚠️" in line:
# This is likely the start of a broken block.
# We need to look ahead for the broken parts.
description_content = line.split("description: ", 1)[1] # Get content after "description: "
# If it doesn't start with quote, let's start expecting breakage
if not description_content.startswith('"'):
current_desc = description_content
i += 1
# Consume next lines if they start with specific markers
while i < len(lines):
next_line = lines[i].strip() # Remove indentation of next line (it's 0 usually if broken)
if not next_line: # Skip empty lines
i += 1
continue
if next_line.startswith("📉 Impacto") or next_line.startswith("🛠️ Ação"):
current_desc += "\\n\\n" + next_line
i += 1
else:
# Not a broken part, stop consuming
break
# Now verify if we need to quote it
# It's safest to ensure it's quoted
if not current_desc.startswith('"'):
current_desc = '"' + current_desc
if not current_desc.endswith('"'):
current_desc = current_desc + '"'
# Reconstruct the line preserving original indentation
indentation = line.split("description:")[0]
fixed_lines.append(f"{indentation}description: {current_desc}\n")
continue # i is already advanced
else:
# Already quoted? might be fine, or might be the "simple fix" case
pass
fixed_lines.append(lines[i])
i += 1
try:
with open(TARGET_FILE, 'w', encoding='utf-8') as f:
f.writelines(fixed_lines)
print(f"Saved repaired file.")
except Exception as e:
print(f"Error writing: {e}")
if __name__ == "__main__":
repair_indentation()