155 lines
7.0 KiB
Python
155 lines
7.0 KiB
Python
"""
|
|
LadderToSCL - Conversor de Siemens LAD/FUP XML a SCL
|
|
|
|
Este script genera documentación en Markdown y SCL a partir de un proyecto XML de Siemens LAD/FUP.
|
|
|
|
"""
|
|
# ToUpload/x5_aggregate.py
|
|
# -*- coding: utf-8 -*-
|
|
import os
|
|
import argparse
|
|
import sys
|
|
import glob
|
|
import traceback
|
|
script_root = os.path.dirname(
|
|
os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
|
|
)
|
|
sys.path.append(script_root)
|
|
from backend.script_utils import load_configuration
|
|
|
|
# --- Constantes ---
|
|
# Nombre del archivo de salida por defecto (se creará en el directorio raíz del proyecto)
|
|
# AGGREGATED_FILENAME = "full_project_representation.md" # Se leerá de config
|
|
# Directorio donde x4 guarda sus salidas (relativo al directorio raíz del proyecto)
|
|
# XREF_OUTPUT_SUBDIR = "xref_output" # Se leerá de config
|
|
# SCL_OUTPUT_DIRNAME = "scl_output" # Se leerá de config
|
|
|
|
def aggregate_outputs(project_root_dir, output_filepath, scl_output_dirname, xref_output_dirname): # Añadido scl_output_dirname, xref_output_dirname
|
|
"""
|
|
Busca archivos .scl y .md generados y los agrega en un único archivo Markdown.
|
|
"""
|
|
print(f"--- Iniciando Agregación de Archivos (x5) ---")
|
|
print(f"Leyendo desde directorios: '{scl_output_dirname}' y '{xref_output_dirname}' (relativos a la raíz)")
|
|
print(f"Directorio Raíz del Proyecto: {project_root_dir}")
|
|
print(f"Archivo de Salida: {output_filepath}")
|
|
|
|
# Patrones para buscar archivos generados
|
|
# Buscamos .scl en cualquier subdirectorio (generados por x3 junto a los XML)
|
|
scl_pattern = os.path.join(project_root_dir, "**", "*.scl")
|
|
# Buscamos .md en cualquier subdirectorio (UDT/TagTable generados por x3, XRef por x4)
|
|
md_pattern_general = os.path.join(project_root_dir, "**", "*.md")
|
|
# Directorio de salida de x4
|
|
xref_dir_abs = os.path.join(project_root_dir, xref_output_dirname)
|
|
scl_dir_abs = os.path.join(project_root_dir, scl_output_dirname)
|
|
|
|
print(f"Buscando archivos SCL con patrón: {scl_pattern}")
|
|
print(f"Buscando archivos MD con patrón: {md_pattern_general}")
|
|
|
|
scl_files = glob.glob(scl_pattern, recursive=True)
|
|
md_files = glob.glob(md_pattern_general, recursive=True)
|
|
|
|
# Filtrar los archivos para asegurar que provienen de los directorios esperados
|
|
# y excluir el archivo de salida del propio x5.
|
|
output_filename_base = os.path.basename(output_filepath)
|
|
|
|
scl_files_filtered = [f for f in scl_files if os.path.dirname(f).startswith(scl_dir_abs)]
|
|
md_files_filtered = [
|
|
f for f in md_files
|
|
if os.path.basename(f) != output_filename_base # Excluir el archivo de salida
|
|
and (os.path.dirname(f).startswith(scl_dir_abs) or os.path.dirname(f).startswith(xref_dir_abs)) # Incluir MD de scl_output y xref_output
|
|
]
|
|
|
|
all_files = sorted(scl_files_filtered + md_files_filtered) # Combinar y ordenar alfabéticamente
|
|
|
|
all_files = sorted(scl_files + md_files_filtered) # Combinar y ordenar alfabéticamente
|
|
|
|
if not all_files:
|
|
print("Error: No se encontraron archivos .scl o .md para agregar.", file=sys.stderr)
|
|
print("Asegúrate de que los scripts x3 y x4 se ejecutaron correctamente.", file=sys.stderr)
|
|
return False
|
|
|
|
print(f"Se agregarán {len(all_files)} archivos.")
|
|
|
|
try:
|
|
with open(output_filepath, "w", encoding="utf-8") as outfile:
|
|
outfile.write(f"# Representación Completa del Proyecto PLC\n\n")
|
|
outfile.write(f"*(Agregado desde {len(all_files)} archivos)*\n\n")
|
|
|
|
for filepath in all_files:
|
|
relative_path = os.path.relpath(filepath, project_root_dir)
|
|
print(f" Agregando: {relative_path}")
|
|
|
|
# Añadir separador y encabezado de archivo
|
|
outfile.write(f"\n---\n")
|
|
outfile.write(f"## Archivo: `{relative_path.replace(os.sep, '/')}`\n\n")
|
|
|
|
try:
|
|
with open(filepath, "r", encoding="utf-8") as infile:
|
|
content = infile.read()
|
|
|
|
# Determinar si es SCL para envolverlo en bloque de código
|
|
if filepath.lower().endswith(".scl"):
|
|
outfile.write("```pascal\n")
|
|
outfile.write(content)
|
|
outfile.write("\n```\n")
|
|
else: # Asumir que es Markdown y escribir directamente
|
|
outfile.write(content)
|
|
outfile.write("\n") # Asegurar nueva línea al final
|
|
|
|
except Exception as read_err:
|
|
print(f" Error al leer {relative_path}: {read_err}", file=sys.stderr)
|
|
outfile.write(f"```\nERROR AL LEER ARCHIVO: {relative_path}\n{read_err}\n```\n")
|
|
|
|
print(f"\nAgregación completada. Archivo guardado en: {output_filepath}")
|
|
return True
|
|
|
|
except IOError as write_err:
|
|
print(f"Error Crítico: No se pudo escribir el archivo agregado '{output_filepath}'. Error: {write_err}", file=sys.stderr)
|
|
return False
|
|
except Exception as e:
|
|
print(f"Error Crítico inesperado durante la agregación: {e}", file=sys.stderr)
|
|
traceback.print_exc(file=sys.stderr)
|
|
return False
|
|
|
|
|
|
# --- Punto de Entrada ---
|
|
if __name__ == "__main__":
|
|
print("(x5 - Standalone) Ejecutando agregación de salidas...")
|
|
|
|
# Cargar configuración para obtener rutas
|
|
configs = load_configuration()
|
|
working_directory = configs.get("working_directory")
|
|
|
|
# Acceder a la configuración específica del grupo
|
|
group_config = configs.get("level2", {})
|
|
|
|
# Leer parámetros con valores por defecto (usando los defaults del esquema como guía)
|
|
# Parámetros necesarios para x5
|
|
cfg_scl_output_dirname = group_config.get("scl_output_dir", "scl_output")
|
|
cfg_xref_output_dirname = group_config.get("xref_output_dir", "xref_output")
|
|
cfg_aggregated_filename = group_config.get("aggregated_filename", "full_project_representation.md")
|
|
if not working_directory:
|
|
print("Error: 'working_directory' no encontrado en la configuración.", file=sys.stderr)
|
|
else:
|
|
# Calcular rutas basadas en la configuración
|
|
plc_subdir_name = "PLC" # Asumir nombre estándar
|
|
project_root_dir = os.path.join(working_directory, plc_subdir_name)
|
|
# El archivo agregado va al working_directory original
|
|
output_agg_file = os.path.join(working_directory, cfg_aggregated_filename) # Usar nombre de archivo leído
|
|
|
|
if not os.path.isdir(project_root_dir):
|
|
print(f"Error: Directorio del proyecto '{project_root_dir}' no encontrado.", file=sys.stderr)
|
|
else:
|
|
# Llamar a la función principal
|
|
# Pasar los nombres de directorios leídos
|
|
success = aggregate_outputs(
|
|
project_root_dir,
|
|
output_agg_file,
|
|
cfg_scl_output_dirname,
|
|
cfg_xref_output_dirname)
|
|
|
|
if success:
|
|
print("\n(x5 - Standalone) Proceso completado exitosamente.")
|
|
else:
|
|
print("\n(x5 - Standalone) Proceso finalizado con errores.", file=sys.stderr)
|