本文整理汇总了Python中jinja2.FileSystemLoader.list_templates方法的典型用法代码示例。如果您正苦于以下问题:Python FileSystemLoader.list_templates方法的具体用法?Python FileSystemLoader.list_templates怎么用?Python FileSystemLoader.list_templates使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jinja2.FileSystemLoader
的用法示例。
在下文中一共展示了FileSystemLoader.list_templates方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_manifests
# 需要导入模块: from jinja2 import FileSystemLoader [as 别名]
# 或者: from jinja2.FileSystemLoader import list_templates [as 别名]
def generate_manifests(path, kwargs, pkg_name):
click.echo('Generate manifest')
loader = FileSystemLoader(path)
env = Environment(loader=loader)
out = os.path.join(ROOT_PATH, 'kubernetes')
for name in loader.list_templates():
template = env.get_template(name)
fname = os.path.join(out, name.replace('.j2', ''))
with open(fname, 'w') as f:
f.write(template.render(**kwargs))
with tarfile.open(pkg_name, "w:gz") as tar:
tar.add(out, arcname='kube-admin')
示例2: render_pages
# 需要导入模块: from jinja2 import FileSystemLoader [as 别名]
# 或者: from jinja2.FileSystemLoader import list_templates [as 别名]
def render_pages():
loader = FileSystemLoader('source')
env = Environment(loader=loader)
for name in loader.list_templates():
if name.startswith('_'):
continue
template = env.get_template(name)
print "writing {}".format(name)
with open(name, 'w') as handle:
output = template.render()
handle.write(output)
示例3: TemplateService
# 需要导入模块: from jinja2 import FileSystemLoader [as 别名]
# 或者: from jinja2.FileSystemLoader import list_templates [as 别名]
class TemplateService(EngineService):
endpoint_methods = None
published_members = ['render', 'get_base_templates', 'render_from_string', 'get_source']
name = 'template'
# Project's base path
__base_path = os.path.dirname(os.path.realpath(__file__))
# User's application base path
__app_base_path = None
__app_base_templates_dir = None
# Our internal jinja2 template env
__template_env = None
__fs_loader = None
def __init__(self, engine):
super(TemplateService, self).__init__(engine)
self.__app_base_path = self.engine.get('csettings', 'all')()['templates_root']
self.__app_base_templates_dir = self.engine.get('csettings', 'all')()['base_templates_dir']
self.__fs_loader = FileSystemLoader(
# Search path gets saved as a list in jinja2 internally, so we could
# add to it if necessary.
searchpath=[self.__base_path + '/templates', self.__app_base_path],
encoding='utf-8',
)
self.__template_env = Environment(loader=self.__fs_loader, trim_blocks=True)
def render(self, template_name, context):
"""
Context must be a dictionary right now, but could also be **kwargs
"""
# Add the global corus settings from engine to the context
context['csettings'] = self.engine.get('csettings', 'all')()
return self.__template_env.get_template(template_name).render(context)
def render_from_string(self, s, context):
# TODO we should probably make a new loader for getting stuff out of NDB
return self.__template_env.from_string(s).render(context)
def get_base_templates(self):
# The call to FS loader list_templates is a sorted set, so just append, return
bts = []
for t in self.__fs_loader.list_templates():
if t.startswith(self.__app_base_templates_dir):
bts.append(t)
return bts
def get_source(self, template_name):
source, filename, uptodate = self.__fs_loader.get_source(self.__template_env, template_name)
return source
示例4: _clean_nereid_template
# 需要导入模块: from jinja2 import FileSystemLoader [as 别名]
# 或者: from jinja2.FileSystemLoader import list_templates [as 别名]
def _clean_nereid_template(translation):
"""
Clean the template translations if the module is not installed, or if
the template is not there.
"""
TranslationSet = Pool().get('ir.translation.set', type='wizard')
installed_modules = TranslationSet._get_installed_module_directories()
# Clean if the module is not installed anymore
for module, directory in installed_modules:
if translation.module == module:
break
else:
return True
# Clean if the template directory does not exist
template_dir = os.path.join(directory, 'templates')
if not os.path.isdir(template_dir):
return True
# Clean if the template is not found
loader = FileSystemLoader(template_dir)
if translation.name not in loader.list_templates():
return True
# Clean if the translation has changed (avoid duplicates)
# (translation has no equivalent in template)
found = False
for template, lineno, function, message, comments in \
TranslationSet._get_nereid_template_messages_from_file(
TranslationSet, template_dir, translation.name):
if (template, lineno, message, comments and
'\n'.join(comments) or None) == \
(translation.name, translation.res_id, translation.src,
translation.comments):
found = True
break
if not found:
return True
示例5: _get_nereid_template_messages
# 需要导入模块: from jinja2 import FileSystemLoader [as 别名]
# 或者: from jinja2.FileSystemLoader import list_templates [as 别名]
def _get_nereid_template_messages(cls):
"""
Extract localizable strings from the templates of installed modules.
For every string found this function yields a
`(module, template, lineno, function, message)` tuple, where:
* module is the name of the module in which the template is found
* template is the name of the template in which message was found
* lineno is the number of the line on which the string was found,
* function is the name of the gettext function used (if the string
was extracted from embedded Python code), and
* message is the string itself (a unicode object, or a tuple of
unicode objects for functions with multiple string arguments).
* comments List of Translation comments if any. Comments in the code
should have a prefix `trans:`. Example::
{{ _(Welcome) }} {# trans: In the top banner #}
"""
extract_options = cls._get_nereid_template_extract_options()
for module, directory in cls._get_installed_module_directories():
template_dir = os.path.join(directory, 'templates')
if not os.path.isdir(template_dir):
# The template directory does not exist. Just continue
continue
# now that there is a template directory, load the templates
# using a simple filesystem loader and load all the
# translations from it.
loader = FileSystemLoader(template_dir)
for template in loader.list_templates():
file_obj = open(loader.get_source({}, template)[1])
for message_tuple in babel_extract(
file_obj, GETTEXT_FUNCTIONS,
['trans:'], extract_options):
yield (module, template) + message_tuple
示例6: _clean_nereid_template
# 需要导入模块: from jinja2 import FileSystemLoader [as 别名]
# 或者: from jinja2.FileSystemLoader import list_templates [as 别名]
def _clean_nereid_template(translation):
"""
Clean the template translations if the module is not installed, or if
the template is not there.
"""
TranslationSet = Pool().get('ir.translation.set', type='wizard')
installed_modules = TranslationSet._get_installed_module_directories()
# Clean if the module is not installed anymore
for module, directory in installed_modules:
if translation.module == module:
break
else:
return True
# Clean if the template directory does not exist
template_dir = os.path.join(directory, 'templates')
if not os.path.isdir(template_dir):
return True
# Clean if the template is not found
loader = FileSystemLoader(template_dir)
if translation.name not in loader.list_templates():
return True