visitor.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. """
  3. jinja2.visitor
  4. ~~~~~~~~~~~~~~
  5. This module implements a visitor for the nodes.
  6. :copyright: (c) 2017 by the Jinja Team.
  7. :license: BSD.
  8. """
  9. from jinja2.nodes import Node
  10. class NodeVisitor(object):
  11. """Walks the abstract syntax tree and call visitor functions for every
  12. node found. The visitor functions may return values which will be
  13. forwarded by the `visit` method.
  14. Per default the visitor functions for the nodes are ``'visit_'`` +
  15. class name of the node. So a `TryFinally` node visit function would
  16. be `visit_TryFinally`. This behavior can be changed by overriding
  17. the `get_visitor` function. If no visitor function exists for a node
  18. (return value `None`) the `generic_visit` visitor is used instead.
  19. """
  20. def get_visitor(self, node):
  21. """Return the visitor function for this node or `None` if no visitor
  22. exists for this node. In that case the generic visit function is
  23. used instead.
  24. """
  25. method = 'visit_' + node.__class__.__name__
  26. return getattr(self, method, None)
  27. def visit(self, node, *args, **kwargs):
  28. """Visit a node."""
  29. f = self.get_visitor(node)
  30. if f is not None:
  31. return f(node, *args, **kwargs)
  32. return self.generic_visit(node, *args, **kwargs)
  33. def generic_visit(self, node, *args, **kwargs):
  34. """Called if no explicit visitor function exists for a node."""
  35. for node in node.iter_child_nodes():
  36. self.visit(node, *args, **kwargs)
  37. class NodeTransformer(NodeVisitor):
  38. """Walks the abstract syntax tree and allows modifications of nodes.
  39. The `NodeTransformer` will walk the AST and use the return value of the
  40. visitor functions to replace or remove the old node. If the return
  41. value of the visitor function is `None` the node will be removed
  42. from the previous location otherwise it's replaced with the return
  43. value. The return value may be the original node in which case no
  44. replacement takes place.
  45. """
  46. def generic_visit(self, node, *args, **kwargs):
  47. for field, old_value in node.iter_fields():
  48. if isinstance(old_value, list):
  49. new_values = []
  50. for value in old_value:
  51. if isinstance(value, Node):
  52. value = self.visit(value, *args, **kwargs)
  53. if value is None:
  54. continue
  55. elif not isinstance(value, Node):
  56. new_values.extend(value)
  57. continue
  58. new_values.append(value)
  59. old_value[:] = new_values
  60. elif isinstance(old_value, Node):
  61. new_node = self.visit(old_value, *args, **kwargs)
  62. if new_node is None:
  63. delattr(node, field)
  64. else:
  65. setattr(node, field, new_node)
  66. return node
  67. def visit_list(self, node, *args, **kwargs):
  68. """As transformers may return lists in some places this method
  69. can be used to enforce a list as return value.
  70. """
  71. rv = self.visit(node, *args, **kwargs)
  72. if not isinstance(rv, list):
  73. rv = [rv]
  74. return rv