The import's catalog matching and commit read the WebApi's catalog, silver batch and table maps; add_resultado moves here
How: `catalog_matching`, `catalog_commit_service` and the imports blueprint import `Catalogo`, `catalogo_desde_toml`, `ruta_de_catalogo`, `CatalogoInvalido`, `LotePlata`, `valor_de_observacion` and `tablas_de` from `webapi.orchestration`; the commit writes through `carga_repository.save_load` and `catalog_repository.sync`. `add_resultado` and its tests come over unchanged from the indicator kit. Implications: the import path no longer needs the indicator kit, which isn't a dependency.
This commit is contained in:
@@ -8,11 +8,11 @@ from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from extraction import BBox, ExtractionError
|
||||
from extraction.readers import reader_for
|
||||
from indicadores_kit.registry.catalogos import CatalogoInvalido
|
||||
from webapi.imports import audit as import_audit
|
||||
from webapi.imports.connectors import ConnectorError, build_connector, catalog, connector_class, kind_for_filename
|
||||
from webapi.imports.connectors.base import decode_file
|
||||
from webapi.imports.connectors.pdf import PdfConnector
|
||||
from webapi.orchestration.reference.catalogs import CatalogoInvalido
|
||||
from webapi.imports.connectors.sql import SqlConnector
|
||||
from webapi.imports.connectors.tabular import FileConnector
|
||||
from webapi.imports.schemas.registry import ENTITY_REGISTRY
|
||||
|
||||
@@ -16,8 +16,8 @@ from rapidfuzz import fuzz, process
|
||||
|
||||
from extraction.nlp import DiscoveredField
|
||||
from extraction.nlp.text import find_date, fold
|
||||
from indicadores_kit.plata.procedimiento import valor_de_observacion
|
||||
from indicadores_kit.registry.catalogos import Catalogo, add_resultado
|
||||
from webapi.orchestration.reference.catalogs import Catalogo, add_resultado
|
||||
from webapi.orchestration.silver.procedures import valor_de_observacion
|
||||
|
||||
# `token_sort_ratio`, not `WRatio`: `WRatio`'s partial-alignment bonus scores
|
||||
# two short, unrelated captions that merely share common words ("de", "del")
|
||||
|
||||
@@ -11,7 +11,7 @@ the import report lists them (`webapi.imports.audit`).
|
||||
There is no staging row for these — the whole point is a field outside
|
||||
today's structured schemas — so this bypasses `DESDE_STAGING` and writes
|
||||
straight through the domain's existing `carga_repository`, the same one
|
||||
`webapi.orquestacion`'s `cargar` step uses for staging-derived batches.
|
||||
`webapi.orchestration`'s `load` step uses for staging-derived batches.
|
||||
|
||||
Marked clearly apart from every staging-derived procedimiento:
|
||||
`fuente_tabla` is the sentinel `FUENTE_CATALOGO_ABIERTO`, never a real
|
||||
@@ -27,11 +27,11 @@ from dataclasses import dataclass
|
||||
from importlib import import_module
|
||||
|
||||
from extraction import ExtractedRecord
|
||||
from indicadores_kit.plata.procedimiento import LotePlata
|
||||
from indicadores_kit.registry.catalogos import Catalogo, catalogo_desde_toml, ruta_de_catalogo
|
||||
from indicadores_kit.tablas import tablas_de
|
||||
|
||||
from webapi.imports.schemas.catalog_matching import MatchedField, match_or_mint
|
||||
from webapi.orchestration.reference.catalogs import Catalogo, catalogo_desde_toml, ruta_de_catalogo
|
||||
from webapi.orchestration.silver.procedures import LotePlata
|
||||
from webapi.orchestration.tables import tablas_de
|
||||
|
||||
FUENTE_CATALOGO_ABIERTO = "catalogo_abierto"
|
||||
|
||||
@@ -93,7 +93,7 @@ def commit_discovered_fields(
|
||||
|
||||
if procedures:
|
||||
_sync_catalog(dominio, proyecto, tablas)
|
||||
dominio.carga_repository.guardar_carga(
|
||||
dominio.carga_repository.save_load(
|
||||
fuente=FUENTE_CATALOGO_ABIERTO, personas=(), plata=LotePlata(procedures, observations, {})
|
||||
)
|
||||
return result
|
||||
@@ -120,4 +120,4 @@ def _sync_catalog(dominio, proyecto: str, tablas) -> None:
|
||||
runs: `procedimiento.estudio_id` and `observacion`'s catalog both need
|
||||
the matching `cat_estudio`/`cat_resultado` row to already be there."""
|
||||
catalogo: Catalogo = catalogo_desde_toml(ruta_de_catalogo(proyecto), tablas=tablas)
|
||||
dominio.catalogo_repository.sincronizar(catalogo.estudios, catalogo.resultados, catalogo.valores)
|
||||
dominio.catalog_repository.sync(catalogo.estudios, catalogo.resultados, catalogo.valores)
|
||||
|
||||
@@ -79,6 +79,40 @@ def catalogo_desde_toml(ruta: Path, *, tablas: TablasKit) -> Catalogo:
|
||||
return Catalogo(estudios, resultados, valores)
|
||||
|
||||
|
||||
def add_resultado(
|
||||
proyecto: str, *, estudio_id: str, nombre: str, etiqueta: str, tipo_valor: str, estudio_etiqueta: str | None = None
|
||||
) -> str:
|
||||
"""Appends a new `resultado_id` to the project's TOML (not to the
|
||||
database): a `[estudio.resultados.nombre]` table, and the `[estudio]`
|
||||
table too if it didn't exist yet. The file stays the source: the next
|
||||
`catalogo_desde_toml` already brings it, and whoever syncs the table
|
||||
writes it.
|
||||
|
||||
Idempotent: if the resultado is already in the file, nothing is
|
||||
written and its `resultado_id` is returned as is. `nombre` must be a
|
||||
valid TOML key (whoever discovers the field builds it, e.g. from a
|
||||
slug of the text found).
|
||||
"""
|
||||
if tipo_valor not in TIPOS_DE_VALOR:
|
||||
raise CatalogoInvalido(f"{estudio_id}.{nombre}: tipo_valor {tipo_valor!r} no es uno de {', '.join(TIPOS_DE_VALOR)}.")
|
||||
ruta = ruta_de_catalogo(proyecto)
|
||||
existentes = _leer(ruta)
|
||||
resultado_id = f"{estudio_id}.{nombre}"
|
||||
if estudio_id in existentes and nombre in existentes[estudio_id].get("resultados", {}):
|
||||
return resultado_id
|
||||
blocks = []
|
||||
if estudio_id not in existentes:
|
||||
blocks.append(f'\n[{estudio_id}]\netiqueta = "{_escape(estudio_etiqueta or estudio_id)}"\n')
|
||||
blocks.append(f'\n[{estudio_id}.resultados.{nombre}]\netiqueta = "{_escape(etiqueta)}"\ntipo_valor = "{tipo_valor}"\n')
|
||||
with Path(ruta).open("a", encoding="utf-8") as archivo:
|
||||
archivo.write("".join(blocks))
|
||||
return resultado_id
|
||||
|
||||
|
||||
def _escape(text: str) -> str:
|
||||
return text.replace("\\", "\\\\").replace('"', '\\"')
|
||||
|
||||
|
||||
def codigos_hospital(proyecto: str) -> dict[str, str]:
|
||||
"""Código del hospital -> `estudio_id`, del catálogo del proyecto.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user