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-01-28 20:35:27 +08:00
|
|
|
|
2016-12-31 09:09:26 +08:00
|
|
|
from django.template import TemplateDoesNotExist
|
2016-03-09 11:35:39 +08:00
|
|
|
from django.template.backends.django import copy_exception
|
2017-02-07 16:17:38 +08:00
|
|
|
from django.utils.encoding import force_bytes
|
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-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)
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__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)
|
|
|
|
|
2016-12-31 09:09:26 +08:00
|
|
|
def get_template(self, template_name, 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.
|
|
|
|
"""
|
2016-12-31 09:09:26 +08:00
|
|
|
key = self.cache_key(template_name, skip)
|
2015-03-04 05:48:26 +08:00
|
|
|
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:
|
2017-01-21 21:13:44 +08:00
|
|
|
template = super().get_template(template_name, skip)
|
2015-03-04 05:48:26 +08:00
|
|
|
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
|
|
|
|
|
2016-12-31 09:09:26 +08:00
|
|
|
def get_template_sources(self, template_name):
|
2015-03-04 05:48:26 +08:00
|
|
|
for loader in self.loaders:
|
2017-02-24 09:06:01 +08:00
|
|
|
yield from loader.get_template_sources(template_name)
|
2015-03-04 05:48:26 +08:00
|
|
|
|
2016-12-31 09:09:26 +08:00
|
|
|
def cache_key(self, template_name, skip=None):
|
2015-03-04 05:48:26 +08:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
|
2017-02-07 16:17:38 +08:00
|
|
|
return '-'.join(filter(bool, [str(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()
|
|
|
|
|
2009-12-14 20:08:23 +08:00
|
|
|
def reset(self):
|
|
|
|
"Empty the template cache."
|
|
|
|
self.template_cache.clear()
|
2015-03-04 05:48:26 +08:00
|
|
|
self.get_template_cache.clear()
|