Files

50 lines
1.7 KiB
Python

"""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 = [
Entry(game=snap.game.name if snap.game else _NO_GAME, snapshot=Surface(snap.data))
for snap in db.scalars(select(models.Snapshot).order_by(models.Snapshot.id))
]
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"'},
)