From 16f71ba2fd753140a404f7cb7795e943e6a80cb9 Mon Sep 17 00:00:00 2001 From: Uwe Fladrich Date: Tue, 5 Nov 2024 11:32:52 +0100 Subject: [PATCH 1/8] Create test for issue --- tests/jobs/test_conditionals.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/jobs/test_conditionals.py b/tests/jobs/test_conditionals.py index 23c91e4..147825b 100644 --- a/tests/jobs/test_conditionals.py +++ b/tests/jobs/test_conditionals.py @@ -1,5 +1,6 @@ import yaml +from scriptengine.engines import SimpleScriptEngine from scriptengine.yaml.parser import parse @@ -31,3 +32,19 @@ def test_when_false(capsys): j.run({}) captured = capsys.readouterr() assert "Hello, world!" not in captured.out + + +def test_when_clause_parses_noparse_parameter(capsys): + s = from_yaml( + """ + - base.context: + foo: me + bar: !noparse_jinja "{{ foo }}" + - when: "{{ bar == 'me' }}" + base.echo: + msg: Hello! + """ + ) + SimpleScriptEngine().run(s, context={}) + captured = capsys.readouterr() + assert "Hello!" in captured.out From 964fce88b6b89b53cd334019c2bc0497d415aa5b Mon Sep 17 00:00:00 2001 From: Uwe Fladrich Date: Tue, 5 Nov 2024 11:43:43 +0100 Subject: [PATCH 2/8] Add Jinja filter 'render' --- src/scriptengine/jinja.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/scriptengine/jinja.py b/src/scriptengine/jinja.py index 58cd13d..3483cfa 100644 --- a/src/scriptengine/jinja.py +++ b/src/scriptengine/jinja.py @@ -45,6 +45,11 @@ def path_join(pathlist): return os.path.join(*pathlist) +@jinja2.pass_context +def render_filter(context, expression): + return render(expression, context) + + def filters(): """Return all defined Jinja2 filters by their name and corresponding function""" return { @@ -55,6 +60,7 @@ def filters(): "dirname": dirname, "exists": exists, "path_join": path_join, + "render": render_filter, } From 02eb3e60ae6b14bf204c72d3837c6fd72551eeef Mon Sep 17 00:00:00 2001 From: Uwe Fladrich Date: Tue, 5 Nov 2024 11:44:25 +0100 Subject: [PATCH 3/8] Update test --- tests/jobs/test_conditionals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/jobs/test_conditionals.py b/tests/jobs/test_conditionals.py index 147825b..54852e8 100644 --- a/tests/jobs/test_conditionals.py +++ b/tests/jobs/test_conditionals.py @@ -40,7 +40,7 @@ def test_when_clause_parses_noparse_parameter(capsys): - base.context: foo: me bar: !noparse_jinja "{{ foo }}" - - when: "{{ bar == 'me' }}" + - when: "{{ bar|render == 'me' }}" base.echo: msg: Hello! """ From 7e4991ed8da88267b55744d9a5e03452972519ee Mon Sep 17 00:00:00 2001 From: Uwe Fladrich Date: Tue, 5 Nov 2024 11:44:46 +0100 Subject: [PATCH 4/8] Update jinja2 dependency The jinja2.pass_context decorator needs 3.0.0 at least --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 145b037..59c56ff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,7 @@ dependencies = [ "python-dateutil", "deepmerge", "PyYAML", - "jinja2", + "jinja2>=3.0.0", ] [project.optional-dependencies] From c40d10f0c8bed9457e582252186431f64f7e721c Mon Sep 17 00:00:00 2001 From: Uwe Fladrich Date: Tue, 5 Nov 2024 12:44:14 +0100 Subject: [PATCH 5/8] Update documentation --- docs/sphinx/scripts.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/sphinx/scripts.rst b/docs/sphinx/scripts.rst index 766694f..fb398a7 100644 --- a/docs/sphinx/scripts.rst +++ b/docs/sphinx/scripts.rst @@ -437,6 +437,23 @@ path_join base.echo: msg: "{{ ['foo', 'bar.txt'] | path_join }}" +Other filters +^^^^^^^^^^^^^ + +render + Renders the expression (e.g. a variable) with Jinja2 and the current context. This can be used, for + example, to explicitly render context parameters that have been set with the `!noparse` tag:: + + - base.context: + foo: me + bar: !noparse "{{ foo }}" + - when "{{ bar|render == 'me' }}" + base.echo: + msg: "It is {{ bar }}!" + + Without using the `render` filter in the example, the `when` clause would evaluate to `false` because + the value of `bar` would still be `"{{ foo }}"` because of `!noparse`. + .. _PyYAML: https://pyyaml.org .. _RFC5545: https://tools.ietf.org/html/rfc5545 From 95a33830fb5e07402dd46ff09fdf33a6aa5e2ba6 Mon Sep 17 00:00:00 2001 From: Uwe Fladrich Date: Tue, 5 Nov 2024 13:23:52 +0100 Subject: [PATCH 6/8] Update CHANGES --- CHANGES.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.txt b/CHANGES.txt index 7f5d613..580ab9b 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,7 @@ Next release Features -------- - #119: New Jinja filter: increment_datetime (thanks @jmrgonza) +- #122: New Jinja filter: render (solves #121) Internal changes ---------------- From 5cfc40010c2a44a98a04102e53fb3098f2740590 Mon Sep 17 00:00:00 2001 From: Uwe Fladrich Date: Tue, 5 Nov 2024 13:59:23 +0100 Subject: [PATCH 7/8] Some fixes in the documentation --- docs/sphinx/scripts.rst | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/sphinx/scripts.rst b/docs/sphinx/scripts.rst index fb398a7..0a3764d 100644 --- a/docs/sphinx/scripts.rst +++ b/docs/sphinx/scripts.rst @@ -442,17 +442,20 @@ Other filters render Renders the expression (e.g. a variable) with Jinja2 and the current context. This can be used, for - example, to explicitly render context parameters that have been set with the `!noparse` tag:: + example, to explicitly render context parameters that have been set with the ``!noparse`` tag:: - base.context: foo: me bar: !noparse "{{ foo }}" - - when "{{ bar|render == 'me' }}" + - when: "{{ bar|render == 'me' }}" base.echo: msg: "It is {{ bar }}!" - Without using the `render` filter in the example, the `when` clause would evaluate to `false` because - the value of `bar` would still be `"{{ foo }}"` because of `!noparse`. + Without using the ``render`` filter in the example, the ``when`` clause would evaluate to ``false`` + because the value of ``bar`` would still be ``"{{ foo }}"`` as a consequence of ``!noparse``. + +.. versionadded:: 1.1 + ``render`` filter added. .. _PyYAML: https://pyyaml.org From 5c9d5c76bde1a58b7bccbc26576e1817d67ff7cc Mon Sep 17 00:00:00 2001 From: Uwe Fladrich Date: Tue, 5 Nov 2024 14:03:01 +0100 Subject: [PATCH 8/8] Further left-over documentation fix --- docs/sphinx/scripts.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/sphinx/scripts.rst b/docs/sphinx/scripts.rst index 0a3764d..178a46b 100644 --- a/docs/sphinx/scripts.rst +++ b/docs/sphinx/scripts.rst @@ -389,6 +389,9 @@ increment_datetime base.context: date_time: "{{ '2022-01-01 00:00:00' | datetime | increment_datetime(days=1, hours=6)}}"" +.. versionadded:: 1.1 + ``increment_datetime`` filter added. + date Converts a string to a ``datetime.date`` object::