2015-03-04 05:48:26 +08:00
|
|
|
import warnings
|
|
|
|
|
2015-04-25 03:35:30 +08:00
|
|
|
from django.template import Origin, Template, TemplateDoesNotExist
|
2015-06-23 01:54:35 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2015-06-11 05:24:04 +08:00
|
|
|
from django.utils.inspect import func_supports_parameter
|
2014-11-16 01:35:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Loader(object):
|
|
|
|
|
2014-11-20 06:23:58 +08:00
|
|
|
def __init__(self, engine):
|
|
|
|
self.engine = engine
|
2014-11-16 01:35:02 +08:00
|
|
|
|
|
|
|
def __call__(self, template_name, template_dirs=None):
|
2015-06-23 01:54:35 +08:00
|
|
|
# RemovedInDjango20Warning: Allow loaders to be called like functions.
|
2014-11-16 01:35:02 +08:00
|
|
|
return self.load_template(template_name, template_dirs)
|
|
|
|
|
2015-03-04 05:48:26 +08:00
|
|
|
def get_template(self, template_name, template_dirs=None, skip=None):
|
|
|
|
"""
|
|
|
|
Calls self.get_template_sources() and returns a Template object for
|
|
|
|
the first template matching template_name. If skip is provided,
|
|
|
|
template origins in skip are ignored. This is used to avoid recursion
|
|
|
|
during template extending.
|
|
|
|
"""
|
|
|
|
tried = []
|
|
|
|
|
|
|
|
args = [template_name]
|
2015-06-23 01:54:35 +08:00
|
|
|
# RemovedInDjango20Warning: Add template_dirs for compatibility with
|
2015-03-04 05:48:26 +08:00
|
|
|
# old loaders
|
2015-06-11 05:24:04 +08:00
|
|
|
if func_supports_parameter(self.get_template_sources, 'template_dirs'):
|
2015-03-04 05:48:26 +08:00
|
|
|
args.append(template_dirs)
|
|
|
|
|
|
|
|
for origin in self.get_template_sources(*args):
|
|
|
|
if skip is not None and origin in skip:
|
|
|
|
tried.append((origin, 'Skipped'))
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
contents = self.get_contents(origin)
|
|
|
|
except TemplateDoesNotExist:
|
|
|
|
tried.append((origin, 'Source does not exist'))
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
return Template(
|
|
|
|
contents, origin, origin.template_name, self.engine,
|
|
|
|
)
|
|
|
|
|
|
|
|
raise TemplateDoesNotExist(template_name, tried=tried)
|
|
|
|
|
2014-11-16 01:35:02 +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
|
|
|
)
|
2014-11-16 01:35:02 +08:00
|
|
|
source, display_name = self.load_template_source(
|
2015-03-04 05:48:26 +08:00
|
|
|
template_name, template_dirs,
|
|
|
|
)
|
|
|
|
origin = Origin(
|
|
|
|
name=display_name,
|
|
|
|
template_name=template_name,
|
|
|
|
loader=self,
|
|
|
|
)
|
2014-11-16 01:35:02 +08:00
|
|
|
try:
|
2014-11-29 05:29:45 +08:00
|
|
|
template = Template(source, origin, template_name, self.engine)
|
2014-11-16 01:35:02 +08:00
|
|
|
except TemplateDoesNotExist:
|
|
|
|
# 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 of the actual template that does not exist.
|
|
|
|
return source, display_name
|
|
|
|
else:
|
|
|
|
return template, None
|
|
|
|
|
2015-03-04 05:48:26 +08:00
|
|
|
def get_template_sources(self, template_name):
|
2014-11-16 01:35:02 +08:00
|
|
|
"""
|
2015-03-04 05:48:26 +08:00
|
|
|
An iterator that yields possible matching template paths for a
|
2014-11-16 01:35:02 +08:00
|
|
|
template name.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError(
|
2015-03-04 05:48:26 +08:00
|
|
|
'subclasses of Loader must provide a get_template_sources() method'
|
|
|
|
)
|
|
|
|
|
|
|
|
def load_template_source(self, template_name, template_dirs=None):
|
|
|
|
"""
|
2015-06-23 01:54:35 +08:00
|
|
|
RemovedInDjango20Warning: Returns a tuple containing the source and
|
2015-03-04 05:48:26 +08:00
|
|
|
origin for the given template name.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError(
|
|
|
|
'subclasses of Loader must provide a load_template_source() method'
|
|
|
|
)
|
2014-11-16 01:35:02 +08:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
"""
|
|
|
|
Resets any state maintained by the loader instance (e.g. cached
|
|
|
|
templates or cached loader modules).
|
|
|
|
"""
|
|
|
|
pass
|
2015-03-04 05:48:26 +08:00
|
|
|
|
|
|
|
@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 hasattr(self, 'get_contents')
|