"""Catalog-wide compatibility table, grouped by library name, as a downloadable file. GET /compat?format=md -> Markdown table (attachment compat.md) GET /compat?format=json -> structured JSON (attachment compat.json) Builds `ams.compat` entries from every catalogued snapshot, labelling each by its game (or '(bez gry)' when unlinked). Same engine library name across games clusters into surface variants, so divergent copies get an asterisk — exactly what the docs table needs. """ from __future__ import annotations from fastapi import APIRouter, Depends, Query from fastapi.responses import Response from sqlalchemy import select from sqlalchemy.orm import Session from ...compat import Entry, compatibility_table, render_json, render_markdown from ...snapshot import Snapshot as Surface from .. import models from ..db import get_db router = APIRouter(tags=["compat"]) _NO_GAME = "(bez gry)" @router.get("/compat") def get_compat( format: str = Query("md", pattern="^(md|json)$", description="md = Markdown, json = structured"), db: Session = Depends(get_db), ) -> Response: entries = [] for snap in db.scalars(select(models.Snapshot).order_by(models.Snapshot.id)): surface = Surface(snap.data) # Content-identical builds can ship with several games. Keep those aliases # even though the legacy ORM links a snapshot to only one primary game. games = {g for g in surface.binary.get("games", []) if isinstance(g, str) and g} if snap.game: games.add(snap.game.name) for game in sorted(games or {_NO_GAME}): entries.append(Entry(game=game, snapshot=surface)) table = compatibility_table(entries) if format == "json": return Response( content=render_json(table), media_type="application/json", headers={"Content-Disposition": 'attachment; filename="compat.json"'}, ) return Response( content=render_markdown(table), media_type="text/markdown; charset=utf-8", headers={"Content-Disposition": 'attachment; filename="compat.md"'}, )