2009-12-14 20:08:23 +08:00
|
|
|
"""
|
|
|
|
Wrapper class that takes a list of template loaders as an argument and attempts
|
|
|
|
to load templates from them in order, caching the result.
|
|
|
|
"""
|
|
|
|
|
2011-03-28 10:11:19 +08:00
|
|
|
import hashlib
|
2015-03-04 05:48:26 +08:00
|
|
|
import warnings
|
2015-01-28 20:35:27 +08:00
|
|
|
|
2015-04-25 03:35:30 +08:00
|
|
|
from django.template import Origin, Template, TemplateDoesNotExist
|
2016-03-09 11:35:39 +08:00
|
|
|
from django.template.backends.django import copy_exception
|
2015-06-23 01:54:35 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2016-05-10 21:57:03 +08:00
|
|
|
from django.utils.encoding import force_bytes, force_text
|
2015-06-11 05:24:04 +08:00
|
|
|
from django.utils.inspect import func_supports_parameter
|
2009-12-14 20:08:23 +08:00
|
|
|
|
2014-11-16 01:35:02 +08:00
|
|
|
from .base import Loader as BaseLoader
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2009-12-14 20:08:23 +08:00
|
|
|
class Loader(BaseLoader):
|
|
|
|
|
2014-11-20 06:23:58 +08:00
|
|
|
def __init__(self, engine, loaders):
|
2009-12-14 20:08:23 +08:00
|
|
|
self.template_cache = {}
|
2015-06-23 01:54:35 +08:00
|
|
|
self.find_template_cache = {} # RemovedInDjango20Warning
|
2015-03-04 05:48:26 +08:00
|
|
|
self.get_template_cache = {}
|
2014-11-20 06:23:58 +08:00
|
|
|
self.loaders = engine.get_template_loaders(loaders)
|
|
|
|
super(Loader, self).__init__(engine)
|
2009-12-14 20:08:23 +08:00
|
|
|
|
2015-03-04 05:48:26 +08:00
|
|
|
def get_contents(self, origin):
|
|
|
|
return origin.loader.get_contents(origin)
|
|
|
|
|
|
|
|
def get_template(self, template_name, template_dirs=None, skip=None):
|
2016-03-09 11:35:39 +08:00
|
|
|
"""
|
|
|
|
Perform the caching that gives this loader its name. Often many of the
|
|
|
|
templates attempted will be missing, so memory use is of concern here.
|
|
|
|
To keep it in check, caching behavior is a little complicated when a
|
|
|
|
template is not found. See ticket #26306 for more details.
|
|
|
|
|
|
|
|
With template debugging disabled, cache the TemplateDoesNotExist class
|
|
|
|
for every missing template and raise a new instance of it after
|
|
|
|
fetching it from the cache.
|
|
|
|
|
|
|
|
With template debugging enabled, a unique TemplateDoesNotExist object
|
|
|
|
is cached for each missing template to preserve debug data. When
|
|
|
|
raising an exception, Python sets __traceback__, __context__, and
|
|
|
|
__cause__ attributes on it. Those attributes can contain references to
|
|
|
|
all sorts of objects up the call chain and caching them creates a
|
|
|
|
memory leak. Thus, unraised copies of the exceptions are cached and
|
|
|
|
copies of those copies are raised after they're fetched from the cache.
|
|
|
|
"""
|
2015-03-04 05:48:26 +08:00
|
|
|
key = self.cache_key(template_name, template_dirs, skip)
|
|
|
|
cached = self.get_template_cache.get(key)
|
|
|
|
if cached:
|
2016-03-09 11:35:39 +08:00
|
|
|
if isinstance(cached, type) and issubclass(cached, TemplateDoesNotExist):
|
|
|
|
raise cached(template_name)
|
|
|
|
elif isinstance(cached, TemplateDoesNotExist):
|
|
|
|
raise copy_exception(cached)
|
2015-03-04 05:48:26 +08:00
|
|
|
return cached
|
|
|
|
|
|
|
|
try:
|
|
|
|
template = super(Loader, self).get_template(
|
|
|
|
template_name, template_dirs, skip,
|
|
|
|
)
|
|
|
|
except TemplateDoesNotExist as e:
|
2016-03-09 11:35:39 +08:00
|
|
|
self.get_template_cache[key] = copy_exception(e) if self.engine.debug else TemplateDoesNotExist
|
2015-03-04 05:48:26 +08:00
|
|
|
raise
|
2013-07-26 03:27:23 +08:00
|
|
|
else:
|
2015-03-04 05:48:26 +08:00
|
|
|
self.get_template_cache[key] = template
|
|
|
|
|
|
|
|
return template
|
|
|
|
|
|
|
|
def get_template_sources(self, template_name, template_dirs=None):
|
|
|
|
for loader in self.loaders:
|
|
|
|
args = [template_name]
|
2015-06-23 01:54:35 +08:00
|
|
|
# RemovedInDjango20Warning: Add template_dirs for compatibility
|
2015-03-04 05:48:26 +08:00
|
|
|
# with old loaders
|
2015-06-11 05:24:04 +08:00
|
|
|
if func_supports_parameter(loader.get_template_sources, 'template_dirs'):
|
2015-03-04 05:48:26 +08:00
|
|
|
args.append(template_dirs)
|
|
|
|
for origin in loader.get_template_sources(*args):
|
|
|
|
yield origin
|
|
|
|
|
|
|
|
def cache_key(self, template_name, template_dirs, skip=None):
|
|
|
|
"""
|
|
|
|
Generate a cache key for the template name, dirs, and skip.
|
|
|
|
|
|
|
|
If skip is provided, only origins that match template_name are included
|
|
|
|
in the cache key. This ensures each template is only parsed and cached
|
|
|
|
once if contained in different extend chains like:
|
|
|
|
|
|
|
|
x -> a -> a
|
|
|
|
y -> a -> a
|
|
|
|
z -> a -> a
|
|
|
|
"""
|
|
|
|
dirs_prefix = ''
|
|
|
|
skip_prefix = ''
|
|
|
|
|
|
|
|
if skip:
|
|
|
|
matching = [origin.name for origin in skip if origin.template_name == template_name]
|
|
|
|
if matching:
|
|
|
|
skip_prefix = self.generate_hash(matching)
|
|
|
|
|
|
|
|
if template_dirs:
|
|
|
|
dirs_prefix = self.generate_hash(template_dirs)
|
|
|
|
|
2016-05-10 21:57:03 +08:00
|
|
|
return '-'.join(filter(bool, [force_text(template_name), skip_prefix, dirs_prefix]))
|
2015-03-04 05:48:26 +08:00
|
|
|
|
|
|
|
def generate_hash(self, values):
|
|
|
|
return hashlib.sha1(force_bytes('|'.join(values))).hexdigest()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supports_recursion(self):
|
|
|
|
"""
|
2015-06-23 01:54:35 +08:00
|
|
|
RemovedInDjango20Warning: This is an internal property used by the
|
2015-03-04 05:48:26 +08:00
|
|
|
ExtendsNode during the deprecation of non-recursive loaders.
|
|
|
|
"""
|
|
|
|
return all(hasattr(loader, 'get_contents') for loader in self.loaders)
|
2013-07-26 03:27:23 +08:00
|
|
|
|
|
|
|
def find_template(self, name, dirs=None):
|
2013-08-25 08:26:34 +08:00
|
|
|
"""
|
2015-06-23 01:54:35 +08:00
|
|
|
RemovedInDjango20Warning: An internal method to lookup the template
|
2015-03-04 05:48:26 +08:00
|
|
|
name in all the configured loaders.
|
2013-08-25 08:26:34 +08:00
|
|
|
"""
|
2013-07-26 03:27:23 +08:00
|
|
|
key = self.cache_key(name, dirs)
|
|
|
|
try:
|
|
|
|
result = self.find_template_cache[key]
|
|
|
|
except KeyError:
|
|
|
|
result = None
|
|
|
|
for loader in self.loaders:
|
|
|
|
try:
|
|
|
|
template, display_name = loader(name, dirs)
|
|
|
|
except TemplateDoesNotExist:
|
|
|
|
pass
|
|
|
|
else:
|
2015-03-04 05:48:26 +08:00
|
|
|
origin = Origin(
|
|
|
|
name=display_name,
|
|
|
|
template_name=name,
|
|
|
|
loader=loader,
|
|
|
|
)
|
2014-11-23 04:30:05 +08:00
|
|
|
result = template, origin
|
2013-11-20 00:49:16 +08:00
|
|
|
break
|
2013-07-26 03:27:23 +08:00
|
|
|
self.find_template_cache[key] = result
|
|
|
|
if result:
|
|
|
|
return result
|
|
|
|
else:
|
2013-08-25 08:26:34 +08:00
|
|
|
self.template_cache[key] = TemplateDoesNotExist
|
2013-07-26 03:27:23 +08:00
|
|
|
raise TemplateDoesNotExist(name)
|
2010-05-21 16:54:15 +08:00
|
|
|
|
2013-07-26 03:27:23 +08:00
|
|
|
def load_template(self, template_name, template_dirs=None):
|
2015-03-04 05:48:26 +08:00
|
|
|
warnings.warn(
|
|
|
|
'The load_template() method is deprecated. Use get_template() '
|
2015-06-23 01:54:35 +08:00
|
|
|
'instead.', RemovedInDjango20Warning,
|
2015-03-04 05:48:26 +08:00
|
|
|
)
|
2013-07-26 03:27:23 +08:00
|
|
|
key = self.cache_key(template_name, template_dirs)
|
2013-08-22 02:23:53 +08:00
|
|
|
template_tuple = self.template_cache.get(key)
|
2013-08-25 08:26:34 +08:00
|
|
|
# A cached previous failure:
|
2013-08-22 02:23:53 +08:00
|
|
|
if template_tuple is TemplateDoesNotExist:
|
2016-02-26 14:49:02 +08:00
|
|
|
raise TemplateDoesNotExist(template_name)
|
2013-08-22 02:23:53 +08:00
|
|
|
elif template_tuple is None:
|
2009-12-14 20:08:23 +08:00
|
|
|
template, origin = self.find_template(template_name, template_dirs)
|
|
|
|
if not hasattr(template, 'render'):
|
2010-03-16 22:34:57 +08:00
|
|
|
try:
|
2014-11-29 05:29:45 +08:00
|
|
|
template = Template(template, origin, template_name, self.engine)
|
2010-03-16 22:34:57 +08:00
|
|
|
except TemplateDoesNotExist:
|
2010-05-21 16:54:15 +08:00
|
|
|
# If compiling the template we found raises TemplateDoesNotExist,
|
|
|
|
# back off to returning the source and display name for the template
|
|
|
|
# we were asked to load. This allows for correct identification (later)
|
2010-03-16 22:34:57 +08:00
|
|
|
# of the actual template that does not exist.
|
2013-08-22 02:23:53 +08:00
|
|
|
self.template_cache[key] = (template, origin)
|
|
|
|
self.template_cache[key] = (template, None)
|
|
|
|
return self.template_cache[key]
|
2009-12-14 20:08:23 +08:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
"Empty the template cache."
|
|
|
|
self.template_cache.clear()
|
2015-06-23 01:54:35 +08:00
|
|
|
self.find_template_cache.clear() # RemovedInDjango20Warning
|
2015-03-04 05:48:26 +08:00
|
|
|
self.get_template_cache.clear()
|