Moved engine-related exceptions to django.template.exceptions.

With the introduction of multiple template engines these exceptions are no
longer DTL-specific. It makes more sense for them to be moved out of
DTL-related modules.
This commit is contained in:
Preston Timmons 2015-04-24 14:35:30 -05:00
parent adff499e47
commit d17a035132
16 changed files with 80 additions and 60 deletions

View File

@ -54,9 +54,9 @@ __all__ = ('Engine', 'engines')
# Django Template Language # Django Template Language
# Public exceptions # Public exceptions
from .base import (TemplateDoesNotExist, TemplateSyntaxError, # NOQA from .base import VariableDoesNotExist # NOQA
VariableDoesNotExist)
from .context import ContextPopException # NOQA from .context import ContextPopException # NOQA
from .exceptions import TemplateDoesNotExist, TemplateSyntaxError # NOQA
# Template parts # Template parts
from .base import (Context, Node, NodeList, Origin, RequestContext, # NOQA from .base import (Context, Node, NodeList, Origin, RequestContext, # NOQA

View File

@ -82,6 +82,8 @@ from django.utils.text import (
from django.utils.timezone import template_localtime from django.utils.timezone import template_localtime
from django.utils.translation import pgettext_lazy, ugettext_lazy from django.utils.translation import pgettext_lazy, ugettext_lazy
from .exceptions import TemplateSyntaxError
TOKEN_TEXT = 0 TOKEN_TEXT = 0
TOKEN_VAR = 1 TOKEN_VAR = 1
TOKEN_BLOCK = 2 TOKEN_BLOCK = 2
@ -129,36 +131,6 @@ builtins = []
logger = logging.getLogger('django.template') logger = logging.getLogger('django.template')
class TemplateSyntaxError(Exception):
pass
class TemplateDoesNotExist(Exception):
"""
The exception used by backends when a template does not exist. Accepts the
following optional arguments:
backend
The template backend class used when raising this exception.
tried
A list of sources that were tried when finding the template. This
is formatted as a list of tuples containing (origin, status), where
origin is an Origin object and status is a string with the reason the
template wasn't found.
chain
A list of intermediate TemplateDoesNotExist exceptions. This is used to
encapsulate multiple exceptions when loading templates from multiple
engines.
"""
def __init__(self, msg, tried=None, backend=None, chain=None):
self.backend = backend
self.tried = tried or []
self.chain = chain or []
super(TemplateDoesNotExist, self).__init__(msg)
class TemplateEncodingError(Exception): class TemplateEncodingError(Exception):
pass pass

View File

@ -9,7 +9,6 @@ from functools import wraps
from pprint import pformat from pprint import pformat
from django.conf import settings from django.conf import settings
from django.template.base import Library, Variable, VariableDoesNotExist
from django.utils import formats, six from django.utils import formats, six
from django.utils.dateformat import format, time_format from django.utils.dateformat import format, time_format
from django.utils.deprecation import RemovedInDjango20Warning from django.utils.deprecation import RemovedInDjango20Warning
@ -26,6 +25,8 @@ from django.utils.text import (
from django.utils.timesince import timesince, timeuntil from django.utils.timesince import timesince, timeuntil
from django.utils.translation import ugettext, ungettext from django.utils.translation import ugettext, ungettext
from .base import Library, Variable, VariableDoesNotExist
register = Library() register = Library()

View File

@ -9,7 +9,14 @@ from datetime import datetime
from itertools import cycle as itertools_cycle, groupby from itertools import cycle as itertools_cycle, groupby
from django.conf import settings from django.conf import settings
from django.template.base import ( from django.utils import six, timezone
from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.encoding import force_text, smart_text
from django.utils.html import format_html
from django.utils.lorem_ipsum import paragraphs, words
from django.utils.safestring import mark_safe
from .base import (
BLOCK_TAG_END, BLOCK_TAG_START, COMMENT_TAG_END, COMMENT_TAG_START, BLOCK_TAG_END, BLOCK_TAG_START, COMMENT_TAG_END, COMMENT_TAG_START,
SINGLE_BRACE_END, SINGLE_BRACE_START, VARIABLE_ATTRIBUTE_SEPARATOR, SINGLE_BRACE_END, SINGLE_BRACE_START, VARIABLE_ATTRIBUTE_SEPARATOR,
VARIABLE_TAG_END, VARIABLE_TAG_START, Context, InvalidTemplateLibrary, VARIABLE_TAG_END, VARIABLE_TAG_START, Context, InvalidTemplateLibrary,
@ -17,14 +24,8 @@ from django.template.base import (
VariableDoesNotExist, get_library, kwarg_re, render_value_in_context, VariableDoesNotExist, get_library, kwarg_re, render_value_in_context,
token_kwargs, token_kwargs,
) )
from django.template.defaultfilters import date from .defaultfilters import date
from django.template.smartif import IfParser, Literal from .smartif import IfParser, Literal
from django.utils import six, timezone
from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.encoding import force_text, smart_text
from django.utils.html import format_html
from django.utils.lorem_ipsum import paragraphs, words
from django.utils.safestring import mark_safe
register = Library() register = Library()

View File

@ -6,8 +6,9 @@ from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.utils.module_loading import import_string from django.utils.module_loading import import_string
from .base import Context, Template, TemplateDoesNotExist from .base import Context, Template
from .context import _builtin_context_processors from .context import _builtin_context_processors
from .exceptions import TemplateDoesNotExist
_context_instance_undefined = object() _context_instance_undefined = object()
_dictionary_undefined = object() _dictionary_undefined = object()

View File

@ -0,0 +1,43 @@
"""
This module contains generic exceptions used by template backends. Although,
due to historical reasons, the Django template language also internally uses
these exceptions, other exceptions specific to the DTL should not be added
here.
"""
class TemplateDoesNotExist(Exception):
"""
The exception used when a template does not exist. Accepts the following
optional arguments:
backend
The template backend class used when raising this exception.
tried
A list of sources that were tried when finding the template. This
is formatted as a list of tuples containing (origin, status), where
origin is an Origin object or duck type and status is a string with the
reason the template wasn't found.
chain
A list of intermediate TemplateDoesNotExist exceptions. This is used to
encapsulate multiple exceptions when loading templates from multiple
engines.
"""
def __init__(self, msg, tried=None, backend=None, chain=None):
self.backend = backend
if tried is None:
tried = []
self.tried = tried
if chain is None:
chain = []
self.chain = chain
super(TemplateDoesNotExist, self).__init__(msg)
class TemplateSyntaxError(Exception):
"""
The exception used for syntax errors during parsing or rendering.
"""
pass

View File

@ -4,10 +4,10 @@ from django.utils.deprecation import RemovedInDjango20Warning
from . import engines from . import engines
from .backends.django import DjangoTemplates from .backends.django import DjangoTemplates
from .base import TemplateDoesNotExist
from .engine import ( from .engine import (
_context_instance_undefined, _dictionary_undefined, _dirs_undefined, _context_instance_undefined, _dictionary_undefined, _dirs_undefined,
) )
from .exceptions import TemplateDoesNotExist
from .loaders import base from .loaders import base

View File

@ -1,11 +1,12 @@
from collections import defaultdict from collections import defaultdict
from django.template.base import ( from django.utils import six
from django.utils.safestring import mark_safe
from .base import (
Library, Node, Template, TemplateSyntaxError, TextNode, Variable, Library, Node, Template, TemplateSyntaxError, TextNode, Variable,
token_kwargs, token_kwargs,
) )
from django.utils import six
from django.utils.safestring import mark_safe
register = Library() register = Library()

View File

@ -1,7 +1,7 @@
import warnings import warnings
from inspect import getargspec from inspect import getargspec
from django.template.base import Origin, Template, TemplateDoesNotExist from django.template import Origin, Template, TemplateDoesNotExist
from django.utils.deprecation import RemovedInDjango21Warning from django.utils.deprecation import RemovedInDjango21Warning

View File

@ -7,7 +7,7 @@ import hashlib
import warnings import warnings
from inspect import getargspec from inspect import getargspec
from django.template.base import Origin, Template, TemplateDoesNotExist from django.template import Origin, Template, TemplateDoesNotExist
from django.utils.deprecation import RemovedInDjango21Warning from django.utils.deprecation import RemovedInDjango21Warning
from django.utils.encoding import force_bytes from django.utils.encoding import force_bytes

View File

@ -4,7 +4,7 @@ from __future__ import unicode_literals
import warnings import warnings
from django.apps import apps from django.apps import apps
from django.template.base import Origin, TemplateDoesNotExist from django.template import Origin, TemplateDoesNotExist
from django.utils import six from django.utils import six
from django.utils.deprecation import RemovedInDjango21Warning from django.utils.deprecation import RemovedInDjango21Warning

View File

@ -7,7 +7,7 @@ import io
import warnings import warnings
from django.core.exceptions import SuspiciousFileOperation from django.core.exceptions import SuspiciousFileOperation
from django.template.base import Origin, TemplateDoesNotExist from django.template import Origin, TemplateDoesNotExist
from django.utils._os import safe_join from django.utils._os import safe_join
from django.utils.deprecation import RemovedInDjango21Warning from django.utils.deprecation import RemovedInDjango21Warning

View File

@ -4,7 +4,7 @@ Wrapper for loading templates from a plain Python dict.
import warnings import warnings
from django.template.base import Origin, TemplateDoesNotExist from django.template import Origin, TemplateDoesNotExist
from django.utils.deprecation import RemovedInDjango21Warning from django.utils.deprecation import RemovedInDjango21Warning
from .base import Loader as BaseLoader from .base import Loader as BaseLoader

View File

@ -1,12 +1,14 @@
import warnings import warnings
from django.http import HttpResponse from django.http import HttpResponse
from django.template import Context, RequestContext, Template, loader
from django.template.backends.django import Template as BackendTemplate
from django.template.context import _current_app_undefined
from django.utils import six from django.utils import six
from django.utils.deprecation import RemovedInDjango20Warning from django.utils.deprecation import RemovedInDjango20Warning
from .backends.django import Template as BackendTemplate
from .base import Template
from .context import Context, RequestContext, _current_app_undefined
from .loader import get_template, select_template
class ContentNotRenderedError(Exception): class ContentNotRenderedError(Exception):
pass pass
@ -75,9 +77,9 @@ class SimpleTemplateResponse(HttpResponse):
def resolve_template(self, template): def resolve_template(self, template):
"Accepts a template object, path-to-template or list of paths" "Accepts a template object, path-to-template or list of paths"
if isinstance(template, (list, tuple)): if isinstance(template, (list, tuple)):
return loader.select_template(template, using=self.using) return select_template(template, using=self.using)
elif isinstance(template, six.string_types): elif isinstance(template, six.string_types):
return loader.get_template(template, using=self.using) return get_template(template, using=self.using)
else: else:
return template return template

View File

@ -13,8 +13,7 @@ from unittest import skipUnless
from django import forms from django import forms
from django.conf import settings from django.conf import settings
from django.template import Context, Template from django.template import Context, Template, TemplateSyntaxError
from django.template.base import TemplateSyntaxError
from django.test import RequestFactory, TestCase, override_settings from django.test import RequestFactory, TestCase, override_settings
from django.utils import six, translation from django.utils import six, translation
from django.utils._os import upath from django.utils._os import upath

View File

@ -14,7 +14,7 @@ from unittest import skipIf
from django.core import mail from django.core import mail
from django.core.files.uploadedfile import SimpleUploadedFile from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.template.base import TemplateDoesNotExist from django.template import TemplateDoesNotExist
from django.test import RequestFactory, TestCase, override_settings from django.test import RequestFactory, TestCase, override_settings
from django.test.utils import LoggingCaptureMixin from django.test.utils import LoggingCaptureMixin
from django.utils import six from django.utils import six