41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
"""Helpers para serializar resultados extraidos como filas de base de datos."""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from core.csv_utils import rows_from_data
|
|
|
|
|
|
def expand_alias_map(alias_map: dict[str, str]) -> dict[str, str]:
|
|
expanded = dict(alias_map or {})
|
|
for original, alias in list(expanded.items()):
|
|
if "/*/" in original:
|
|
tail = original.split("/*/", 1)[1]
|
|
if tail:
|
|
expanded.setdefault(tail, alias)
|
|
return expanded
|
|
|
|
|
|
def flatten_extraction_result(
|
|
data: dict[str, Any],
|
|
fields: list[str] | None,
|
|
alias_map: dict[str, str] | None = None,
|
|
) -> list[dict[str, Any]]:
|
|
rows, _header = rows_from_data(data, allowed_paths=fields)
|
|
if not alias_map:
|
|
return rows
|
|
|
|
aliases = expand_alias_map(alias_map)
|
|
aliased: list[dict[str, Any]] = []
|
|
for row in rows:
|
|
new_row: dict[str, Any] = {}
|
|
for key, value in row.items():
|
|
new_key = aliases.get(key, key)
|
|
if new_key in new_row and new_row[new_key] not in (None, ""):
|
|
if value not in (None, "") and str(value) != str(new_row[new_key]):
|
|
new_row[new_key] = f"{new_row[new_key]}, {value}"
|
|
else:
|
|
new_row[new_key] = value
|
|
aliased.append(new_row)
|
|
return aliased
|