From 4a3bdee5e6d59625c978c06c7224099c4d2071e6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ale=C5=A1=20Mr=C3=A1zek?= Date: Fri, 17 Jul 2026 15:14:36 +0200 Subject: [PATCH] python: config: initial structure with templates --- python/knot_resolver/config/__init__.py | 5 +++ python/knot_resolver/config/config.py | 11 ++++++ .../config/templates/__init__.py | 37 +++++++++++++++++++ .../config/templates/loader.lua.j2 | 0 .../config/templates/supervisord.toml.j2 | 0 .../config/templates/worker.lua.j2 | 0 6 files changed, 53 insertions(+) create mode 100644 python/knot_resolver/config/config.py create mode 100644 python/knot_resolver/config/templates/__init__.py create mode 100644 python/knot_resolver/config/templates/loader.lua.j2 create mode 100644 python/knot_resolver/config/templates/supervisord.toml.j2 create mode 100644 python/knot_resolver/config/templates/worker.lua.j2 diff --git a/python/knot_resolver/config/__init__.py b/python/knot_resolver/config/__init__.py index e69de29bb..12061c0b7 100644 --- a/python/knot_resolver/config/__init__.py +++ b/python/knot_resolver/config/__init__.py @@ -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 index 000000000..cf75c694c --- /dev/null +++ b/python/knot_resolver/config/config.py @@ -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 index 000000000..d0445de3d --- /dev/null +++ b/python/knot_resolver/config/templates/__init__.py @@ -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 index 000000000..e69de29bb diff --git a/python/knot_resolver/config/templates/supervisord.toml.j2 b/python/knot_resolver/config/templates/supervisord.toml.j2 new file mode 100644 index 000000000..e69de29bb diff --git a/python/knot_resolver/config/templates/worker.lua.j2 b/python/knot_resolver/config/templates/worker.lua.j2 new file mode 100644 index 000000000..e69de29bb -- 2.47.3