reorg parsers

This commit is contained in:
2025-05-17 19:02:50 +02:00
parent 976116e2f3
commit 43acf4f415
34 changed files with 104 additions and 120 deletions

View File

@@ -78,7 +78,7 @@ async def load_csv(cfg: object) -> tuple:
logger.debug("Connessione al database stabilita")
query = f"""
SELECT id, unit_type, tool_type
SELECT id, unit_type, tool_type, unit_name, tool_name
FROM {cfg.dbname}.{cfg.dbrectable}
WHERE locked = 0 AND status = {CSV_RECEIVED}
LIMIT 1
@@ -88,8 +88,8 @@ async def load_csv(cfg: object) -> tuple:
result = cur.fetchone()
if result:
id, unit_type, tool_type = result
logger.info(f"Trovato CSV da elaborare: ID={id}, Tipo={unit_type}_{tool_type}")
id, unit_type, tool_type, unit_name, tool_name = result
logger.info(f"Trovato CSV da elaborare: ID={id}, Tipo={unit_type}_{tool_type}, Nome={unit_name}_{tool_name}")
lock_query = f"UPDATE {cfg.dbname}.{cfg.dbrectable} SET locked = 1 WHERE id = {id}"
logger.debug(f"Lock del record: {lock_query}")
@@ -97,21 +97,32 @@ async def load_csv(cfg: object) -> tuple:
conn.commit()
# Costruisce il nome del modulo da caricare dinamicamente
module_name = f'utils.parsers.{unit_type.lower()}_{tool_type.lower()}'
logger.debug(f"Caricamento modulo dinamico: {module_name}")
module_names = [f'utils.parsers.by_name.{unit_name.lower()}_{tool_name.lower()}',
f'utils.parsers.by_name.{unit_name.lower()}_{tool_type.lower()}',
f'utils.parsers.by_name.{unit_name.lower()}_all',
f'utils.parsers.by_type.{unit_type.lower()}_{tool_type.lower()}']
modulo = None
for module_name in module_names:
try:
logger.debug(f"Caricamento dinamico del modulo: {module_name}")
modulo = importlib.import_module(module_name)
logger.debug(f"Funzione 'main_loader' caricata dal modulo {module_name}")
return True, True
except (ImportError, AttributeError) as e:
logger.error(f"Errore nel caricamento del modulo {module_name}: {e}", exc_info=debug_mode)
try:
modulo = importlib.import_module(module_name)
funzione = getattr(modulo, "main_loader")
logger.debug(f"Funzione 'main_loader' trovata nel modulo {module_name}")
# Esegui la funzione async
await funzione(cfg, id)
logger.info(f"Elaborazione completata per ID={id}")
return True, True
except (ImportError, AttributeError) as e:
logger.error(f"Errore nel caricamento del modulo o della funzione: {e}", exc_info=debug_mode)
if not modulo:
logger.error(f"Nessun modulo trovato {module_names}")
return True, False
# Ottiene la funzione 'main_loader' dal modulo
funzione = getattr(modulo, "main_loader")
# Esegui la funzione
await funzione(cfg, id)
logger.info(f"Elaborazione completata per ID={id}")
return True, True
else:
logger.debug("Nessun record disponibile per l'elaborazione")
return False, False