126 lines
5.0 KiB
Markdown
126 lines
5.0 KiB
Markdown
|
|
### How to work with config setup Example
|
|
|
|
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
|
|
|
|
if __name__ == "__main__":
|
|
"""
|
|
Load configuration from script_config.json in the current script directory.
|
|
|
|
Returns:
|
|
Dict containing configurations with levels 1, 2, 3 and working_directory
|
|
|
|
Example usage in scripts:
|
|
from script_utils import load_configuration
|
|
|
|
configs = load_configuration()
|
|
level1_config = configs.get("level1", {})
|
|
level2_config = configs.get("level2", {})
|
|
level3_config = configs.get("level3", {})
|
|
working_dir = configs.get("working_directory", "")
|
|
""""
|
|
|
|
try:
|
|
configs = load_configuration()
|
|
working_directory = configs.get("working_directory")
|
|
except Exception as e:
|
|
print(f"Warning: Could not load configuration (frontend not running): {e}")
|
|
configs = {}
|
|
working_directory = None
|
|
|
|
# Validate working directory with .debug fallback for standalone execution
|
|
if not working_directory or not os.path.isdir(working_directory):
|
|
print("Working directory not set or invalid in configuration.")
|
|
print("Using .debug directory as fallback for direct script execution.")
|
|
|
|
# Fallback to .debug directory under script location
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
debug_dir = os.path.join(script_dir, ".debug")
|
|
|
|
# Create .debug directory if it doesn't exist
|
|
os.makedirs(debug_dir, exist_ok=True)
|
|
working_directory = debug_dir
|
|
print(f"Using debug directory: {working_directory}")
|
|
else:
|
|
print(f"Using configured working directory: {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 x4
|
|
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_xref_source_subdir = group_config.get("xref_source_subdir", "source")
|
|
|
|
|
|
|
|
### Debug Directory Fallback
|
|
|
|
Cuando los scripts se ejecutan directamente (no desde el frontend), utilizan un sistema de fallback para determinar el directorio de trabajo:
|
|
|
|
1. **Con Frontend**: Los scripts utilizan el `working_directory` configurado dinámicamente a través de `load_configuration()`
|
|
2. **Ejecución Directa (Debug)**: Si no hay configuración válida, los scripts crean automáticamente un directorio `.debug` en la ubicación del script
|
|
|
|
**Estructura del directorio de debug:**
|
|
```
|
|
IO_adaptation/
|
|
├── .debug/ # ← Directorio creado automáticamente para debug
|
|
│ ├── *.aml # Archivos AML procesados
|
|
│ ├── *.hierarchical.json # Datos estructurados extraídos
|
|
│ ├── *_IO_Upward_Debug.md # Archivo de debug de conexiones IO
|
|
│ ├── Hardware.md # Tabla resumen de IO
|
|
│ └── <PLC_Name>/
|
|
│ └── Documentation/
|
|
│ └── *_Hardware_Tree.md # Árbol de hardware por PLC
|
|
├── x2_process_CAx.py # Script principal
|
|
└── example/ # Archivos de ejemplo
|
|
```
|
|
|
|
**Ventajas del directorio .debug:**
|
|
- ✅ Separación clara entre archivos de debug y archivos de producción
|
|
- ✅ Fácil limpieza (se puede eliminar todo el directorio `.debug`)
|
|
- ✅ No interfiere con archivos del proyecto principal
|
|
- ✅ Creación automática sin configuración manual
|
|
|
|
### Directory structure for Tia Portal scripts
|
|
|
|
<working_directory>/
|
|
├── <Project_Name>_CAx_Export.aml
|
|
├── <PLC1_Name>/
|
|
│ ├── ProgramBlocks_XML/
|
|
│ │ └── ... (archivos XML de bloques)
|
|
│ ├── ProgramBlocks_SCL/
|
|
│ │ └── ... (archivos SCL de bloques)
|
|
│ ├── ProgramBlocks_CR/
|
|
│ │ └── ... (archivos XML de referencias cruzadas de bloques)
|
|
│ ├── PlcTags/
|
|
│ │ └── ... (archivos XML de tablas de tags)
|
|
│ ├── PlcTags_CR/
|
|
│ │ └── ... (archivos XML de referencias cruzadas de tablas de tags)
|
|
│ ├── PlcDataTypes_CR/
|
|
│ │ └── ... (archivos XML de referencias cruzadas de UDTs)
|
|
│ ├── SystemBlocks_CR/
|
|
│ │ └── ...
|
|
│ └── SoftwareUnits_CR/
|
|
│ └── ...
|
|
│ └── Documentation/
|
|
│ └── Source
|
|
│ └── ... (archivos md de bloques de programa)
|
|
│ └── JSON
|
|
│ └── ... (archivos JSON temporales)
|
|
│ └── xref_calls_tree.md
|
|
│ └── xref_db_usage_summary.md
|
|
│ └── xref_plc_tags_summary.md
|
|
│ └── full_project_representation.md
|
|
│ └── <Project_Name>_CAx_Export_Hardware_Tree.md
|
|
|
|
├── <PLC2_Name>/
|
|
│ ├── ProgramBlocks_XML/
|
|
│ │ └── ...
|
|
│ └── ...
|
|
└── ... |