From d0ae5d79c9252863dda5620148cf754e2d538569 Mon Sep 17 00:00:00 2001 From: Konstantin Demin Date: Fri, 6 Jun 2025 11:31:38 +0300 Subject: [PATCH] j2cfg: improve loading - provide almost all implemented helper functions as functions and filters (was: only filters) - improve diagnostic messages during load --- j2cfg/j2cfg/__init__.py | 25 ++++++++++++++++--------- j2cfg/j2cfg/functions.py | 7 +++++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/j2cfg/j2cfg/__init__.py b/j2cfg/j2cfg/__init__.py index 5ec41a8..39d7f07 100644 --- a/j2cfg/j2cfg/__init__.py +++ b/j2cfg/j2cfg/__init__.py @@ -8,8 +8,8 @@ import jinja2 import wcmatch.wcmatch import yaml -from .functions import * from .settings import * +from .functions import * J2CFG_CONFIG_EXT = ['yml', 'yaml', 'json'] @@ -161,12 +161,6 @@ class J2cfg: 'env_vars_preserve': J2CFG_PRESERVE_ENVS, 'env_vars_passthrough': J2CFG_PASSTHROUGH_ENVS, }) - for m in self.modules: - if m in self.kwargs: - print(f'J2cfg: kwargs already has {m} key', - file=sys.stderr) - continue - self.kwargs[m] = importlib.import_module(m) self.j2fs_loaders = { d: jinja2.FileSystemLoader( @@ -181,13 +175,26 @@ class J2cfg: ) def init_env(e: jinja2.Environment): + for m in self.modules: + if m in e.globals: + print(f'J2cfg: globals already has {m} key, module will not be imported', + file=sys.stderr) + continue + e.globals.update({m: importlib.import_module(m)}) + for s in J2CFG_FUNCTIONS: + n = s.__name__ + if n in e.globals: + print(f'J2cfg: globals already has {n} key, function will not be imported', + file=sys.stderr) + continue + e.globals.update({n: s}) for s in J2CFG_FILTERS: n = s.__name__ if n in e.filters: - print(f'J2cfg: filters already has {n} key', + print(f'J2cfg: filters already has {n} key, filter will not be imported', file=sys.stderr) continue - e.filters[n] = s + e.filters.update({n: s}) init_env(self.j2env) diff --git a/j2cfg/j2cfg/functions.py b/j2cfg/j2cfg/functions.py index 3749fa2..d13a169 100644 --- a/j2cfg/j2cfg/functions.py +++ b/j2cfg/j2cfg/functions.py @@ -354,7 +354,7 @@ def join_prefix(prefix: str, *paths) -> str: return rv -J2CFG_FILTERS = [ +J2CFG_FUNCTIONS = [ any_to_env_dict, any_to_str_list, as_cgi_hdr, @@ -380,8 +380,11 @@ J2CFG_FILTERS = [ re_sub, remove_empty_str, remove_non_str, - sh_like_file_to_list, str_split_to_list, uniq, uniq_str_list, ] + +J2CFG_FILTERS = J2CFG_FUNCTIONS + [ + sh_like_file_to_list, +]