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:
rockdrigo
2026-11-13 15:23:45 -06:00
parent f183e495fb
commit 113b792a7b
8 changed files with 127 additions and 16 deletions
@@ -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.