1 # -*- coding: utf-8 -*-
6 This module implements a visitor for the nodes.
8 :copyright: (c) 2010 by the Jinja Team.
11 from jinja2
.nodes
import Node
14 class NodeVisitor(object):
15 """Walks the abstract syntax tree and call visitor functions for every
16 node found. The visitor functions may return values which will be
17 forwarded by the `visit` method.
19 Per default the visitor functions for the nodes are ``'visit_'`` +
20 class name of the node. So a `TryFinally` node visit function would
21 be `visit_TryFinally`. This behavior can be changed by overriding
22 the `get_visitor` function. If no visitor function exists for a node
23 (return value `None`) the `generic_visit` visitor is used instead.
26 def get_visitor(self
, node
):
27 """Return the visitor function for this node or `None` if no visitor
28 exists for this node. In that case the generic visit function is
31 method
= 'visit_' + node
.__class
__.__name
__
32 return getattr(self
, method
, None)
34 def visit(self
, node
, *args
, **kwargs
):
36 f
= self
.get_visitor(node
)
38 return f(node
, *args
, **kwargs
)
39 return self
.generic_visit(node
, *args
, **kwargs
)
41 def generic_visit(self
, node
, *args
, **kwargs
):
42 """Called if no explicit visitor function exists for a node."""
43 for node
in node
.iter_child_nodes():
44 self
.visit(node
, *args
, **kwargs
)
47 class NodeTransformer(NodeVisitor
):
48 """Walks the abstract syntax tree and allows modifications of nodes.
50 The `NodeTransformer` will walk the AST and use the return value of the
51 visitor functions to replace or remove the old node. If the return
52 value of the visitor function is `None` the node will be removed
53 from the previous location otherwise it's replaced with the return
54 value. The return value may be the original node in which case no
55 replacement takes place.
58 def generic_visit(self
, node
, *args
, **kwargs
):
59 for field
, old_value
in node
.iter_fields():
60 if isinstance(old_value
, list):
62 for value
in old_value
:
63 if isinstance(value
, Node
):
64 value
= self
.visit(value
, *args
, **kwargs
)
67 elif not isinstance(value
, Node
):
68 new_values
.extend(value
)
70 new_values
.append(value
)
71 old_value
[:] = new_values
72 elif isinstance(old_value
, Node
):
73 new_node
= self
.visit(old_value
, *args
, **kwargs
)
77 setattr(node
, field
, new_node
)
80 def visit_list(self
, node
, *args
, **kwargs
):
81 """As transformers may return lists in some places this method
82 can be used to enforce a list as return value.
84 rv
= self
.visit(node
, *args
, **kwargs
)
85 if not isinstance(rv
, list):