def get_template_object(template_file=''):
"""Retrieve template.
Args:
template_file (str): Name of template file.
Returns:
jinja2.Template: Template ready to render.
Raises:
AssertionError: Configured path for templates does not exist.
:obj:`foremast.exceptions.ForemastTemplateNotFound`: Requested template
is not available.
"""
jinja_lst = []
if TEMPLATES_PATH:
external_templates = os.path.expanduser(TEMPLATES_PATH)
assert os.path.isdir(external_templates), 'External template path "{0}" not found'.format(external_templates)
jinja_lst.append(external_templates)
jinja_lst.append(LOCAL_TEMPLATES)
jinjaenv = jinja2.Environment(loader=jinja2.FileSystemLoader(jinja_lst))
try:
template = jinjaenv.get_template(template_file)
except jinja2.TemplateNotFound:
message = 'Unable to find template "{template_file}" in paths {paths}'.format(
template_file=template_file, paths=jinjaenv.loader.searchpath)
LOG.error(message)
raise ForemastTemplateNotFound(message)
return template
评论列表
文章目录