Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/django/template/loaders/locmem.py
blob: a27dcd845b53a263b616a7de003d7a8d75d13997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Wrapper for loading templates from a plain Python dict.
"""

import warnings

from django.template import Origin, TemplateDoesNotExist
from django.utils.deprecation import RemovedInDjango20Warning

from .base import Loader as BaseLoader


class Loader(BaseLoader):

    def __init__(self, engine, templates_dict):
        self.templates_dict = templates_dict
        super(Loader, self).__init__(engine)

    def get_contents(self, origin):
        try:
            return self.templates_dict[origin.name]
        except KeyError:
            raise TemplateDoesNotExist(origin)

    def get_template_sources(self, template_name):
        yield Origin(
            name=template_name,
            template_name=template_name,
            loader=self,
        )

    def load_template_source(self, template_name, template_dirs=None):
        warnings.warn(
            'The load_template_sources() method is deprecated. Use '
            'get_template() or get_contents() instead.',
            RemovedInDjango20Warning,
        )
        try:
            return self.templates_dict[template_name], template_name
        except KeyError:
            raise TemplateDoesNotExist(template_name)