Commit 2577ae6a authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Moved all template loaders under django.template.loaders.

Reformatted the code of base.Loader according to modern standards.

Turned the test template loader into a regular locmem.Loader -- but
didn't document it.

Added a normal deprecation path for BaseLoader which is a public API.

Added an accelerated deprecation path for TestTemplateLoader which is
a private API.
parent cffa5590
Loading
Loading
Loading
Loading
+17 −39
Original line number Diff line number Diff line
@@ -25,54 +25,18 @@
# Python eggs) sets is_usable to False if the "pkg_resources" module isn't
# installed, because pkg_resources is necessary to read eggs.

import warnings

from django.core.exceptions import ImproperlyConfigured
from django.template.base import Origin, Template, Context, TemplateDoesNotExist
from django.conf import settings
from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.module_loading import import_string
from django.utils import six

template_source_loaders = None


class BaseLoader(object):
    is_usable = False

    def __init__(self, *args, **kwargs):
        pass

    def __call__(self, template_name, template_dirs=None):
        return self.load_template(template_name, template_dirs)

    def load_template(self, template_name, template_dirs=None):
        source, display_name = self.load_template_source(template_name, template_dirs)
        origin = make_origin(display_name, self.load_template_source, template_name, template_dirs)
        try:
            template = get_template_from_string(source, origin, template_name)
            return template, None
        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 (later) of the actual template that does
            # not exist.
            return source, display_name

    def load_template_source(self, template_name, template_dirs=None):
        """
        Returns a tuple containing the source and origin for the given template
        name.

        """
        raise NotImplementedError('subclasses of BaseLoader must provide a load_template_source() method')

    def reset(self):
        """
        Resets any state maintained by the loader instance (e.g., cached
        templates or cached loader modules).

        """
        pass


class LoaderOrigin(Origin):
    def __init__(self, display_name, loader, name, dirs):
        super(LoaderOrigin, self).__init__(display_name)
@@ -199,3 +163,17 @@ def select_template(template_name_list, dirs=None):
            continue
    # If we get here, none of the templates could be loaded
    raise TemplateDoesNotExist(', '.join(not_found))


# This line must remain at the bottom to avoid import loops.
from .loaders import base


class BaseLoader(base.Loader):

    def __init__(self, *args, **kwargs):
        warnings.warn(
            "django.template.loader.BaseLoader was renamed to "
            "django.template.loaders.base.Loader.",
            RemovedInDjango20Warning, stacklevel=2)
        super(BaseLoader, self).__init__(*args, **kwargs)
+2 −1
Original line number Diff line number Diff line
@@ -11,10 +11,11 @@ from django.apps import apps
from django.conf import settings
from django.core.exceptions import SuspiciousFileOperation
from django.template.base import TemplateDoesNotExist
from django.template.loader import BaseLoader
from django.utils._os import safe_join
from django.utils import six

from .base import Loader as BaseLoader


def calculate_app_template_dirs():
    if six.PY2:
+47 −0
Original line number Diff line number Diff line
from django.template.base import TemplateDoesNotExist
from django.template.loader import get_template_from_string, make_origin


class Loader(object):
    is_usable = False

    def __init__(self, *args, **kwargs):
        # XXX dropping arguments silently may not be the best idea.
        pass

    def __call__(self, template_name, template_dirs=None):
        return self.load_template(template_name, template_dirs)

    def load_template(self, template_name, template_dirs=None):
        source, display_name = self.load_template_source(
            template_name, template_dirs)
        origin = make_origin(
            display_name, self.load_template_source,
            template_name, template_dirs)

        try:
            template = get_template_from_string(source, origin, template_name)
        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

    def load_template_source(self, template_name, template_dirs=None):
        """
        Returns a tuple containing the source and origin for the given
        template name.
        """
        raise NotImplementedError(
            "subclasses of Loader must provide "
            "a load_template_source() method")

    def reset(self):
        """
        Resets any state maintained by the loader instance (e.g. cached
        templates or cached loader modules).
        """
        pass
+3 −1
Original line number Diff line number Diff line
@@ -5,9 +5,11 @@ to load templates from them in order, caching the result.

import hashlib
from django.template.base import TemplateDoesNotExist
from django.template.loader import BaseLoader, get_template_from_string, find_template_loader, make_origin
from django.template.loader import get_template_from_string, find_template_loader, make_origin
from django.utils.encoding import force_bytes

from .base import Loader as BaseLoader


class Loader(BaseLoader):
    is_usable = True
+2 −1
Original line number Diff line number Diff line
@@ -9,9 +9,10 @@ except ImportError:
from django.apps import apps
from django.conf import settings
from django.template.base import TemplateDoesNotExist
from django.template.loader import BaseLoader
from django.utils import six

from .base import Loader as BaseLoader


class Loader(BaseLoader):
    is_usable = resource_string is not None
Loading