from __future__ import annotations from collections.abc import Awaitable, Callable, Iterable from typing import Any from fastapi import HTTPException from api_server.html5_access import ( render_html5_access_page, render_html5_access_publish_plan, render_html5_access_publish_result, ) def html5_access_page( *, project_id: str, profile: str | None, project_summaries: Callable[[], Iterable[object]], normalized_project: Callable[[str], object], access_profile_by_name: Callable[[object, str], object | None], access_publish_plan: Callable[[object, object], object], ) -> str: try: normalized = normalized_project(project_id) selected = _selected_profile(normalized, profile, access_profile_by_name) plan = access_publish_plan(normalized, selected) if selected is not None else None return render_html5_access_page( project_id=project_id, projects=project_summaries(), normalized=normalized, selected_profile=profile, plan=plan, ) except HTTPException as error: return render_html5_access_page( project_id=project_id, projects=project_summaries(), normalized=None, error=str(error.detail), ) def html5_access_publish_plan( *, project_id: str, profile_name: str, normalized_project: Callable[[str], object], access_profile_by_name: Callable[[object, str], object | None], access_publish_plan: Callable[[object, object], object], ) -> str: normalized = normalized_project(project_id) profile = access_profile_by_name(normalized, profile_name) if profile is None: raise HTTPException(status_code=404, detail="Access profile not found") return render_html5_access_publish_plan( project_id=project_id, profile=profile, plan=access_publish_plan(normalized, profile), ) async def html5_access_publish_dry_run( *, project_id: str, profile_name: str, publish_dry_run: Callable[[str, str], Awaitable[Any]], ) -> str: result = await publish_dry_run(project_id, profile_name) return render_html5_access_publish_result(project_id=project_id, result=result) def _selected_profile( normalized: object, profile_name: str | None, access_profile_by_name: Callable[[object, str], object | None], ) -> object | None: access = getattr(normalized, "access", None) profiles = list(getattr(access, "profiles", []) or []) if not profiles: return None if not profile_name: return profiles[0] return access_profile_by_name(normalized, profile_name) or profiles[0]