From: Aleš Mrázek Date: Fri, 17 Jul 2026 13:14:36 +0000 (+0200) Subject: python: config: initial structure with templates X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fpython-refactoring-controller;p=thirdparty%2Fknot-resolver.git python: config: initial structure with templates --- 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