Added main.cl.current_template variable fixed #29

master
Иванов Денис 3 years ago
parent 583518d4a7
commit 3f5ff16e67

@ -1961,6 +1961,9 @@ class DirectoryProcessor:
else:
self._add_package_to_group(group_name, package_name)
# Создаем переменную для указания текущих шаблонов, если ее еще нет.
self._make_current_template_var()
# Инициализируем шаблонизатор.
self.template_engine = TemplateEngine(
datavars_module=self.datavars_module,
@ -2089,6 +2092,29 @@ class DirectoryProcessor:
continue
return True
def _make_current_template_var(self) -> None:
var_path = ['main', 'cl']
namespace = self.datavars_module
print("namespace:", namespace)
for field in var_path:
if field not in namespace:
if isinstance(self.datavars_module, (Datavars, NamespaceNode)):
namespace = NamespaceNode(field, parent=namespace)
else:
namespace[field] = Variables()
namespace = namespace[field]
else:
namespace = namespace[field]
if 'current_template' not in namespace:
if isinstance(self.datavars_module, (Datavars, NamespaceNode)):
variable = VariableNode('current_template', namespace,
variable_type=StringType,
source="")
else:
namespace['current_template'] = ""
def process_template_directories(self) -> None:
'''Метод для обхода шаблонов, содержащихся в каталогах из
main.cl_template.path.'''
@ -2429,7 +2455,8 @@ class DirectoryProcessor:
# Удаляем все параметры, которые не наследуются и используем
# полученный контейнер для сбора параметров файлов шаблонов.
template_parameters.remove_not_inheritable()
template_path = os.path.join(current_directory_path, template_name)
template_path = os.path.join(current_directory_path,
template_name)
# Применяем к файлу шаблона шаблонизатор.
template_text = self._parse_template(template_parameters,
@ -2625,6 +2652,10 @@ class DirectoryProcessor:
else:
template_path = join_paths(template_directory, template_name)
if isinstance(self.datavars_module, (Datavars, NamespaceNode)):
self.datavars_module.main.cl['current_template'].set(template_path)
else:
self.datavars_module.main.cl['current_template'] = template_path
try:
self.template_engine.process_template(template_name,
template_type,

@ -32,6 +32,9 @@ Variable("chroot_status", type=BooleanType,
Variable("system_boot_set", type=BooleanType,
source=Calculate(is_system_boot))
# Переменная, в которую будут помещаться пути к текущим шаблонам.
Variable("current_template", type=StringType, source="")
class CmdlineParams(object):
"""
Параметры опции загрузки ядра calculate=

@ -1436,6 +1436,29 @@ class TestDirectoryProcessor:
new_text = new_file.read()
assert new_text == expected_new_text
def test_current_template_variable(self):
datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH,
'templates_45')
template_path = join_paths(CHROOT_PATH,
"templates_45/install/dir_62/file_0")
expected_output = ('#' + '-' * 79 + '\n' +
'# Modified by Calculate Utilities 4.0\n' +
'# Processed template files:\n' +
f'# {template_path}\n' +
'#' + '-' * 79 +
f'\n\n\ntemplate = {template_path}')
directory_processor = DirectoryProcessor(
'install',
datavars_module=datavars,
package='test-category/test-package'
)
directory_processor.process_template_directories()
assert os.path.exists(join_paths(CHROOT_PATH,'/etc/dir_62/file_0'))
with open(join_paths(CHROOT_PATH,
'/etc/dir_62/file_0'), 'r') as output_file:
output_text = output_file.read()
assert output_text == expected_output
def test_view_tree(self):
list_path = join_paths(CHROOT_PATH, '/etc')
show_tree(list_path)

@ -0,0 +1 @@
{% calculate action = "install", append = "skip", package = "test-category/test-package" %}

@ -0,0 +1,2 @@
{% calculate append = 'join', format = 'raw' %}
template = {{ main.cl.current_template }}
Loading…
Cancel
Save