feat: Introduce build and installation scripts for IPSExtractor, including a batch file for PyInstaller and Inno Setup integration, and add application preferences management for document types and retry logic in the GUI.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
"""Persistencia simple de preferencias de la GUI."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict
|
||||
|
||||
from .config import DEFAULT_ALLOWED_DOCUMENT_TYPES
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
PREFS_FILE = Path(__file__).resolve().parent.parent / "app_prefs.json"
|
||||
|
||||
|
||||
def load_app_prefs() -> Dict[str, Any]:
|
||||
"""Carga preferencias desde disco si existen."""
|
||||
try:
|
||||
if not PREFS_FILE.exists():
|
||||
return {}
|
||||
raw = PREFS_FILE.read_text(encoding="utf-8")
|
||||
data = json.loads(raw)
|
||||
return data if isinstance(data, dict) else {}
|
||||
except Exception:
|
||||
logger.exception("No se pudieron cargar las preferencias de la app")
|
||||
return {}
|
||||
|
||||
|
||||
def save_app_prefs(data: Dict[str, Any]) -> None:
|
||||
"""Guarda preferencias de la app en JSON."""
|
||||
try:
|
||||
PREFS_FILE.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
except Exception:
|
||||
logger.exception("No se pudieron guardar las preferencias de la app")
|
||||
|
||||
|
||||
def normalize_document_types(values: Any) -> list[str]:
|
||||
"""Normaliza una lista editable de tipos documentales."""
|
||||
raw_values = values if isinstance(values, list) else []
|
||||
normalized: list[str] = []
|
||||
seen: set[str] = set()
|
||||
for item in raw_values:
|
||||
text = str(item or "").strip().upper()
|
||||
if not text or text == "NIT" or text in seen:
|
||||
continue
|
||||
seen.add(text)
|
||||
normalized.append(text)
|
||||
return normalized or list(DEFAULT_ALLOWED_DOCUMENT_TYPES)
|
||||
|
||||
|
||||
def get_allowed_document_types() -> tuple[str, ...]:
|
||||
"""Devuelve la lista persistida de tipos documentales válidos."""
|
||||
prefs = load_app_prefs()
|
||||
return tuple(normalize_document_types(prefs.get("allowed_document_types")))
|
||||
Reference in New Issue
Block a user