1
0

fix inconsistences in j2cfg

This commit is contained in:
Konstantin Demin 2024-07-20 18:29:09 +03:00
parent 288d27f9f2
commit 1dda7066c9
Signed by: krd
GPG Key ID: 4D56F87A8BA65FD0
5 changed files with 145 additions and 9 deletions

View File

@ -50,9 +50,9 @@ RUN libpython="${PYTHON_SITE_PACKAGES%/*}" ; \
python3 -m compileall -q -j 2 python3 -m compileall -q -j 2
## Python cache warmup ## Python cache warmup
RUN echo > /tmp/f.j2 ; \ RUN j2cfg-single /usr/local/lib/j2cfg/test.j2 /tmp/test ; \
j2cfg-single /tmp/f.j2 ; \ cat /tmp/test ; echo ; \
rm -f /tmp/f /tmp/f.j2 rm -f /tmp/test
## Python cache adjustments ## Python cache adjustments
RUN d="@$(date '+%s')" ; \ RUN d="@$(date '+%s')" ; \

View File

@ -117,13 +117,14 @@ class J2cfg:
def merge_dict_default(): def merge_dict_default():
search_pattern = '|'.join(['*.' + ext for ext in J2CFG_CONFIG_EXT]) search_pattern = '|'.join(['*.' + ext for ext in J2CFG_CONFIG_EXT])
search_flags = wcmatch.wcmatch.RECURSIVE | wcmatch.wcmatch.SYMLINKS search_flags = wcmatch.wcmatch.SYMLINKS
for d in self.config_path: for d in self.config_path:
if not os.path.isdir(d): if not os.path.isdir(d):
continue continue
for f in wcmatch.wcmatch.WcMatch(d, search_pattern, m = wcmatch.wcmatch.WcMatch(d, search_pattern,
flags=search_flags).imatch(): flags=search_flags)
for f in sorted(m.match()):
merge_dict_from_file(f) merge_dict_from_file(f)
if self.config_file is None: if self.config_file is None:
@ -203,6 +204,9 @@ class J2cfg:
continue continue
self.ensure_fs_loader_for(d) self.ensure_fs_loader_for(d)
dirs.insert(0, d) dirs.insert(0, d)
if f_in.startswith('/'):
self.ensure_fs_loader_for('/')
dirs.append('/')
j2_environ = self.j2env.overlay(loader=jinja2.ChoiceLoader([ j2_environ = self.j2env.overlay(loader=jinja2.ChoiceLoader([
self.j2fs_loaders[d] for d in dirs self.j2fs_loaders[d] for d in dirs

View File

@ -74,7 +74,7 @@ def str_list_re_sub(a: list, pattern, repl, count=0, flags=0) -> list:
def sh_like_file_to_list(j2env, file_in: str) -> list: def sh_like_file_to_list(j2env, file_in: str) -> list:
tpl = j2env.get_template(file_in) tpl = j2env.get_template(file_in)
text = pathlib.Path(tpl.filename).read_text(encoding='utf-8') text = pathlib.Path(tpl.filename).read_text(encoding='utf-8')
lines = re.split(r'\r\n', text) lines = re.split(r'[\r\n]', text)
return list(itertools.filterfalse( return list(itertools.filterfalse(
lambda x: re.match(r'\s*#', x), lines lambda x: re.match(r'\s*#', x), lines
)) ))

View File

@ -1,9 +1,9 @@
J2CFG_TEMPLATE_EXT = '.j2' J2CFG_TEMPLATE_EXT = '.j2'
J2CFG_PATH = [ J2CFG_PATH = [
'/etc/angie/j2cfg.dist',
'/etc/angie/j2cfg',
'/angie/j2cfg', '/angie/j2cfg',
'/etc/angie/j2cfg',
'/etc/angie/j2cfg.dist',
] ]
J2CFG_PYTHON_MODULES = [ J2CFG_PYTHON_MODULES = [

132
j2cfg/test.j2 Normal file
View File

@ -0,0 +1,132 @@
j2cfg:
{{ j2cfg }}
{% set x = [2,3,1,2] %}
x = {{ x }}
uniq_list:
{{ x | uniq_list }}
{% set x = ['2',3,'1','2'] %}
x = {{ x }}
list_remove_non_str:
{{ x | list_remove_non_str }}
{% set x = ['2','','1','2'] %}
x = {{ x }}
list_remove_empty_str:
{{ x | list_remove_empty_str }}
{% set x = ['2','3','1','2'] %}
x = {{ x }}
uniq_str_list:
{{ x | uniq_str_list }}
{% set x = '2 3 1 2 ' %}
"x = {{ x }}"
str_split_to_list:
{{ x | str_split_to_list }}
{% set x = '2:3::1:2:' %}
"x = {{ x }}"
str_split_to_list(':'):
{{ x | str_split_to_list(':') }}
{% set x = [1,2,3,4] %}
x = {{ x }}
is_sequence:
{{ x | is_sequence }}
{% set x = {1:2,3:4} %}
x = {{ x }}
is_sequence:
{{ x | is_sequence }}
{% set x = [1,2,3,4] %}
x = {{ x }}
is_mapping:
{{ x | is_mapping }}
{% set x = {1:2,3:4} %}
x = {{ x }}
is_mapping:
{{ x | is_mapping }}
{% set x = '1 2 3 4' %}
"x = {{ x }}"
any_to_str_list:
{{ x | any_to_str_list }}
{% set x = [1,2,3,4] %}
x = {{ x }}
any_to_str_list:
{{ x | any_to_str_list }}
{% set x = 3.1415926 %}
x = {{ x }}
any_to_str_list:
{{ x | any_to_str_list }}
{% set x = ['a2','b3','c1','d2'] %}
x = {{ x }}
is_str_list_re_match('[ab]'):
{{ x | is_str_list_re_match('[ab]') }}
is_str_list_re_match('[mn]'):
{{ x | is_str_list_re_match('[mn]') }}
{% set x = ['a2','b3','c1','d2'] %}
x = {{ x }}
is_str_list_re_fullmatch('[ab]'):
{{ x | is_str_list_re_fullmatch('[ab]') }}
is_str_list_re_fullmatch('[ab][12]'):
{{ x | is_str_list_re_fullmatch('[ab][12]') }}
{% set x = ['a2','b3','c1','d2'] %}
x = {{ x }}
str_list_re_match('[ab]'):
{{ x | str_list_re_match('[ab]') }}
str_list_re_match('[mn]'):
{{ x | str_list_re_match('[mn]') }}
{% set x = ['a2','b3','c1','d2'] %}
x = {{ x }}
str_list_re_fullmatch('[ab]'):
{{ x | str_list_re_fullmatch('[ab]') }}
str_list_re_fullmatch('[ab][12]'):
{{ x | str_list_re_fullmatch('[ab][12]') }}
{% set x = ['a2b','b3b','c1f','d2g'] %}
x = {{ x }}
str_list_re_sub('[ab]', '_'):
{{ x | str_list_re_sub('[ab]', '_') }}
str_list_re_sub('[mn]', '_'):
{{ x | str_list_re_sub('[mn]', '_') }}
{% set x = 'j2cfg-multi.py' %}
x = {{ x }}
sh_like_file_to_list:
{{ 'j2cfg-multi.py' | sh_like_file_to_list }}
{% set x = 'Accept-Encoding' %}
x = {{ x }}
as_cgi_header:
{{ x | as_cgi_header }}
{% set x = '_Permissions-Policy--' %}
x = {{ x }}
as_cgi_header:
{{ x | as_cgi_header }}
{% set x = 'VAR1=Etc/UTC' %}
x = {{ x }}
env_any_to_str_list:
{{ x | env_any_to_str_list }}
{% set x = ['VAR1=Etc/UTC', 'VAR2=', 'VAR3', '4VAR4=yeah', 'VAR5=yeah', 'VAR5=not-yeah'] %}
x = {{ x }}
env_any_to_str_list:
{{ x | env_any_to_str_list }}
{% set x = { 'VAR1': 'Etc/UTC', 'VAR2': '', 'VAR3': None, '4VAR4': 'yeah', 'VAR5=not': 'yeah', 'VAR5=real yeah': None, 'VAR6': {'pi': 3.1415926}, 'VAR7': ['pi', 3.1415926] } %}
x = {{ x }}
env_any_to_str_list:
{{ x | env_any_to_str_list }}