Add new attribute "include" to "file"

This attribute have 3 values:
- "no": it's a normal file
- "name": the name is included in another template so it is generated before other templates
- "content": the content is included in another template so it is generated before the other templates and is not installed on the server

fixes #2
This commit is contained in:
2021-02-21 10:34:01 +01:00
parent 710711b253
commit 0be7a72d28
101 changed files with 631 additions and 377 deletions

View File

@ -67,6 +67,7 @@
<!ATTLIST file filelist CDATA #IMPLIED>
<!ATTLIST file redefine (True|False) "False">
<!ATTLIST file engine (none|creole|jinja2) #IMPLIED>
<!ATTLIST file include (no|name|content) "no">
<!ELEMENT override EMPTY>
<!ATTLIST override source CDATA #IMPLIED>

View File

@ -267,6 +267,7 @@ class RougailBaseTemplate:
source=source,
true_destfilename=destfile,
destfilename=destfilename,
destdir=self.destinations_dir,
variable=var,
rougail_variables_dict=self.rougail_variables_dict,
eosfunc=self.eosfunc,
@ -285,16 +286,23 @@ class RougailBaseTemplate:
)
for template in listdir('.'):
self.prepare_template(template)
for service_obj in await self.config.option('services').list('all'):
service_name = await service_obj.option.name()
for fills in await service_obj.list('all'):
type_ = await fills.option.name()
for fill_obj in await fills.list('all'):
fill = await fill_obj.value.dict()
if fill['activate']:
self.instance_file(fill, type_, service_name)
else:
self.log.debug(_("Instantiation of file '{filename}' disabled"))
for include in (True, False):
for service_obj in await self.config.option('services').list('all'):
service_name = await service_obj.option.name()
for fills in await service_obj.list('all'):
type_ = await fills.option.name()
for fill_obj in await fills.list('all'):
fill = await fill_obj.value.dict()
if 'include' in fill:
if (fill['include'] == 'no' and include is True) or \
(fill['include'] != 'no' and include is False):
continue
elif include is True:
continue
if fill['activate']:
self.instance_file(fill, type_, service_name)
else:
self.log.debug(_("Instantiation of file '{filename}' disabled"))
self.post_instance()
chdir(ori_dir)

View File

@ -96,6 +96,7 @@ def process(filename: str,
source: str,
true_destfilename: str,
destfilename: str,
destdir: str,
variable: Any,
rougail_variables_dict: Dict,
eosfunc: Dict,
@ -105,7 +106,8 @@ def process(filename: str,
# full path of the destination file
try:
extra_context = {'normalize_family': normalize_family,
'rougail_filename': true_destfilename
'rougail_filename': true_destfilename,
'rougail_destination_dir': destdir,
}
if variable is not None:
extra_context['rougail_variable'] = variable

View File

@ -37,6 +37,7 @@ def process(filename: str,
source: str,
true_destfilename: str,
destfilename: str,
destdir: str,
variable: Any,
rougail_variables_dict: Dict,
eosfunc: Dict,
@ -55,9 +56,13 @@ def process(filename: str,
try:
# extra_context = {'normalize_family': normalize_family,
# eosfunc
env = Environment(loader=FileSystemLoader(dir_name))
env = Environment(loader=FileSystemLoader([dir_name, destdir]))
template = env.get_template(template_name)
data = template.render(**rougail_variables_dict, rougail_filename=true_destfilename, **var)
data = template.render(**rougail_variables_dict,
rougail_filename=true_destfilename,
rougail_destination_dir=destdir,
**var,
)
except UndefinedError as err: # pragma: no cover
varname = err
msg = f"Error: unknown variable used in template {filename} to {destfilename}: {varname}"

View File

@ -47,7 +47,7 @@ z %%filename - - - - -
%for %%service in %%services
%if %%hasattr(%%service, 'files')
%for %%file in %%service.files
%if %%file.activate == True
%if %%file.activate == True and %%file.include != 'content'
%if %%isinstance(%%file.name, list)
%for %%filename in %%file.name
%%display(%%file, %%filename)%slurp
@ -125,6 +125,7 @@ class RougailSystemdTemplate(RougailBaseTemplate):
source=ROUGAIL_TMPL_TEMPLATE,
true_destfilename=destfile,
destfilename=destfilename,
destdir=self.destinations_dir,
variable=None,
rougail_variables_dict=self.rougail_variables_dict,
eosfunc=self.eosfunc,