Files
Spoolman/spoolman/export.py
T
Donkie e509378392 chore: require Python 3.14
The declared floor was ">=3.10", but nothing enforced it: the Docker image
has pinned python:3.14-slim-bookworm for amd64/arm64/armv7 for some time,
and scripts/install.sh runs "uv sync" without UV_PYTHON_DOWNLOADS=0, so uv
fetches a managed interpreter when the system one is too old. Every
dependency declares a floor of <=3.10 with no upper cap.

Raising it unlocks enum.StrEnum, datetime.UTC and typing.Self for the
authentication work that follows.

Consequences handled here:

- uv.lock regenerated. Four pure-backport packages drop out
  (async-timeout, backports-asyncio-runner, exceptiongroup, tomli).

- ruff target-version follows to py314, which switches on PEP 649 deferred
  annotations and with them a batch of previously-dormant rules. The safe
  automatic fixes are applied: datetime.timezone.utc -> datetime.UTC
  (UP017) and redundant quotes dropped from annotations (UP037).

- TC001/TC002/TC003 are ignored rather than applied. They want type-only
  imports moved into "if TYPE_CHECKING:" blocks, which is wrong here:
  FastAPI resolves Annotated[AsyncSession, Depends(...)] and websocket
  parameters at runtime, Pydantic resolves field types at runtime, and
  SQLAlchemy resolves Mapped[...] at mapper-configuration time. A
  TYPE_CHECKING-only import is absent from the runtime namespace, so
  applying them breaks dependency injection. Ruff classifies all 50 as
  unsafe fixes for that reason.

Verified: ruff check/format clean, uv lock --check clean, alembic head
resolves, SQLAlchemy mappers configure with all relationships intact and
lazy="selectin" preserved on the extra-field collections.
2026-07-27 20:09:43 +02:00

67 lines
2.6 KiB
Python

"""Functionality for exporting data in various format."""
import asyncio
import csv
import json
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any
from spoolman.database import models
if TYPE_CHECKING:
from _typeshed import SupportsWrite
banned_attrs = {"awaitable_attrs", "metadata", "registry", "spools", "filaments"}
async def flatten_sqlalchemy_object(obj: models.Base, parent_key: str = "", sep: str = ".") -> dict[str, Any]:
"""Recursively flattens a SQLAlchemy object into a dictionary with dot-separated keys."""
fields = {}
for attr in dir(obj):
# Check if the attribute is a column or a relationship
if not attr.startswith("_") and attr not in banned_attrs:
value = await getattr(obj.awaitable_attrs, attr)
if attr == "extra":
# Handle extra fields
for v in value:
fields[f"{parent_key}extra.{v.key}"] = v.value
continue
# Handle nested SQLAlchemy objects
if isinstance(value, models.Base):
nested_fields = await flatten_sqlalchemy_object(value, f"{parent_key}{attr}{sep}", sep=sep)
fields.update(nested_fields)
else:
# Use only columns and simple data types
fields[f"{parent_key}{attr}"] = value
return fields
async def dump_as_csv(sqlalchemy_objects: Iterable[models.Base], writer: SupportsWrite[str]) -> None:
"""Export a list of objects as CSV to a writer. Nested objects are flattened with dot-separated keys."""
# Flatten each object and get all column names
all_flattened = await asyncio.gather(*[flatten_sqlalchemy_object(obj) for obj in sqlalchemy_objects])
# Collect all unique headers across flattened objects
headers = set()
for flattened_obj in all_flattened:
headers.update(flattened_obj.keys())
headers = sorted(headers) # Sort headers for consistent column ordering
# Write to CSV
csv_writer = csv.DictWriter(writer, fieldnames=headers)
csv_writer.writeheader()
for flattened_obj in all_flattened:
csv_writer.writerow(flattened_obj)
async def dump_as_json(sqlalchemy_objects: Iterable[models.Base], writer: SupportsWrite[str]) -> None:
"""Export a list of objects as JSON to a writer. Nested objects are flattened with dot-separated keys."""
# Flatten each object and get all column names
all_flattened = await asyncio.gather(*[flatten_sqlalchemy_object(obj) for obj in sqlalchemy_objects])
# Write to JSON
json.dump(all_flattened, writer, default=str)