]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
python: config: initial structure with templates docs-python-refac-o5rd0i/deployments/9555 python-refactoring-controller
authorAleš Mrázek <ales.mrazek@nic.cz>
Fri, 17 Jul 2026 13:14:36 +0000 (15:14 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 17 Jul 2026 13:14:36 +0000 (15:14 +0200)
python/knot_resolver/config/__init__.py
python/knot_resolver/config/config.py [new file with mode: 0644]
python/knot_resolver/config/templates/__init__.py [new file with mode: 0644]
python/knot_resolver/config/templates/loader.lua.j2 [new file with mode: 0644]
python/knot_resolver/config/templates/supervisord.toml.j2 [new file with mode: 0644]
python/knot_resolver/config/templates/worker.lua.j2 [new file with mode: 0644]

index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..12061c0b74e9da788a8272764947e89a2058cd6e 100644 (file)
@@ -0,0 +1,5 @@
+from .config import KresConfig
+
+__all__ = [
+    "KresConfig",
+]
diff --git a/python/knot_resolver/config/config.py b/python/knot_resolver/config/config.py
new file mode 100644 (file)
index 0000000..cf75c69
--- /dev/null
@@ -0,0 +1,11 @@
+from __future__ import annotations
+
+from .templates import LOADER_TEMPLATE, WORKER_TEMPLATE
+
+
+class KresConfig:
+    def render_lua_worker(self) -> str:
+        return WORKER_TEMPLATE.render(cfg=self)
+
+    def render_lua_loader(self) -> str:
+        return LOADER_TEMPLATE.render(cfg=self)
diff --git a/python/knot_resolver/config/templates/__init__.py b/python/knot_resolver/config/templates/__init__.py
new file mode 100644 (file)
index 0000000..d0445de
--- /dev/null
@@ -0,0 +1,37 @@
+from __future__ import annotations
+
+from pathlib import Path
+
+from jinja2 import Environment, FileSystemLoader, StrictUndefined, Template
+
+
+def _get_templates_path() -> Path:
+    templates_path = Path(__file__).resolve().parent
+    if not templates_path.exists():
+        raise FileNotFoundError(templates_path)
+    if not templates_path.is_dir():
+        raise NotADirectoryError(templates_path)
+    return templates_path
+
+
+_TEMPLATES_PATH: Path = _get_templates_path()
+
+
+def _load_template_from_str(template: str) -> Template:
+    loader = FileSystemLoader(_TEMPLATES_PATH)
+    env = Environment(trim_blocks=True, lstrip_blocks=True, loader=loader, undefined=StrictUndefined)
+    return env.from_string(template)
+
+
+def _import_template(template: str) -> Template:
+    template_file = _TEMPLATES_PATH / template
+    with template_file.open() as file:
+        template = file.read()
+    return _load_template_from_str(template)
+
+
+SUPERVISORD_TEMPLATE: Template = _import_template("supervisord.toml.j2")
+
+WORKER_TEMPLATE: Template = _import_template("worker.lua.j2")
+
+LOADER_TEMPLATE: Template = _import_template("loader.lua.j2")
diff --git a/python/knot_resolver/config/templates/loader.lua.j2 b/python/knot_resolver/config/templates/loader.lua.j2
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/python/knot_resolver/config/templates/supervisord.toml.j2 b/python/knot_resolver/config/templates/supervisord.toml.j2
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/python/knot_resolver/config/templates/worker.lua.j2 b/python/knot_resolver/config/templates/worker.lua.j2
new file mode 100644 (file)
index 0000000..e69de29