HMI_Translate_Helper_wMaste.../translation_config.py

72 lines
2.4 KiB
Python
Raw Normal View History

from dataclasses import dataclass, asdict
import json
import os
import PyLibrary.funciones_comunes as fc
@dataclass
class TranslationConfig:
codigo_tipo_PLC: str
codigo_columna_maestra: str
codigo_idioma_seleccionado: str
codigo_idioma_secundario: str
work_dir: str
master_name: str
translate_name: str
auto_translate_name: str
nivel_afinidad_minimo: float = 0.5
traducir_todo: bool = False
@classmethod
def load(cls, filename="translation_config.json"):
if os.path.exists(filename):
with open(filename, "r") as f:
data = json.load(f)
return cls(**data)
return cls.default()
def save(self, filename="translation_config.json"):
with open(filename, "w") as f:
json.dump(asdict(self), f, indent=2)
@classmethod
def default(cls):
return cls(
codigo_tipo_PLC="siemens",
codigo_columna_maestra="it-IT",
codigo_idioma_seleccionado="en-GB",
codigo_idioma_secundario="es-ES",
work_dir="",
master_name="1_hmi_master_translates_siemens.xlsx",
translate_name="2_master_export2translate_siemens_en-GB.xlsx",
auto_translate_name="3_master_export2translate_translated_siemens_en-GB.xlsx",
)
def update(self, **kwargs):
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)
def get_tipo_PLC_nombre(self) -> str:
return next(nombre for nombre, codigo in fc.PLCs.values() if codigo == self.codigo_tipo_PLC)
def get_idioma_nombre(self, codigo: str) -> str:
return next(nombre for nombre, code in fc.IDIOMAS.values() if code == codigo)
def get_idioma_seleccionado_nombre(self) -> str:
return self.get_idioma_nombre(self.codigo_idioma_seleccionado)
def get_idioma_secundario_nombre(self) -> str:
return self.get_idioma_nombre(self.codigo_idioma_secundario)
def get_full_path(self, filename: str) -> str:
return os.path.join(self.work_dir, filename)
def get_master_path(self) -> str:
return self.get_full_path(self.master_name)
def get_translate_path(self) -> str:
return self.get_full_path(self.translate_name)
def get_auto_translate_path(self) -> str:
return self.get_full_path(self.auto_translate_name)