from __future__ import annotations import os import os.path import typing import capport.comm.hub import capport.config import capport.utils.ipneigh import jinja2 import quart.templating import quart_trio class DispatchingJinjaLoader(quart.templating.DispatchingJinjaLoader): app: MyQuartApp def __init__(self, app: MyQuartApp) -> None: super().__init__(app) def _loaders(self) -> typing.Generator[jinja2.BaseLoader, None, None]: if self.app.custom_loader: yield self.app.custom_loader for loader in super()._loaders(): yield loader class MyQuartApp(quart_trio.QuartTrio): my_nc: typing.Optional[capport.utils.ipneigh.NeighborController] = None my_hub: typing.Optional[capport.comm.hub.Hub] = None my_config: capport.config.Config custom_loader: typing.Optional[jinja2.FileSystemLoader] = None def __init__(self, import_name: str, **kwargs) -> None: self.my_config = capport.config.Config.load_default_once() kwargs.setdefault('template_folder', os.path.join(os.path.dirname(__file__), 'templates')) cust_templ = os.path.join('custom', 'templates') if os.path.exists(cust_templ): self.custom_loader = jinja2.FileSystemLoader(os.fspath(cust_templ)) super().__init__(import_name, **kwargs) self.debug = self.my_config.debug self.secret_key = self.my_config.cookie_secret def create_global_jinja_loader(self) -> DispatchingJinjaLoader: """Create and return a global (not blueprint specific) Jinja loader.""" return DispatchingJinjaLoader(self)