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:
@@ -8,8 +8,8 @@ import jinja2
|
|||||||
import wcmatch.wcmatch
|
import wcmatch.wcmatch
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from .functions import *
|
|
||||||
from .settings import *
|
from .settings import *
|
||||||
|
from .functions import *
|
||||||
|
|
||||||
|
|
||||||
J2CFG_CONFIG_EXT = ['yml', 'yaml', 'json']
|
J2CFG_CONFIG_EXT = ['yml', 'yaml', 'json']
|
||||||
@@ -161,12 +161,6 @@ class J2cfg:
|
|||||||
'env_vars_preserve': J2CFG_PRESERVE_ENVS,
|
'env_vars_preserve': J2CFG_PRESERVE_ENVS,
|
||||||
'env_vars_passthrough': J2CFG_PASSTHROUGH_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 = {
|
self.j2fs_loaders = {
|
||||||
d: jinja2.FileSystemLoader(
|
d: jinja2.FileSystemLoader(
|
||||||
@@ -181,13 +175,26 @@ class J2cfg:
|
|||||||
)
|
)
|
||||||
|
|
||||||
def init_env(e: jinja2.Environment):
|
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:
|
for s in J2CFG_FILTERS:
|
||||||
n = s.__name__
|
n = s.__name__
|
||||||
if n in e.filters:
|
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)
|
file=sys.stderr)
|
||||||
continue
|
continue
|
||||||
e.filters[n] = s
|
e.filters.update({n: s})
|
||||||
|
|
||||||
init_env(self.j2env)
|
init_env(self.j2env)
|
||||||
|
|
||||||
|
@@ -354,7 +354,7 @@ def join_prefix(prefix: str, *paths) -> str:
|
|||||||
return rv
|
return rv
|
||||||
|
|
||||||
|
|
||||||
J2CFG_FILTERS = [
|
J2CFG_FUNCTIONS = [
|
||||||
any_to_env_dict,
|
any_to_env_dict,
|
||||||
any_to_str_list,
|
any_to_str_list,
|
||||||
as_cgi_hdr,
|
as_cgi_hdr,
|
||||||
@@ -380,8 +380,11 @@ J2CFG_FILTERS = [
|
|||||||
re_sub,
|
re_sub,
|
||||||
remove_empty_str,
|
remove_empty_str,
|
||||||
remove_non_str,
|
remove_non_str,
|
||||||
sh_like_file_to_list,
|
|
||||||
str_split_to_list,
|
str_split_to_list,
|
||||||
uniq,
|
uniq,
|
||||||
uniq_str_list,
|
uniq_str_list,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
J2CFG_FILTERS = J2CFG_FUNCTIONS + [
|
||||||
|
sh_like_file_to_list,
|
||||||
|
]
|
||||||
|
Reference in New Issue
Block a user