From 841fb8b361677c1201c11aa589216e8137c0581e Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Tue, 2 Jun 2026 21:08:00 +0200 Subject: [PATCH] cmds.projects.lib.templates.tmpl_render(): search_path Add an additional keyword-argument search_path to templates.tmpl_render(). It allows to specifiy a list of directory paths in which a template of a given name can be found. It defaults to [], in which case only the built-in templates are considered. Otherwise file locations are tried first, then the built-in templates. Signed-off-by: Jan Lindemann --- .../jw/pkg/cmds/projects/lib/templates.py | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/python/jw/pkg/cmds/projects/lib/templates.py b/src/python/jw/pkg/cmds/projects/lib/templates.py index eec74085..d7388b95 100644 --- a/src/python/jw/pkg/cmds/projects/lib/templates.py +++ b/src/python/jw/pkg/cmds/projects/lib/templates.py @@ -1,6 +1,8 @@ import textwrap -def format_lines(template, values, li_quote, li_delimiter): +def format_lines( + template: str, values: dict[str, str], li_quote: bool, li_delimiter: str +) -> str: def __format_value(val): if not li_quote: @@ -63,11 +65,31 @@ _templates = { } def tmpl_render( - template_name: str, values, li_quote = False, li_delimiter = '\n' + template_name: str, + values, + li_quote = False, + li_delimiter = '\n', + search_path: list[str] = [] ) -> str: - return format_lines( - textwrap.dedent(_templates[template_name]), - values, - li_quote = li_quote, - li_delimiter = li_delimiter, - ) + + def __format(template: str) -> str: + return format_lines( + template, + values, + li_quote = li_quote, + li_delimiter = li_delimiter, + ) + + for d in search_path: + path = d + '/' + template_name + try: + with open(path, 'r') as f: + template = f.read() + return __format(template) + except FileNotFoundError: + pass + + raw = _templates.get(template_name, None) + if raw is None: + raise Exception(f'Failed to find template "{template_name}"') + return __format(textwrap.dedent(raw))