Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
visitor.py93 linesDownload Raw Back to jinja2
1"""API for traversing the AST nodes. Implemented by the compiler and2meta introspection.3"""4 5import typing as t6 7from .nodes import Node8 9if t.TYPE_CHECKING:10    import typing_extensions as te11 12    class VisitCallable(te.Protocol):13        def __call__(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any: ...14 15 16class NodeVisitor:17    """Walks the abstract syntax tree and call visitor functions for every18    node found.  The visitor functions may return values which will be19    forwarded by the `visit` method.20 21    Per default the visitor functions for the nodes are ``'visit_'`` +22    class name of the node.  So a `TryFinally` node visit function would23    be `visit_TryFinally`.  This behavior can be changed by overriding24    the `get_visitor` function.  If no visitor function exists for a node25    (return value `None`) the `generic_visit` visitor is used instead.26    """27 28    def get_visitor(self, node: Node) -> "t.Optional[VisitCallable]":29        """Return the visitor function for this node or `None` if no visitor30        exists for this node.  In that case the generic visit function is31        used instead.32        """33        return getattr(self, f"visit_{type(node).__name__}", None)34 35    def visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any:36        """Visit a node."""37        f = self.get_visitor(node)38 39        if f is not None:40            return f(node, *args, **kwargs)41 42        return self.generic_visit(node, *args, **kwargs)43 44    def generic_visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any:45        """Called if no explicit visitor function exists for a node."""46        for child_node in node.iter_child_nodes():47            self.visit(child_node, *args, **kwargs)48 49 50class NodeTransformer(NodeVisitor):51    """Walks the abstract syntax tree and allows modifications of nodes.52 53    The `NodeTransformer` will walk the AST and use the return value of the54    visitor functions to replace or remove the old node.  If the return55    value of the visitor function is `None` the node will be removed56    from the previous location otherwise it's replaced with the return57    value.  The return value may be the original node in which case no58    replacement takes place.59    """60 61    def generic_visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> Node:62        for field, old_value in node.iter_fields():63            if isinstance(old_value, list):64                new_values = []65                for value in old_value:66                    if isinstance(value, Node):67                        value = self.visit(value, *args, **kwargs)68                        if value is None:69                            continue70                        elif not isinstance(value, Node):71                            new_values.extend(value)72                            continue73                    new_values.append(value)74                old_value[:] = new_values75            elif isinstance(old_value, Node):76                new_node = self.visit(old_value, *args, **kwargs)77                if new_node is None:78                    delattr(node, field)79                else:80                    setattr(node, field, new_node)81        return node82 83    def visit_list(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.List[Node]:84        """As transformers may return lists in some places this method85        can be used to enforce a list as return value.86        """87        rv = self.visit(node, *args, **kwargs)88 89        if not isinstance(rv, list):90            return [rv]91 92        return rv93 
codekingpro/portable-devtools · Team Ai