107 lines
3.0 KiB
Python
107 lines
3.0 KiB
Python
"""CLI local del Bot Digitador (sin API remota)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from bot_digitador.core.coords import DEFAULT_APP_PATH
|
|
from bot_digitador.core.digitador_service import DigitadorService
|
|
from bot_digitador.models.events import DigitadorEvent
|
|
|
|
|
|
def _print_event(event: DigitadorEvent) -> None:
|
|
print(event.display_message(), flush=True)
|
|
|
|
|
|
def build_parser() -> argparse.ArgumentParser:
|
|
parser = argparse.ArgumentParser(
|
|
description="Digita radicados en Integración Total desde un CSV local."
|
|
)
|
|
parser.add_argument(
|
|
"--csv",
|
|
required=True,
|
|
help="Ruta al CSV de entrada (coma o pipe)",
|
|
)
|
|
parser.add_argument(
|
|
"--output-dir",
|
|
default=None,
|
|
help="Carpeta de reportes (default: <csv_dir>/reportes_bot/<timestamp>)",
|
|
)
|
|
parser.add_argument(
|
|
"--delimiter",
|
|
choices=[",", "|"],
|
|
default="|",
|
|
help="Delimitador CSV (se auto-detecta si no coincide con columnas)",
|
|
)
|
|
parser.add_argument(
|
|
"--date-format",
|
|
choices=["dd/mm/yyyy", "mm/dd/yyyy"],
|
|
default="dd/mm/yyyy",
|
|
help="Formato preferido de fechas en el CSV",
|
|
)
|
|
parser.add_argument(
|
|
"--app-path",
|
|
default=os.getenv("DIGITADOR_APP_PATH", DEFAULT_APP_PATH),
|
|
help="Ruta al acceso directo / ejecutable de Integración Total",
|
|
)
|
|
parser.add_argument(
|
|
"--reinicio-cada",
|
|
type=int,
|
|
default=6,
|
|
help="Reinicio completo de la app cada N registros",
|
|
)
|
|
return parser
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
args = build_parser().parse_args(argv)
|
|
|
|
csv_path = Path(args.csv).resolve()
|
|
if not csv_path.exists():
|
|
print(f"CSV no encontrado: {csv_path}", file=sys.stderr)
|
|
return 2
|
|
|
|
if args.output_dir:
|
|
output_dir = Path(args.output_dir).resolve()
|
|
else:
|
|
from datetime import datetime
|
|
|
|
stamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
output_dir = csv_path.parent / "reportes_bot" / stamp
|
|
|
|
output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
service = DigitadorService(
|
|
csv_path=str(csv_path),
|
|
output_dir=str(output_dir),
|
|
date_format=args.date_format,
|
|
delimiter=args.delimiter,
|
|
app_path=args.app_path,
|
|
reinicio_cada=args.reinicio_cada,
|
|
event_callback=_print_event,
|
|
)
|
|
summary = service.run()
|
|
|
|
print(
|
|
f"Resumen: OK={summary.exitosos}/{summary.total}, "
|
|
f"fallidos={summary.fallidos}, existentes={summary.existentes}, "
|
|
f"omitidos={summary.omitidos}, timeouts={summary.timeouts}, "
|
|
f"cancelled={summary.cancelled}",
|
|
flush=True,
|
|
)
|
|
print(f"Reportes en: {summary.output_dir}", flush=True)
|
|
|
|
if summary.fatal_error:
|
|
print(f"Error fatal: {summary.fatal_error}", file=sys.stderr)
|
|
return 1
|
|
if summary.cancelled:
|
|
return 130
|
|
return 0 if summary.fallidos == 0 else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|