"""Template path helpers shared by web/auth routes. The packaged PyInstaller build stores templates under the bundled resource folder, while source runs use the repository checkout. Keep route modules from hardcoding relative template paths such as ``src/landppt/web/templates``. """ from __future__ import annotations import os import sys from fastapi.templating import Jinja2Templates def get_templates_dir() -> str: """Resolve the template directory for source and PyInstaller packaged runs.""" candidates: list[str] = [] if getattr(sys, "frozen", False): executable_dir = os.path.dirname(sys.executable) meipass = getattr(sys, "_MEIPASS", "") candidates.extend([ os.path.join(meipass, "src", "landppt", "web", "templates"), os.path.join(executable_dir, "_internal", "src", "landppt", "web", "templates"), os.path.join(executable_dir, "src", "landppt", "web", "templates"), ]) candidates.extend([ os.path.abspath(os.path.join(os.path.dirname(__file__), "templates")), os.path.abspath("src/landppt/web/templates"), ]) for candidate in candidates: if candidate and os.path.isdir(candidate): return candidate return candidates[0] def create_templates() -> Jinja2Templates: """Create a Jinja2Templates instance using the resolved template path.""" return Jinja2Templates(directory=get_templates_dir())