summaryrefslogtreecommitdiff
path: root/src/jinja2/nativetypes.py
blob: e0ad94d3248e5a5abc3d57648848c578fe61f4f9 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import types
from ast import literal_eval
from itertools import chain
from itertools import islice

from . import nodes
from .compiler import CodeGenerator
from .compiler import has_safe_repr
from .environment import Environment
from .environment import Template


def native_concat(nodes, preserve_quotes=True):
    """Return a native Python type from the list of compiled nodes. If
    the result is a single node, its value is returned. Otherwise, the
    nodes are concatenated as strings. If the result can be parsed with
    :func:`ast.literal_eval`, the parsed value is returned. Otherwise,
    the string is returned.

    :param nodes: Iterable of nodes to concatenate.
    :param preserve_quotes: Whether to re-wrap literal strings with
        quotes, to preserve quotes around expressions for later parsing.
        Should be ``False`` in :meth:`NativeEnvironment.render`.
    """
    head = list(islice(nodes, 2))

    if not head:
        return None

    if len(head) == 1:
        raw = head[0]
    else:
        if isinstance(nodes, types.GeneratorType):
            nodes = chain(head, nodes)
        raw = "".join([str(v) for v in nodes])

    try:
        literal = literal_eval(raw)
    except (ValueError, SyntaxError, MemoryError):
        return raw

    # If literal_eval returned a string, re-wrap with the original
    # quote character to avoid dropping quotes between expression nodes.
    # Without this, "'{{ a }}', '{{ b }}'" results in "a, b", but should
    # be ('a', 'b').
    if preserve_quotes and isinstance(literal, str):
        quote = raw[0]
        return f"{quote}{literal}{quote}"

    return literal


class NativeCodeGenerator(CodeGenerator):
    """A code generator which renders Python types by not adding
    ``str()`` around output nodes, and using :func:`native_concat`
    to convert complex strings back to Python types if possible.
    """

    @staticmethod
    def _default_finalize(value):
        return value

    def _output_const_repr(self, group):
        return repr(native_concat(group))

    def _output_child_to_const(self, node, frame, finalize):
        const = node.as_const(frame.eval_ctx)

        if not has_safe_repr(const):
            raise nodes.Impossible()

        if isinstance(node, nodes.TemplateData):
            return const

        return finalize.const(const)

    def _output_child_pre(self, node, frame, finalize):
        if finalize.src is not None:
            self.write(finalize.src)

    def _output_child_post(self, node, frame, finalize):
        if finalize.src is not None:
            self.write(")")


class NativeEnvironment(Environment):
    """An environment that renders templates to native Python types."""

    code_generator_class = NativeCodeGenerator


class NativeTemplate(Template):
    environment_class = NativeEnvironment

    def render(self, *args, **kwargs):
        """Render the template to produce a native Python type. If the
        result is a single node, its value is returned. Otherwise, the
        nodes are concatenated as strings. If the result can be parsed
        with :func:`ast.literal_eval`, the parsed value is returned.
        Otherwise, the string is returned.
        """
        vars = dict(*args, **kwargs)
        try:
            return native_concat(
                self.root_render_func(self.new_context(vars)), preserve_quotes=False
            )
        except Exception:
            return self.environment.handle_exception()


NativeEnvironment.template_class = NativeTemplate