import warnings from functools import lru_cache from pathlib import Path from typing import Literal PROJECT_ROOT = Path(__file__).parents[2] CSS_FOLDER = PROJECT_ROOT / "src/assets/css" if not CSS_FOLDER.exists(): warnings.warn(f"CSS folder not found: {CSS_FOLDER.as_posix()!r}. Please check the folder exists.", stacklevel=2) JS_FOLDER = PROJECT_ROOT / "src/assets/js" if not JS_FOLDER.exists(): warnings.warn( f"JavaScript folder not found: {JS_FOLDER.as_posix()!r}. Please check the folder exists.", stacklevel=2 ) HTML_FOLDER = PROJECT_ROOT / "src/assets/html" if not HTML_FOLDER.exists(): warnings.warn(f"HTML folder not found: {HTML_FOLDER.as_posix()!r}. Please check the folder exists.", stacklevel=2) custom_css = CSS_FOLDER / "custom.css" # ------------------------------------------------------------ # Backend status indicator # ------------------------------------------------------------ @lru_cache(maxsize=1) def backend_status_js() -> str: file_path = JS_FOLDER / "backend-status.js" if not file_path.exists(): raise FileNotFoundError(f"JavaScript file not found: {file_path.as_posix()!r}. Please check the file exists.") return file_path.read_text(encoding="utf-8") @lru_cache(maxsize=3) def backend_status_indicator_html(status: Literal["healthy", "unhealthy", "undefined"]) -> str: file_path = HTML_FOLDER / f"backend-status-indicator.{status}.html" if not file_path.exists(): raise FileNotFoundError(f"HTML file not found: {file_path.as_posix()!r}. Please check the file exists.") return file_path.read_text(encoding="utf-8") @lru_cache(maxsize=1) def get_window_url_params() -> str: return """ function(url_params) { const params = new URLSearchParams(window.location.search); url_params = Object.fromEntries(params); return url_params; } """ backend_status_indicator_css = CSS_FOLDER / "backend-status-indicator.css" # ------------------------------------------------------------ # Submit form # ------------------------------------------------------------ @lru_cache(maxsize=1) def submit_html() -> str: file_path = HTML_FOLDER / "submit.html" if not file_path.exists(): raise FileNotFoundError(f"HTML file not found: {file_path.as_posix()!r}. Please check the file exists.") return file_path.read_text(encoding="utf-8") @lru_cache(maxsize=1) def submit_js() -> str: file_path = JS_FOLDER / "submit.js" if not file_path.exists(): raise FileNotFoundError(f"JavaScript file not found: {file_path.as_posix()!r}. Please check the file exists.") return file_path.read_text(encoding="utf-8")