Files
plantillas-proyectos/backend/api/v1/modules/a76/layouts_csv/common/meta.py

36 lines
1.1 KiB
Python

"""
Carga y guardado de meta (tenant_id, company_id) para imports CSV.
"""
import json
import os
from typing import Dict, Any, Tuple
def load_meta(file_path: str) -> Dict[str, Any]:
"""Carga meta desde archivo .meta.json asociado al CSV. Devuelve dict vacío si no existe o falla."""
meta_path = file_path.replace(".csv", ".meta.json")
if not os.path.exists(meta_path):
return {}
try:
with open(meta_path, "r", encoding="utf-8") as f:
return json.load(f) or {}
except Exception:
return {}
def require_tenant_context(file_path: str) -> Tuple[int, int]:
"""
Obtiene tenant_id y company_id del meta. Lanza ValueError si faltan.
"""
meta = load_meta(file_path)
tenant_id = meta.get("tenant_id")
company_id = meta.get("company_id")
if not tenant_id or not company_id:
raise ValueError("Falta contexto (tenant/company)")
return int(tenant_id), int(company_id)
def get_meta_path(file_path: str) -> str:
"""Ruta del archivo .meta.json para un CSV."""
return file_path.replace(".csv", ".meta.json")