"""Functions for generating documentation.""" import json from contextlib import suppress from pathlib import Path from typing import Any from fastapi import FastAPI from fastapi.openapi.utils import get_openapi from spoolman.api.v1.router import app as v1_app def generate_openapi(app: FastAPI) -> dict[str, Any]: """Generate the OpenAPI document for a specific FastAPI app. Args: app (FastAPI): The FastAPI app. Returns: dict[str, Any]: The OpenAPI document. """ return get_openapi( title=app.title, version=app.version, openapi_version=app.openapi_version, description=app.description, routes=app.routes, contact=app.contact, license_info=app.license_info, servers=app.servers, tags=app.openapi_tags, terms_of_service=app.terms_of_service, ) def generate_docs() -> None: """Generate documentation for this service in the docs/ directory.""" with suppress(FileExistsError): Path("docs").mkdir() spec = json.dumps(generate_openapi(v1_app)) with Path("docs", "index.html").open("w") as f: f.write( f"""