diff --git a/Dockerfile b/Dockerfile index e04e12d..a599a8f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -50,9 +50,9 @@ RUN libpython="${PYTHON_SITE_PACKAGES%/*}" ; \ python3 -m compileall -q -j 2 ## Python cache warmup -RUN echo > /tmp/f.j2 ; \ - j2cfg-single /tmp/f.j2 ; \ - rm -f /tmp/f /tmp/f.j2 +RUN j2cfg-single /usr/local/lib/j2cfg/test.j2 /tmp/test ; \ + cat /tmp/test ; echo ; \ + rm -f /tmp/test ## Python cache adjustments RUN d="@$(date '+%s')" ; \ diff --git a/j2cfg/j2cfg/__init__.py b/j2cfg/j2cfg/__init__.py index 0d7d1da..d322592 100644 --- a/j2cfg/j2cfg/__init__.py +++ b/j2cfg/j2cfg/__init__.py @@ -117,13 +117,14 @@ class J2cfg: def merge_dict_default(): 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: if not os.path.isdir(d): continue - for f in wcmatch.wcmatch.WcMatch(d, search_pattern, - flags=search_flags).imatch(): + m = wcmatch.wcmatch.WcMatch(d, search_pattern, + flags=search_flags) + for f in sorted(m.match()): merge_dict_from_file(f) if self.config_file is None: @@ -203,6 +204,9 @@ class J2cfg: continue self.ensure_fs_loader_for(d) dirs.insert(0, d) + if f_in.startswith('/'): + self.ensure_fs_loader_for('/') + dirs.append('/') j2_environ = self.j2env.overlay(loader=jinja2.ChoiceLoader([ self.j2fs_loaders[d] for d in dirs diff --git a/j2cfg/j2cfg/functions.py b/j2cfg/j2cfg/functions.py index 0520b7c..fe38019 100644 --- a/j2cfg/j2cfg/functions.py +++ b/j2cfg/j2cfg/functions.py @@ -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: tpl = j2env.get_template(file_in) 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( lambda x: re.match(r'\s*#', x), lines )) diff --git a/j2cfg/j2cfg/settings.py b/j2cfg/j2cfg/settings.py index dc1e3b2..7bb9eeb 100644 --- a/j2cfg/j2cfg/settings.py +++ b/j2cfg/j2cfg/settings.py @@ -1,9 +1,9 @@ J2CFG_TEMPLATE_EXT = '.j2' J2CFG_PATH = [ - '/etc/angie/j2cfg.dist', - '/etc/angie/j2cfg', '/angie/j2cfg', + '/etc/angie/j2cfg', + '/etc/angie/j2cfg.dist', ] J2CFG_PYTHON_MODULES = [ diff --git a/j2cfg/test.j2 b/j2cfg/test.j2 new file mode 100644 index 0000000..e730ada --- /dev/null +++ b/j2cfg/test.j2 @@ -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 }}