PageRenderTime 43ms CodeModel.GetById 19ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/bangkokhotel/lib/python2.5/site-packages/django/template/debug.py

https://bitbucket.org/luisrodriguez/bangkokhotel
Python | 97 lines | 80 code | 17 blank | 0 comment | 10 complexity | 60bef10d60a9c97005cd01d2e8d40172 MD5 | raw file
 1from django.template.base import Lexer, Parser, tag_re, NodeList, VariableNode, TemplateSyntaxError
 2from django.utils.encoding import force_unicode
 3from django.utils.html import escape
 4from django.utils.safestring import SafeData, EscapeData
 5from django.utils.formats import localize
 6from django.utils.timezone import localtime
 7
 8
 9class DebugLexer(Lexer):
10    def __init__(self, template_string, origin):
11        super(DebugLexer, self).__init__(template_string, origin)
12
13    def tokenize(self):
14        "Return a list of tokens from a given template_string"
15        result, upto = [], 0
16        for match in tag_re.finditer(self.template_string):
17            start, end = match.span()
18            if start > upto:
19                result.append(self.create_token(self.template_string[upto:start], (upto, start), False))
20                upto = start
21            result.append(self.create_token(self.template_string[start:end], (start, end), True))
22            upto = end
23        last_bit = self.template_string[upto:]
24        if last_bit:
25            result.append(self.create_token(last_bit, (upto, upto + len(last_bit)), False))
26        return result
27
28    def create_token(self, token_string, source, in_tag):
29        token = super(DebugLexer, self).create_token(token_string, in_tag)
30        token.source = self.origin, source
31        return token
32
33class DebugParser(Parser):
34    def __init__(self, lexer):
35        super(DebugParser, self).__init__(lexer)
36        self.command_stack = []
37
38    def enter_command(self, command, token):
39        self.command_stack.append( (command, token.source) )
40
41    def exit_command(self):
42        self.command_stack.pop()
43
44    def error(self, token, msg):
45        return self.source_error(token.source, msg)
46
47    def source_error(self, source, msg):
48        e = TemplateSyntaxError(msg)
49        e.django_template_source = source
50        return e
51
52    def create_nodelist(self):
53        return DebugNodeList()
54
55    def create_variable_node(self, contents):
56        return DebugVariableNode(contents)
57
58    def extend_nodelist(self, nodelist, node, token):
59        node.source = token.source
60        super(DebugParser, self).extend_nodelist(nodelist, node, token)
61
62    def unclosed_block_tag(self, parse_until):
63        command, source = self.command_stack.pop()
64        msg = "Unclosed tag '%s'. Looking for one of: %s " % (command, ', '.join(parse_until))
65        raise self.source_error(source, msg)
66
67    def compile_function_error(self, token, e):
68        if not hasattr(e, 'django_template_source'):
69            e.django_template_source = token.source
70
71class DebugNodeList(NodeList):
72    def render_node(self, node, context):
73        try:
74            return node.render(context)
75        except Exception, e:
76            if not hasattr(e, 'django_template_source'):
77                e.django_template_source = node.source
78            raise
79
80
81class DebugVariableNode(VariableNode):
82    def render(self, context):
83        try:
84            output = self.filter_expression.resolve(context)
85            output = localtime(output, use_tz=context.use_tz)
86            output = localize(output, use_l10n=context.use_l10n)
87            output = force_unicode(output)
88        except UnicodeDecodeError:
89            return ''
90        except Exception, e:
91            if not hasattr(e, 'django_template_source'):
92                e.django_template_source = self.source
93            raise
94        if (context.autoescape and not isinstance(output, SafeData)) or isinstance(output, EscapeData):
95            return escape(output)
96        else:
97            return output