X-Git-Url: https://git.mar77i.info/?a=blobdiff_plain;f=hub%2Futils.py;h=d888f1588a19b74a65559a6869b54c3162762b7c;hb=f591748910835b1a11c3765723d9d30193a5bd26;hp=b66fd97992c224a41e37928e5b68fc997349eb0e;hpb=3c5ec422ace644d848d2f845b0f3ef8de73462ef;p=hublib diff --git a/hub/utils.py b/hub/utils.py index b66fd97..d888f15 100644 --- a/hub/utils.py +++ b/hub/utils.py @@ -1,32 +1,40 @@ from jinja2_simple_tags import StandaloneTag -from .hubapp import TreeFileApp +from .static import StaticTemplateFile, TreeFileApp class StaticTag(StandaloneTag): tags = {"static"} + @staticmethod + def get_hubapp( + static_file: StaticTemplateFile, hubapp: str | TreeFileApp | None + ) -> TreeFileApp: + h = static_file.hubapp + if hubapp == "root": + return h.root + elif isinstance(hubapp, str): + return h.root.hubapps[hubapp] + elif isinstance(hubapp, TreeFileApp): + return hubapp + return static_file.hubapp + def render( self, filename: str = "", hubapp: str | TreeFileApp | None = None - ): + ) -> str: """ If filename starts with '/', interpret the path as relative to hubapp.base_dir, otherwise assume the path is relative to the current file. """ static_file = self.context["static_file"] - h = static_file.hubapp - if isinstance(hubapp, str): - h = h.app.hubapps[hubapp] - elif isinstance(hubapp, TreeFileApp): - h = hubapp - del hubapp - if filename.startswith("/") or h != static_file.hubapp: - path = h.base_dir / filename.lstrip("/") + hubapp = self.get_hubapp(static_file, hubapp) + if filename.startswith("/") or hubapp != static_file.hubapp: + path = hubapp.base_dir / filename.lstrip("/") else: path = static_file.path.parent / filename - return h.uri(path) + return hubapp.uri(path) -def get_redis_pass(redis_conf): +def get_redis_pass(redis_conf: str) -> str: """ Poor man's redis credentials: read the password from redis_conf. Requires redis being configured with a `requirepass` password set. @@ -36,4 +44,4 @@ def get_redis_pass(redis_conf): for line in fh: if line.startswith(prefix): return line[len(prefix) :].rstrip() - return None + raise ValueError("No redis password found")