|
|
|
@ -12,7 +12,7 @@ parameter_1 = {{ vars_1.value_1 }}
|
|
|
|
|
!parameter_2
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
backup_template_text = '''{% calculate append = 'join' -%}
|
|
|
|
|
backup_template_text = '''{% calculate append = 'join', format = 'samba' -%}
|
|
|
|
|
[section one]
|
|
|
|
|
parameter_1 = value
|
|
|
|
|
parameter_2 = value_2
|
|
|
|
@ -37,23 +37,87 @@ class TemplateActionError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TargetFile:
|
|
|
|
|
class TemplateTypeConflict(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TemplateWrapper:
|
|
|
|
|
type_checks = {DIR: os.path.isdir,
|
|
|
|
|
FILE: os.path.isfile,
|
|
|
|
|
LINK: os.path.islink}
|
|
|
|
|
FILE: os.path.isfile}
|
|
|
|
|
|
|
|
|
|
def __init__(self, target_file_path, template_type, package=None):
|
|
|
|
|
self._file_path = target_file_path
|
|
|
|
|
self._package = package
|
|
|
|
|
def __init__(self, target_file_path, parameters, template_type,
|
|
|
|
|
template_text=''):
|
|
|
|
|
self._target_path = target_file_path
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(target_file_path):
|
|
|
|
|
pass
|
|
|
|
|
self.parameters = parameters
|
|
|
|
|
|
|
|
|
|
self.template_type = template_type
|
|
|
|
|
|
|
|
|
|
self.template_text = template_text
|
|
|
|
|
|
|
|
|
|
self.remove_original = False
|
|
|
|
|
|
|
|
|
|
# Если по этому пути что-то есть -- проверяем конфликты.
|
|
|
|
|
if os.path.exists(target_file_path):
|
|
|
|
|
for file_type, checker in self.type_checks.items():
|
|
|
|
|
if checker(target_path):
|
|
|
|
|
self.target_type = file_type
|
|
|
|
|
break
|
|
|
|
|
self.target_is_link = os.path.islink(target_path)
|
|
|
|
|
else:
|
|
|
|
|
self.target_type = None
|
|
|
|
|
|
|
|
|
|
self.check_conflicts(self)
|
|
|
|
|
|
|
|
|
|
def check_conflicts(self):
|
|
|
|
|
'''Проверка конфликтов типов.'''
|
|
|
|
|
if self.parameters.append == 'link':
|
|
|
|
|
if self.parameters.force:
|
|
|
|
|
self.remove_original = True
|
|
|
|
|
return
|
|
|
|
|
elif self.target_type == DIR:
|
|
|
|
|
raise TemplateTypeConflict(
|
|
|
|
|
"The target is a directory while the"
|
|
|
|
|
" template has append = 'link'.")
|
|
|
|
|
else:
|
|
|
|
|
raise TemplateTypeConflict(
|
|
|
|
|
"The target is a file while the"
|
|
|
|
|
" template has append = 'link'.")
|
|
|
|
|
|
|
|
|
|
if self.template_type == DIR:
|
|
|
|
|
if self.parameters.force:
|
|
|
|
|
self.remove_original = True
|
|
|
|
|
return
|
|
|
|
|
elif self.target_type == FILE:
|
|
|
|
|
raise TemplateTypeConflict("The target is a file while the"
|
|
|
|
|
" template is a directory.")
|
|
|
|
|
elif self.target_is_link and self.target_type == DIR:
|
|
|
|
|
self._target_path = os.readlink(self._target_path)
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if self.template_type == FILE:
|
|
|
|
|
if self.parameters.force:
|
|
|
|
|
if self.target_type == DIR:
|
|
|
|
|
self.remove_original = True
|
|
|
|
|
return
|
|
|
|
|
elif self.target_is_link and self.target_type == FILE:
|
|
|
|
|
self._target_path = os.readlink(self._target_path)
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
return
|
|
|
|
|
elif self.target_type == FILE:
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
raise TemplateTypeConflict("The target file is a directory"
|
|
|
|
|
" while the template is a file.")
|
|
|
|
|
|
|
|
|
|
def check_conflicts(self, parameters):
|
|
|
|
|
def append_template():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TemplateActionDraft:
|
|
|
|
|
class TemplateExecutor:
|
|
|
|
|
def __init__(self, datavars_module=Variables(), chroot_path='/'):
|
|
|
|
|
self.datavars_module = datavars_module
|
|
|
|
|
|
|
|
|
@ -73,21 +137,20 @@ class TemplateActionDraft:
|
|
|
|
|
|
|
|
|
|
self.formats_classes = ParametersProcessor.available_formats
|
|
|
|
|
|
|
|
|
|
def use_directory_template(self, target_path, parameters):
|
|
|
|
|
def use_template(self, target_path, parameters, template_type,
|
|
|
|
|
template_text=''):
|
|
|
|
|
print('Template parameters:')
|
|
|
|
|
parameters.print_parameters_for_debug()
|
|
|
|
|
|
|
|
|
|
self.template_parameters = parameters
|
|
|
|
|
self.directory_appends[self.template_parameters.append](target_path)
|
|
|
|
|
|
|
|
|
|
def use_file_template(self, target_path, parameters, template_text=''):
|
|
|
|
|
print('Template parameters:')
|
|
|
|
|
parameters.print_parameters_for_debug()
|
|
|
|
|
|
|
|
|
|
self.template_parameters = parameters
|
|
|
|
|
self.template_text = template_text
|
|
|
|
|
try:
|
|
|
|
|
template_object = TemplateWrapper(target_path, parameters,
|
|
|
|
|
template_type,
|
|
|
|
|
template_text=template_text)
|
|
|
|
|
except TemplateTypeConflict as error:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
self.file_appends[self.template_parameters.append](target_path)
|
|
|
|
|
self.directory_appends[template_object.parameters.append](
|
|
|
|
|
template_object)
|
|
|
|
|
|
|
|
|
|
def _append_join_directory(self, target_path):
|
|
|
|
|
print("append = 'join'")
|
|
|
|
@ -222,6 +285,9 @@ class TemplateActionDraft:
|
|
|
|
|
print('RESULT:')
|
|
|
|
|
print(original_object.document_text)
|
|
|
|
|
|
|
|
|
|
with open(target_path, 'w') as target_file:
|
|
|
|
|
target_file.write(original_object.document_text)
|
|
|
|
|
|
|
|
|
|
def _append_clear_file(self, target_path):
|
|
|
|
|
print("append = 'clear'")
|
|
|
|
|
try:
|
|
|
|
@ -233,13 +299,22 @@ class TemplateActionDraft:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Применение основного шаблона:
|
|
|
|
|
template_engine.process_template_from_string(template_text, FILE)
|
|
|
|
|
template_parameters = template_engine.parameters
|
|
|
|
|
template_text = template_engine.template_text
|
|
|
|
|
|
|
|
|
|
template_action_obj = TemplateActionDraft(datavars_module=DATAVARS_MODULE,
|
|
|
|
|
chroot_path=CHROOT_PATH)
|
|
|
|
|
template_action_obj = TemplateExecutor(datavars_module=DATAVARS_MODULE,
|
|
|
|
|
chroot_path=CHROOT_PATH)
|
|
|
|
|
|
|
|
|
|
result = template_action_obj.use_file_template(target_path,
|
|
|
|
|
template_parameters,
|
|
|
|
|
template_text=template_text)
|
|
|
|
|
print('MAIN TEST TEMPLATE IS USED')
|
|
|
|
|
# Применение шаблона бэкапа:
|
|
|
|
|
template_engine.process_template_from_string(backup_template_text, FILE)
|
|
|
|
|
result = template_action_obj.use_file_template(
|
|
|
|
|
target_path,
|
|
|
|
|
template_engine.parameters,
|
|
|
|
|
template_text=template_engine.template_text)
|
|
|
|
|
print('BACKUP TEMPLATE IS USED')
|
|
|
|
|