Files
soleprint/soleprint/station/tools/dataconvert/output.py
2026-09-16 09:07:07 -03:00

47 lines
1.8 KiB
Python

"""
Output: one .sql file per table or sheet, named after it.
"""
from pathlib import Path
from sqlgen import render_table, sanitize_identifier
def table_filename(raw_name: str, source_name: str, sheet_count: int, bare_prefixes=()) -> str:
"""
Sheets of a multi-sheet workbook are prefixed with the workbook, so two
workbooks cannot collide, unless the config names the sheet as already
unique (bare_sheet_prefixes).
"""
clean = sanitize_identifier(raw_name)
if sheet_count > 1 and not clean.startswith(tuple(bare_prefixes)):
return f"{sanitize_identifier(source_name)}_{clean}.sql"
return f"{clean}.sql"
def write_tables(dfs: dict, out_dir: Path, source_name: str, max_rows=None, report=None, bare_prefixes=()):
"""Write individual .sql files per table/sheet into the output directory."""
for raw_name, df in dfs.items():
if df.empty:
continue
table = sanitize_identifier(raw_name)
filename = table_filename(raw_name, source_name, len(dfs), bare_prefixes)
sql, total, full_bytes, exact = render_table(df, table, max_rows)
out_file = out_dir / filename
# Created on the first file, so a run that finds nothing to convert
# leaves no empty directory behind.
out_dir.mkdir(parents=True, exist_ok=True)
# Several sources can feed the same table; they accumulate in one file.
mode = "a" if out_file.exists() else "w"
with open(out_file, mode, encoding="utf-8") as f:
f.write(sql)
shown = total if exact else max_rows
if report is not None:
report.add(source_name, table, filename, df, total, full_bytes, exact, shown)
suffix = "" if exact else f" ({shown} of {total} rows)"
print(f"[dataconvert] Generated: {out_file}{suffix}")