]> git.mar77i.info Git - hublib/blobdiff - hub/utils.py
big cleanup and refactoring #1
[hublib] / hub / utils.py
index c9d955cfa107e1e6cc01f5d7a7ea1b3d1e9a23d1..b66fd97992c224a41e37928e5b68fc997349eb0e 100644 (file)
@@ -1,18 +1,39 @@
-from base64 import urlsafe_b64encode
-from hashlib import sha3_512
+from jinja2_simple_tags import StandaloneTag
 
+from .hubapp import TreeFileApp
 
-def scramble(secret, value):
-    h = sha3_512()
-    h.update(f"{secret}{value}".encode())
-    return urlsafe_b64encode(h.digest()).rstrip(b"=").decode("ascii")
+class StaticTag(StandaloneTag):
+    tags = {"static"}
+
+    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