about summary refs log tree commit diff
path: root/libs
diff options
context:
space:
mode:
Diffstat (limited to 'libs')
-rw-r--r--libs/gen.py15
-rw-r--r--libs/util.py26
2 files changed, 41 insertions, 0 deletions
diff --git a/libs/gen.py b/libs/gen.py
new file mode 100644
index 0000000..7d68496
--- /dev/null
+++ b/libs/gen.py
@@ -0,0 +1,15 @@
+from pathlib import Path
+from os.path import join
+
+
+def add_files_recursive(files, root, common={}, target_root="/", files_root=None):
+    if files_root == None:
+        files_root = root
+
+    for p in Path(root).rglob("**/*"):
+        if not p.is_dir():
+            rel = str(p.relative_to(files_root))
+            key = join(target_root, rel)
+            files[key] = (files[key] if key in files else {}) | common | {
+                "source": rel,
+            }
diff --git a/libs/util.py b/libs/util.py
new file mode 100644
index 0000000..abc6ea2
--- /dev/null
+++ b/libs/util.py
@@ -0,0 +1,26 @@
+from base64 import b64encode
+from passlib.hash import bcrypt
+import hashlib
+import textwrap
+
+
+def base64(text) -> str:
+    return b64encode(str(text).encode("utf8")).decode("utf8")
+
+
+def sha256(text) -> str:
+    return hashlib.sha256(str(text).encode("utf-8")).hexdigest()
+
+
+def indent(text: str, prefix: str, predicate=None) -> str:
+    text = str(text)
+
+    if predicate == "not_first":
+        first_line = text.splitlines()[0]
+        predicate = lambda ln: ln.strip() != first_line
+
+    return textwrap.indent(textwrap.dedent(text), prefix, predicate)
+
+
+def hash_bcrypt(secret: str, salt: str) -> str:
+    return bcrypt.using(salt=str(salt)).hash(str(secret))