1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
import os
import sys
from jinja2 import Environment, FileSystemLoader, StrictUndefined, Template
def _get_templates_dir() -> str:
module = sys.modules["knot_resolver.datamodel"].__file__
if module:
templates_dir = os.path.join(os.path.dirname(module), "templates")
if os.path.isdir(templates_dir):
return templates_dir
raise NotADirectoryError(f"the templates dir '{templates_dir}' is not a directory or does not exist")
raise OSError("package 'knot_resolver.datamodel' cannot be located or loaded")
_TEMPLATES_DIR = _get_templates_dir()
def _import_kresd_config_template() -> Template:
path = os.path.join(_TEMPLATES_DIR, "kresd.lua.j2")
with open(path, "r", encoding="UTF-8") as file:
template = file.read()
return template_from_str(template)
def _import_policy_loader_config_template() -> Template:
path = os.path.join(_TEMPLATES_DIR, "policy-loader.lua.j2")
with open(path, "r", encoding="UTF-8") as file:
template = file.read()
return template_from_str(template)
def template_from_str(template: str) -> Template:
ldr = FileSystemLoader(_TEMPLATES_DIR)
env = Environment(trim_blocks=True, lstrip_blocks=True, loader=ldr, undefined=StrictUndefined)
return env.from_string(template)
KRESD_CONFIG_TEMPLATE = _import_kresd_config_template()
POLICY_LOADER_CONFIG_TEMPLATE = _import_policy_loader_config_template()
|