本文整理汇总了Python中jinja2.loaders.FileSystemLoader类的典型用法代码示例。如果您正苦于以下问题:Python FileSystemLoader类的具体用法?Python FileSystemLoader怎么用?Python FileSystemLoader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FileSystemLoader类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, searchpath, files, prefix=None, **kwargs):
FileSystemLoader.__init__(self, searchpath, **kwargs)
self.files = files
if prefix is not None and not prefix.endswith("/"):
prefix += "/"
self.prefix = prefix
示例2: get_source
def get_source(self, environment, template):
# keep legacy asset: prefix checking that bypasses
# source path checking altogether
if template.startswith('asset:'):
template = template[6:]
# split the template into the chain of relative-imports
rel_chain = template.split(PARENT_RELATIVE_DELIM)
template, rel_chain = rel_chain[0], rel_chain[1:]
# load the template directly if it's an absolute path or asset spec
if os.path.isabs(template) or ':' in template:
src = self._get_absolute_source(template)
if src is not None:
return src
else:
# fallback to the search path just incase
return FileSystemLoader.get_source(self, environment, template)
# try to import the template as an asset spec or absolute path
# relative to its parents
rel_searchpath = self._relative_searchpath(rel_chain)
for parent in rel_searchpath:
if os.path.isabs(parent):
uri = os.path.join(parent, template)
# avoid recursive includes
if uri not in rel_chain:
src = self._get_absolute_source(uri)
if src is not None:
return src
# avoid doing "':' in" and then redundant "split"
parts = parent.split(':', 1)
if len(parts) > 1:
# parent is an asset spec
ppkg, ppath = parts
ppath = posixpath.join(ppath, template)
uri = '{0}:{1}'.format(ppkg, ppath)
# avoid recursive includes
if uri not in rel_chain:
src = self._get_absolute_source(uri)
if src is not None:
return src
# try to load the template from the default search path
for parent in rel_searchpath:
try:
uri = os.path.join(parent, template)
# avoid recursive includes
if uri not in rel_chain:
return FileSystemLoader.get_source(self, environment, uri)
except TemplateNotFound:
pass
# we're here because of an exception during the last step so extend
# the message and raise an appropriate error
# there should always be an exception because the rel_searchpath is
# guaranteed to contain at least one element ('')
searchpath = [p for p in rel_searchpath if p] + self.searchpath
message = '{0}; searchpath={1}'.format(template, searchpath)
raise TemplateNotFound(name=template, message=message)
示例3: get_source
def get_source(self, environment, template):
# Check if dottedname
if not template.endswith(self.template_extension):
# Get the actual filename from dotted finder
finder = self.dotted_finder
template = finder.get_dotted_filename(template_name=template,
template_extension=self.template_extension)
else:
return FileSystemLoader.get_source(self, environment, template)
# Check if the template exists
if not exists(template):
raise TemplateNotFound(template)
# Get modification time
mtime = getmtime(template)
# Read the source
fd = open(template, 'rb')
try:
source = fd.read().decode('utf-8')
finally:
fd.close()
return source, template, lambda: mtime == getmtime(template)
示例4: list_templates
def list_templates(self):
result = FileSystemLoader.list_templates(self)
if callable(self.path_filter):
result = sorted(filter(self._combined_filter, result))
return result
示例5: get_source
def get_source(self, environment, template):
if callable(self.path_filter):
pieces = split_template_path(template)
if not self._combined_filter(os.path.join(*pieces)):
raise TemplateNotFound(template)
return FileSystemLoader.get_source(self, environment, template)
示例6: get_source
def get_source(self, environment, template):
if not template.startswith(self.prefix):
raise TemplateNotFound(template)
template = template[len(self.prefix):]
if not template in self.files:
raise TemplateNotFound(template)
return FileSystemLoader.get_source(self, environment, template)
示例7: CustomizationLoader
class CustomizationLoader(BaseLoader):
def __init__(self, fallback_loader, customization_dir, customization_debug=False):
from indico.core.logger import Logger
self.logger = Logger.get('customization')
self.debug = customization_debug
self.fallback_loader = fallback_loader
self.fs_loader = FileSystemLoader(customization_dir, followlinks=True)
def _get_fallback(self, environment, template, path, customization_ignored=False):
rv = self.fallback_loader.get_source(environment, template)
if not customization_ignored and self.debug:
try:
orig_path = rv[1]
except TemplateNotFound:
orig_path = None
self.logger.debug('Customizable: %s (original: %s, reference: ~%s)', path, orig_path, template)
return rv
def get_source(self, environment, template):
path = posixpath.join(*split_template_path(template))
if template[0] == '~':
return self._get_fallback(environment, template[1:], path[1:], customization_ignored=True)
try:
plugin, path = path.split(':', 1)
except ValueError:
plugin = None
prefix = posixpath.join('plugins', plugin) if plugin else 'core'
path = posixpath.join(prefix, path)
try:
rv = self.fs_loader.get_source(environment, path)
if self.debug:
self.logger.debug('Customized: %s', path)
return rv
except TemplateNotFound:
return self._get_fallback(environment, template, path)
@internalcode
def load(self, environment, name, globals=None):
tpl = super(CustomizationLoader, self).load(environment, name, globals)
if ':' not in name:
return tpl
# This is almost exactly what PluginPrefixLoader.load() does, but we have
# to replicate it here since we need to handle `~` and use our custom
# `get_source` to get the overridden template
plugin_name, tpl_name = name.split(':', 1)
if plugin_name[0] == '~':
plugin_name = plugin_name[1:]
plugin = get_state(current_app).plugin_engine.get_plugin(plugin_name)
if plugin is None:
# that should never happen
raise RuntimeError('Plugin template {} has no plugin'.format(name))
tpl.plugin = plugin
return tpl
示例8: get_source
def get_source(self, environment, template):
# keep legacy asset: prefix checking that bypasses
# source path checking altogether
if template.startswith('asset:'):
newtemplate = template.split(':', 1)[1]
fi = self._get_asset_source_fileinfo(environment, newtemplate)
return fi.contents, fi.filename, fi.uptodate
fi = self._get_asset_source_fileinfo(environment, template)
if os.path.isfile(fi.filename):
return fi.contents, fi.filename, fi.uptodate
try:
return FileSystemLoader.get_source(self, environment, template)
except TemplateNotFound, ex:
message = ex.message
message += ('; asset=%s; searchpath=%r'
% (fi.filename, self.searchpath))
raise TemplateNotFound(name=ex.name, message=message)
示例9: get_source
def get_source(self, environment, template):
# keep legacy asset: prefix checking that bypasses
# source path checking altogether
if template.startswith('asset:'):
template = template[6:]
# load template directly from the filesystem
filename = abspath_from_asset_spec(template)
fi = FileInfo(filename, self.encoding)
if os.path.isfile(fi.filename):
return fi.contents, fi.filename, fi.uptodate
# fallback to search-path lookup
try:
return FileSystemLoader.get_source(self, environment, template)
except TemplateNotFound as ex:
message = ex.message
message += ('; asset=%s; searchpath=%r'
% (template, self.searchpath))
raise TemplateNotFound(name=ex.name, message=message)
示例10: __init__
def __init__(self, searchpath=(), encoding='utf-8', debug=False):
FileSystemLoader.__init__(self, searchpath, encoding)
self.debug = debug
示例11: __init__
def __init__(self, fallback_loader, customization_dir, customization_debug=False):
from indico.core.logger import Logger
self.logger = Logger.get('customization')
self.debug = customization_debug
self.fallback_loader = fallback_loader
self.fs_loader = FileSystemLoader(customization_dir, followlinks=True)
示例12: __init__
def __init__(self, searchpath, path_filter=None, **kwargs):
FileSystemLoader.__init__(self, searchpath, **kwargs)
self.path_filter = path_filter