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