]> git.mar77i.info Git - hublib/blob - hub/utils.py
d888f1588a19b74a65559a6869b54c3162762b7c
[hublib] / hub / utils.py
1 from jinja2_simple_tags import StandaloneTag
2
3 from .static import StaticTemplateFile, TreeFileApp
4
5 class StaticTag(StandaloneTag):
6 tags = {"static"}
7
8 @staticmethod
9 def get_hubapp(
10 static_file: StaticTemplateFile, hubapp: str | TreeFileApp | None
11 ) -> TreeFileApp:
12 h = static_file.hubapp
13 if hubapp == "root":
14 return h.root
15 elif isinstance(hubapp, str):
16 return h.root.hubapps[hubapp]
17 elif isinstance(hubapp, TreeFileApp):
18 return hubapp
19 return static_file.hubapp
20
21 def render(
22 self, filename: str = "", hubapp: str | TreeFileApp | None = None
23 ) -> str:
24 """
25 If filename starts with '/', interpret the path as relative to hubapp.base_dir,
26 otherwise assume the path is relative to the current file.
27 """
28 static_file = self.context["static_file"]
29 hubapp = self.get_hubapp(static_file, hubapp)
30 if filename.startswith("/") or hubapp != static_file.hubapp:
31 path = hubapp.base_dir / filename.lstrip("/")
32 else:
33 path = static_file.path.parent / filename
34 return hubapp.uri(path)
35
36
37 def get_redis_pass(redis_conf: str) -> str:
38 """
39 Poor man's redis credentials: read the password from redis_conf.
40 Requires redis being configured with a `requirepass` password set.
41 """
42 prefix = "requirepass "
43 with open(redis_conf, "rt") as fh:
44 for line in fh:
45 if line.startswith(prefix):
46 return line[len(prefix) :].rstrip()
47 raise ValueError("No redis password found")