diff --git a/calculate/templates/template_engine.py b/calculate/templates/template_engine.py index c957ccf..8e81f6f 100644 --- a/calculate/templates/template_engine.py +++ b/calculate/templates/template_engine.py @@ -838,6 +838,10 @@ class CalculateExtension(Extension): self.parameters_processor = parameters_processor self.template_type = DIR + # Флаг, указывающий, что тег calculate уже был разобран. Нужен для + # того, чтобы проверять единственность тега calculate. + self.calculate_parsed = False + self.tags = {'calculate', 'save', 'set_var'} self.CONDITION_TOKENS_TYPES = {'eq', 'ne', 'lt', 'gt', 'lteq', 'gteq'} self.LITERAL_TOKENS_TYPES = {'string', 'integer', 'float'} @@ -908,10 +912,15 @@ class CalculateExtension(Extension): def parse_calculate(self): '''Метод для разбора тега calculate, содержащего значения параметров и условия выполнения шаблона.''' - expect_comma_flag = False - lineno = next(self.stream).lineno + if self.calculate_parsed: + raise TemplateSyntaxError( + "template can only have one calculate tag.", + lineno=lineno) + + expect_comma_flag = False + while self.stream.current.type != 'block_end': if expect_comma_flag: self.stream.expect('comma') @@ -967,6 +976,7 @@ class CalculateExtension(Extension): # lineno=lineno) # return nodes.Output([save_node], lineno=lineno) + self.calculate_parsed = True return nodes.Output([nodes.Const('')], lineno=lineno) def _is_variable_name(self, token): @@ -1396,6 +1406,7 @@ class TemplateEngine: self._parameters_object self.calculate_extension.template_type = template_type + self.calculate_extension.calculate_parsed = False template = self.environment.get_template(template_path) self.parameters_processor.check_postparse_parameters() @@ -1423,6 +1434,7 @@ class TemplateEngine: self._parameters_object self.calculate_extension.template_type = template_type + self.calculate_extension.calculate_parsed = False template = self.environment.from_string(string) self.parameters_processor.check_postparse_parameters() diff --git a/tests/templates/test_template_engine.py b/tests/templates/test_template_engine.py index f2d5552..425d712 100644 --- a/tests/templates/test_template_engine.py +++ b/tests/templates/test_template_engine.py @@ -1,7 +1,8 @@ import os import pytest from calculate.templates.template_engine import TemplateEngine, Variables,\ - ConditionFailed, DIR, FILE + ConditionFailed, DIR, FILE,\ + TemplateSyntaxError from calculate.templates.template_processor import TemplateExecutor @@ -25,6 +26,14 @@ class TestTemplateEngine(): output_parameters.path == '/etc/path' and output_parameters.force) + def test_if_an_input_template_contains_more_than_one_calculate_tag__the_template_engine_raises_the_TemplateSyntaxError_exception(self): + input_template = '''{% calculate name = 'filename' %} + {% calculate path = '/etc/path'%} + {% calculate force %}''' + template_engine = TemplateEngine(appends_set=APPENDS_SET) + with pytest.raises(TemplateSyntaxError): + template_engine.process_template_from_string(input_template, DIR) + def test_if_an_input_template_binded_with_datavars_module__variables_available_in_a_template(self): input_template = '''{% calculate name = vars.var_1, path = vars.var_2, autoupdate %}''' datavars_module = Variables({'vars': @@ -71,10 +80,8 @@ class TestTemplateEngine(): pytest.fail('Unexpected ConditionFailed exception.') def test_if_an_input_template_contains_several_conditions_and_it_is_False__the_template_engine_raises_ConditionFailed_exception(self): - input_template = '''{% calculate name = vars.var_1 %} - {% calculate env = 'other_vars'%} - {% calculate var_4 < var_5 %} - {% calculate path = var_3, var_6 == 'value' %}''' + input_template = '''{% calculate name = vars.var_1, env = 'other_vars', + var_4 < var_5, path = var_3, var_6 == 'value' %}''' datavars_module = Variables({'vars': Variables({'var_1': 'filename', @@ -90,10 +97,9 @@ class TestTemplateEngine(): template_engine.process_template_from_string(input_template, DIR) def test_if_an_input_template_contains_several_calculate_tags__the_template_engine_will_parse_them_all_and_will_contain_all_parameters_and_result_of_all_conditions(self): - input_template = '''{% calculate name = vars.var_1 %} - {% calculate env = 'other_vars, vars' %} - {% calculate var_4 > var_5 %} - {% calculate path = var_3, var_6 == 'value' %}''' + input_template = '''{% calculate name = vars.var_1, + env = 'other_vars, vars', var_4 > var_5, + path = var_3, var_6 == 'value' %}''' datavars_module = Variables({'vars': Variables({'var_1': 'filename', 'var_2': '/etc/path'}), @@ -111,9 +117,9 @@ class TestTemplateEngine(): output_parameters.env == {'other_vars', 'vars'}) def test_if_an_input_template_contains_variables_in_its_text__the_rendered_text_will_contain_values_of_this_variables(self): - input_template = '''{% calculate name = 'filename', force -%} + input_template = '''{% calculate name = 'filename', force, + env = 'vars_1' -%} parameter_1 = {{ vars_2.var_3 }} - {% calculate env = 'vars_1' -%} parameter_2 = {{ vars_2.var_4 }} parameter_3 = {{ var_1 }}''' output_text = '''parameter_1 = value_1 @@ -193,8 +199,7 @@ class TestTemplateEngine(): def test_if_an_input_template_calculate_tag_contains_pkg_function_with_an_existing_package_in_its_argument__the_pkg_function_returns_version_value_of_the_package_from_package_parameter_without_any_exceptions(self): input_template = '''{% calculate name = 'filename', - package='test-category/test-package', force -%} - {% calculate pkg() < 2.7 -%}''' + package='test-category/test-package', force, pkg() < 2.7 -%}''' try: template_engine = TemplateEngine(appends_set=APPENDS_SET, diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_10/root/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_10/root/.calculate_directory index c9154f6..7fd928f 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_10/root/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_10/root/.calculate_directory @@ -1,2 +1,2 @@ -{% calculate append = 'skip', action = 'install', package = 'test-category/new-package' %} -{% calculate autoupdate %} +{% calculate append = 'skip', action = 'install', +package = 'test-category/new-package', autoupdate %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_11/root/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_11/root/.calculate_directory index e82b02e..763f36b 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_11/root/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_11/root/.calculate_directory @@ -1,2 +1,2 @@ -{% calculate append = 'skip', package = 'test-category/test-package' %} -{% calculate path = '/etc' %} +{% calculate append = 'skip', package = 'test-category/test-package', +path = '/etc' %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_12/root_0/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_12/root_0/.calculate_directory index a07d12b..7b1b0d6 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_12/root_0/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_12/root_0/.calculate_directory @@ -1,2 +1,2 @@ -{% calculate append = 'skip', package = 'test-category/test-package' %} -{% calculate action = 'install' %} +{% calculate append = 'skip', package = 'test-category/test-package', +action = 'install' %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_12/root_1/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_12/root_1/.calculate_directory index 22931ca..8de024c 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_12/root_1/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_12/root_1/.calculate_directory @@ -1,2 +1,2 @@ -{% calculate append = 'skip', package = 'test-category/test-package' %} -{% calculate action = 'update' %} +{% calculate append = 'skip', package = 'test-category/test-package', +action = 'update' %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_2/root/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_2/root/.calculate_directory index e60987e..78e1164 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_2/root/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_2/root/.calculate_directory @@ -1,2 +1,2 @@ -{% calculate append = 'skip', action = 'install', package = 'test-category/test-package' %} -{% calculate path = '/etc' %} +{% calculate append = 'skip', action = 'install', +package = 'test-category/test-package', path = '/etc' %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_21/root/dir_22/dir_21/file_0 b/tests/templates/testfiles/test_dir_processor_root/templates_21/root/dir_22/dir_21/file_0 index a549fd6..d93686e 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_21/root/dir_22/dir_21/file_0 +++ b/tests/templates/testfiles/test_dir_processor_root/templates_21/root/dir_22/dir_21/file_0 @@ -1,5 +1,5 @@ -{% calculate append = 'join', format = 'bind' %} -{% calculate merge = 'test-category/test-package, test-category/new-package' -%} +{% calculate append = 'join', format = 'bind', +merge = 'test-category/test-package, test-category/new-package' -%} options { parameter-1 {{ variables.variable_1 }}; parameter-2 {{ variables.variable_2 }}; diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_22/root/dir_24/dir_24/file_0 b/tests/templates/testfiles/test_dir_processor_root/templates_22/root/dir_24/dir_24/file_0 index 7115ab4..bac0e41 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_22/root/dir_24/dir_24/file_0 +++ b/tests/templates/testfiles/test_dir_processor_root/templates_22/root/dir_24/dir_24/file_0 @@ -1,5 +1,5 @@ -{% calculate append = 'join', format = 'bind' %} -{% calculate merge = 'test-category/new-package' -%} +{% calculate append = 'join', format = 'bind', +merge = 'test-category/new-package' -%} options { parameter-1 {{ variables.variable_1 }}; parameter-2 {{ variables.variable_2 }}; diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_22/root/dir_26/file_0 b/tests/templates/testfiles/test_dir_processor_root/templates_22/root/dir_26/file_0 index dd43f07..2de7914 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_22/root/dir_26/file_0 +++ b/tests/templates/testfiles/test_dir_processor_root/templates_22/root/dir_26/file_0 @@ -1,5 +1,5 @@ -{% calculate append = 'join', format = 'bind' -%} -{% calculate merge = 'test-category/test-package' -%} +{% calculate append = 'join', format = 'bind', +merge = 'test-category/test-package' -%} options { parameter-1 {{ variables.variable_1 }}; parameter-2 {{ variables.variable_2 }}; diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_23/root/dir_27/dir_28/file_0 b/tests/templates/testfiles/test_dir_processor_root/templates_23/root/dir_27/dir_28/file_0 index 8d0c697..083f103 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_23/root/dir_27/dir_28/file_0 +++ b/tests/templates/testfiles/test_dir_processor_root/templates_23/root/dir_27/dir_28/file_0 @@ -1,5 +1,5 @@ -{% calculate append = 'join', format = 'bind' %} -{% calculate merge = 'test-category/test-package' %} +{% calculate append = 'join', format = 'bind', +merge = 'test-category/test-package' %} options { parameter-1 {{ variables.variable_1 }}; parameter-2 {{ variables.variable_2 }}; diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_23/root/file_9 b/tests/templates/testfiles/test_dir_processor_root/templates_23/root/file_9 index 449a488..ece3ae3 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_23/root/file_9 +++ b/tests/templates/testfiles/test_dir_processor_root/templates_23/root/file_9 @@ -1,5 +1,5 @@ -{% calculate append = 'join', format = 'bind' -%} -{% calculate package = 'test-category/test-package' -%} +{% calculate append = 'join', format = 'bind', +package = 'test-category/test-package' -%} options { parameter-1 {{ variables.variable_1 }}; parameter-2 {{ variables.variable_2 }}; diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_24/root/file_10 b/tests/templates/testfiles/test_dir_processor_root/templates_24/root/file_10 index 8ac874d..0b3aca5 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_24/root/file_10 +++ b/tests/templates/testfiles/test_dir_processor_root/templates_24/root/file_10 @@ -1,5 +1,5 @@ -{% calculate append = 'join', format = 'bind', path = '/etc' -%} -{% calculate install.number > 100 -%} +{% calculate append = 'join', format = 'bind', path = '/etc', +install.number > 100 -%} options { parameter-1 {{ variables.variable_1 }}; parameter-2 {{ variables.variable_2 }}; diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_26/root/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_26/root/.calculate_directory index e60987e..78e1164 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_26/root/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_26/root/.calculate_directory @@ -1,2 +1,2 @@ -{% calculate append = 'skip', action = 'install', package = 'test-category/test-package' %} -{% calculate path = '/etc' %} +{% calculate append = 'skip', action = 'install', +package = 'test-category/test-package', path = '/etc' %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_3/root/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_3/root/.calculate_directory index e60987e..78e1164 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_3/root/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_3/root/.calculate_directory @@ -1,2 +1,2 @@ -{% calculate append = 'skip', action = 'install', package = 'test-category/test-package' %} -{% calculate path = '/etc' %} +{% calculate append = 'skip', action = 'install', +package = 'test-category/test-package', path = '/etc' %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_32/install/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_32/install/.calculate_directory index 969b628..78e1164 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_32/install/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_32/install/.calculate_directory @@ -1,3 +1,2 @@ {% calculate append = 'skip', action = 'install', -package = 'test-category/test-package' %} -{% calculate path = '/etc' %} +package = 'test-category/test-package', path = '/etc' %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_32/update/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_32/update/.calculate_directory index 2c74513..5abe2f1 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_32/update/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_32/update/.calculate_directory @@ -1,3 +1,2 @@ {% calculate append = 'skip', action = 'update', -package = 'test-category/test-package' %} -{% calculate path = '/etc' %} +package = 'test-category/test-package', path = '/etc' %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_33/install/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_33/install/.calculate_directory index da1e1b6..3c0eee1 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_33/install/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_33/install/.calculate_directory @@ -1,2 +1 @@ -{% calculate append = 'skip', action = 'install' %} -{% calculate path = '/etc' %} +{% calculate append = 'skip', action = 'install', path = '/etc' %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_33/update/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_33/update/.calculate_directory index 2c74513..5abe2f1 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_33/update/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_33/update/.calculate_directory @@ -1,3 +1,2 @@ {% calculate append = 'skip', action = 'update', -package = 'test-category/test-package' %} -{% calculate path = '/etc' %} +package = 'test-category/test-package', path = '/etc' %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_4/root/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_4/root/.calculate_directory index e60987e..78e1164 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_4/root/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_4/root/.calculate_directory @@ -1,2 +1,2 @@ -{% calculate append = 'skip', action = 'install', package = 'test-category/test-package' %} -{% calculate path = '/etc' %} +{% calculate append = 'skip', action = 'install', +package = 'test-category/test-package', path = '/etc' %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_5/root/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_5/root/.calculate_directory index e60987e..78e1164 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_5/root/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_5/root/.calculate_directory @@ -1,2 +1,2 @@ -{% calculate append = 'skip', action = 'install', package = 'test-category/test-package' %} -{% calculate path = '/etc' %} +{% calculate append = 'skip', action = 'install', +package = 'test-category/test-package', path = '/etc' %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_7/root/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_7/root/.calculate_directory index 8cd0bf5..daf19be 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_7/root/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_7/root/.calculate_directory @@ -1,2 +1,2 @@ -{% calculate append = 'skip', action = 'install', package = 'test-category/new-package' %} -{% calculate path = '/etc' %} +{% calculate append = 'skip', action = 'install', +package = 'test-category/new-package', path = '/etc' %}