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
|
2015-03-04 05:48:26 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango21Warning
|
2013-02-27 11:26:15 +08:00
|
|
|
from django.utils.encoding import force_bytes
|
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-03-04 05:48:26 +08:00
|
|
|
self.find_template_cache = {} # RemovedInDjango21Warning
|
|
|
|
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):
|
|
|
|
key = self.cache_key(template_name, template_dirs, skip)
|
|
|
|
cached = self.get_template_cache.get(key)
|
|
|
|
if cached:
|
|
|
|
if isinstance(cached, TemplateDoesNotExist):
|
|
|
|
raise cached
|
|
|
|
return cached
|
|
|
|
|
|
|
|
try:
|
|
|
|
template = super(Loader, self).get_template(
|
|
|
|
template_name, template_dirs, skip,
|
|
|
|
)
|
|
|
|
except TemplateDoesNotExist as e:
|
|
|
|
self.get_template_cache[key] = e
|
|
|
|
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]
|
|
|
|
# RemovedInDjango21Warning: Add template_dirs for compatibility
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
return ("%s-%s-%s" % (template_name, skip_prefix, dirs_prefix)).strip('-')
|
|
|
|
|
|
|
|
def generate_hash(self, values):
|
|
|
|
return hashlib.sha1(force_bytes('|'.join(values))).hexdigest()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supports_recursion(self):
|
|
|
|
"""
|
|
|
|
RemovedInDjango21Warning: This is an internal property used by the
|
|
|
|
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-03-04 05:48:26 +08:00
|
|
|
RemovedInDjango21Warning: An internal method to lookup the template
|
|
|
|
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() '
|
|
|
|
'instead.', RemovedInDjango21Warning,
|
|
|
|
)
|
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:
|
|
|
|
raise TemplateDoesNotExist
|
|
|
|
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-03-04 05:48:26 +08:00
|
|
|
self.find_template_cache.clear() # RemovedInDjango21Warning
|
|
|
|
self.get_template_cache.clear()
|