From 816d698d4fbc1fe4b5fabfb2a673af3606e61fc6 Mon Sep 17 00:00:00 2001 From: Matt Robenolt Date: Thu, 23 Apr 2020 21:18:26 -0700 Subject: [PATCH 1/4] Add the ability to import filters Fixes #85 --- jinja2cli/cli.py | 19 +++++++++++++++++-- tests/test_jinja2cli.py | 4 ++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/jinja2cli/cli.py b/jinja2cli/cli.py index 34d5e9b..d418969 100644 --- a/jinja2cli/cli.py +++ b/jinja2cli/cli.py @@ -242,7 +242,7 @@ def _load_json5(): } -def render(template_path, data, extensions, strict=False): +def render(template_path, data, extensions, filters, strict=False): from jinja2 import ( __version__ as jinja_version, Environment, @@ -273,6 +273,13 @@ def render(template_path, data, extensions, strict=False): env.globals["environ"] = lambda key: force_text(os.environ.get(key)) env.globals["get_context"] = lambda: data + if filters: + from jinja2.utils import import_string + + for filter in set(filters): + filter = import_string(filter) + env.filters[filter.__name__] = filter + return env.get_template(os.path.basename(template_path)).render(data) @@ -362,7 +369,7 @@ def cli(opts, args): out = codecs.getwriter("utf8")(out) - out.write(render(template_path, data, extensions, opts.strict)) + out.write(render(template_path, data, extensions, opts.filters, opts.strict)) out.flush() return 0 @@ -430,6 +437,14 @@ def main(): action="append", default=["do", "loopcontrols"], ) + parser.add_option( + "-f", + "--filter", + help="extra jinja2 filters to load", + dest="filters", + action="append", + default=[], + ) parser.add_option( "-D", help="Define template variable in the form of key=value", diff --git a/tests/test_jinja2cli.py b/tests/test_jinja2cli.py index 181a8bd..9c9aac5 100644 --- a/tests/test_jinja2cli.py +++ b/tests/test_jinja2cli.py @@ -10,7 +10,7 @@ def test_relative_path(): path = "./files/template.j2" title = b"\xc3\xb8".decode("utf8") - output = cli.render(path, {"title": title}, []) + output = cli.render(path, {"title": title}, [], []) assert output == title assert type(output) == cli.text_type @@ -20,6 +20,6 @@ def test_absolute_path(): path = os.path.join(absolute_base_path, "files", "template.j2") title = b"\xc3\xb8".decode("utf8") - output = cli.render(path, {"title": title}, []) + output = cli.render(path, {"title": title}, [], []) assert output == title assert type(output) == cli.text_type From c1a95cd9072bf71e9d4e12e3ae448eaa02f9df35 Mon Sep 17 00:00:00 2001 From: Matt Robenolt Date: Thu, 23 Apr 2020 21:23:53 -0700 Subject: [PATCH 2/4] ci: Swap python 3.4 with 3.8 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3ee4ec6..6e0cae6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,10 +3,10 @@ language: python dist: xenial python: - '2.7' - - '3.4' - '3.5' - '3.6' - '3.7' + - '3.8' install: pip install -e .[yaml,toml,tests,xml,hjson,json5] From a93f7208301d59aaa51885ea3f1f7cc2e7020f14 Mon Sep 17 00:00:00 2001 From: rddimon Date: Mon, 4 Mar 2024 15:10:32 +0200 Subject: [PATCH 3/4] bump version --- jinja2cli/__init__.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jinja2cli/__init__.py b/jinja2cli/__init__.py index 360d9a8..18b300a 100644 --- a/jinja2cli/__init__.py +++ b/jinja2cli/__init__.py @@ -6,6 +6,6 @@ """ __author__ = "Matt Robenolt" -__version__ = "0.8.2" +__version__ = "0.8.3" from .cli import main # NOQA diff --git a/setup.py b/setup.py index b04d076..bd0b7df 100755 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ setup( name="jinja2-cli", - version="0.8.2", + version="0.8.3", author="Matt Robenolt", author_email="matt@ydekproductions.com", url="https://github.com/mattrobenolt/jinja2-cli", From a40d7b3c3da84c38f190411034b23462cfcb7a94 Mon Sep 17 00:00:00 2001 From: rddimon Date: Mon, 4 Mar 2024 15:28:41 +0200 Subject: [PATCH 4/4] Remove travis changes --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 6e0cae6..3ee4ec6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,10 +3,10 @@ language: python dist: xenial python: - '2.7' + - '3.4' - '3.5' - '3.6' - '3.7' - - '3.8' install: pip install -e .[yaml,toml,tests,xml,hjson,json5]