1
0

j2cfg: improve loading

- provide almost all implemented helper functions as functions and filters (was: only filters)
- improve diagnostic messages during load
This commit is contained in:
2025-06-06 11:31:38 +03:00
parent 48f13f97a3
commit d0ae5d79c9
2 changed files with 21 additions and 11 deletions

View File

@@ -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)

View File

@@ -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,
]