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