From 238d70510375dce9fa8641e75dd52cd11244ed3b Mon Sep 17 00:00:00 2001 From: Charlie Liu Date: Wed, 14 Nov 2018 17:42:16 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20new=20target=20and=20template?= =?UTF-8?q?=20global=20variables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds new global variables for the target and template files. Closes https://github.com/moremoban/moban/issues/29 --- moban/engine.py | 4 ++++ tests/fixtures/globals/nested.template | 1 + tests/fixtures/globals/variables.template | 3 +++ tests/fixtures/globals/variables.yml | 1 + tests/test_engine.py | 22 ++++++++++++++++++++++ 5 files changed, 31 insertions(+) create mode 100644 tests/fixtures/globals/nested.template create mode 100644 tests/fixtures/globals/variables.template create mode 100644 tests/fixtures/globals/variables.yml diff --git a/moban/engine.py b/moban/engine.py index 571b9e10..8ba66a91 100644 --- a/moban/engine.py +++ b/moban/engine.py @@ -50,6 +50,8 @@ def __init__(self, template_dirs, context_dirs): def render_to_file(self, template_file, data_file, output_file): template = self.jj2_environment.get_template(template_file) data = self.context.get_data(data_file) + template.globals['__target__'] = output_file + template.globals['__template__'] = template.name reporter.report_templating(template_file, output_file) rendered_content = template.render(**data) @@ -96,6 +98,8 @@ def _file_permissions_copy(self, template_file, output_file): utils.file_permissions_copy(true_template_file, output_file) def _apply_template(self, template, data, output): + template.globals['__target__'] = output + template.globals['__template__'] = template.name temp_file_path = get_template_path(self.template_dirs, template) rendered_content = template.render(**data) rendered_content = utils.strip_off_trailing_new_lines(rendered_content) diff --git a/tests/fixtures/globals/nested.template b/tests/fixtures/globals/nested.template new file mode 100644 index 00000000..f28bb6c4 --- /dev/null +++ b/tests/fixtures/globals/nested.template @@ -0,0 +1 @@ +{% include 'variables.template' %} \ No newline at end of file diff --git a/tests/fixtures/globals/variables.template b/tests/fixtures/globals/variables.template new file mode 100644 index 00000000..f2576e70 --- /dev/null +++ b/tests/fixtures/globals/variables.template @@ -0,0 +1,3 @@ +template: {{ __template__ }} +target: {{ __target__ }} +{{ test }} \ No newline at end of file diff --git a/tests/fixtures/globals/variables.yml b/tests/fixtures/globals/variables.yml new file mode 100644 index 00000000..a0067b10 --- /dev/null +++ b/tests/fixtures/globals/variables.yml @@ -0,0 +1 @@ +test: here \ No newline at end of file diff --git a/tests/test_engine.py b/tests/test_engine.py index 9d885945..f8a173e4 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -110,3 +110,25 @@ def test_globals(): content = output_file.read() eq_(content, "world\n\ntest") os.unlink(output) + + +def test_global_template_variables(): + output = "test.txt" + path = os.path.join("tests", "fixtures", "globals") + engine = Engine([path], path) + engine.render_to_file("variables.template", "variables.yml", output) + with open(output, "r") as output_file: + content = output_file.read() + eq_(content, "template: variables.template\ntarget: test.txt\nhere") + os.unlink(output) + + +def test_nested_global_template_variables(): + output = "test.txt" + path = os.path.join("tests", "fixtures", "globals") + engine = Engine([path], path) + engine.render_to_file("nested.template", "variables.yml", output) + with open(output, "r") as output_file: + content = output_file.read() + eq_(content, "template: nested.template\ntarget: test.txt\nhere") + os.unlink(output)