X-Git-Url: https://git.mar77i.info/?a=blobdiff_plain;f=hub%2Futils.py;fp=hub%2Futils.py;h=b66fd97992c224a41e37928e5b68fc997349eb0e;hb=3c5ec422ace644d848d2f845b0f3ef8de73462ef;hp=ae448ed8fb499a14c3c9ea486fcc1e59252fbe42;hpb=16c0b1e580f2c92baf6882ec4dfa655c267d23b9;p=hublib diff --git a/hub/utils.py b/hub/utils.py index ae448ed..b66fd97 100644 --- a/hub/utils.py +++ b/hub/utils.py @@ -1,24 +1,39 @@ -from pathlib import Path - from jinja2_simple_tags import StandaloneTag +from .hubapp import TreeFileApp class StaticTag(StandaloneTag): tags = {"static"} - def render(self, filename="/", hubapp=None): - if not hubapp: - hubapp = self.context["hubapp"] - elif isinstance(hubapp, str): - hubapp = self.context["hubapp"].app.hubapps[hubapp] - return hubapp.uri_from(Path(filename)) + def render( + self, filename: str = "", hubapp: str | TreeFileApp | None = None + ): + """ + 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("/") + else: + path = static_file.path.parent / filename + return h.uri(path) def get_redis_pass(redis_conf): + """ + Poor man's redis credentials: read the password from redis_conf. + Requires redis being configured with a `requirepass` password set. + """ prefix = "requirepass " with open(redis_conf, "rt") as fh: for line in fh: - if not line.startswith(prefix): - continue - return line[len(prefix) :].rstrip() + if line.startswith(prefix): + return line[len(prefix) :].rstrip() return None