(self, tree, name)
return tree.was_changed
Refactors a parse tree (modifying the tree in place).

        For compatible patterns the bottom matcher module is
        used. Otherwise the tree is traversed node-to-node for
        matches.

        Args:
            tree: a pytree.Node instance representing the root of the tree
                  to be refactored.
            name: a human-readable name for this tree.

        Returns:
            True if the tree was modified, False otherwise.
Refactors a parse tree (modifying the tree in place).
Refactors a parse tree ( modifying the tree in place ) .
def refactor_tree(self, tree, name):
        """Refactors a parse tree (modifying the tree in place).

        For compatible patterns the bottom matcher module is
        used. Otherwise the tree is traversed node-to-node for
        matches.

        Args:
            tree: a pytree.Node instance representing the root of the tree
                  to be refactored.
            name: a human-readable name for this tree.

        Returns:
            True if the tree was modified, False otherwise.
        """

        for fixer in chain(self.pre_order, self.post_order):
            fixer.start_tree(tree, name)

        #use traditional matching for the incompatible fixers
        self.traverse_by(self.bmi_pre_order_heads, tree.pre_order())
        self.traverse_by(self.bmi_post_order_heads, tree.post_order())

        # obtain a set of candidate nodes
        match_set = self.BM.run(tree.leaves())

        while any(match_set.values()):
            for fixer in self.BM.fixers:
                if fixer in match_set and match_set[fixer]:
                    #sort by depth; apply fixers from bottom(of the AST) to top
                    match_set[fixer].sort(key=pytree.Base.depth, reverse=True)

                    if fixer.keep_line_order:
                        #some fixers(eg fix_imports) must be applied
                        #with the original file's line order
                        match_set[fixer].sort(key=pytree.Base.get_lineno)

                    for node in list(match_set[fixer]):
                        if node in match_set[fixer]:
                            match_set[fixer].remove(node)

                        try:
                            find_root(node)
                        except AssertionError:
                            # this node has been cut off from a
                            # previous transformation ; skip
                            continue

                        if node.fixers_applied and fixer in node.fixers_applied:
                            # do not apply the same fixer again
                            continue

                        results = fixer.match(node)

                        if results:
                            new = fixer.transform(node, results)
                            if new is not None:
                                node.replace(new)
                                #new.fixers_applied.append(fixer)
                                for node in new.post_order():
                                    # do not apply the fixer again to
                                    # this or any subnode
                                    if not node.fixers_applied:
                                        node.fixers_applied = []
                                    node.fixers_applied.append(fixer)

                                # update the original match set for
                                # the added code
                                new_matches = self.BM.run(new.leaves())
                                for fxr in new_matches:
                                    if not fxr in match_set:
                                        match_set[fxr]=[]

                                    match_set[fxr].extend(new_matches[fxr])

        for fixer in chain(self.pre_order, self.post_order):
            fixer.finish_tree(tree, name)
        return tree.was_changed
def refactor_tree ( self , tree , name ) : for fixer in chain ( self . pre_order , self . post_order ) : fixer . start_tree ( tree , name ) #use traditional matching for the incompatible fixers self . traverse_by ( self . bmi_pre_order_heads , tree . pre_order ( ) ) self . traverse_by ( self . bmi_post_order_heads , tree . post_order ( ) ) # obtain a set of candidate nodes match_set = self . BM . run ( tree . leaves ( ) ) while any ( match_set . values ( ) ) : for fixer in self . BM . fixers : if fixer in match_set and match_set [ fixer ] : #sort by depth; apply fixers from bottom(of the AST) to top match_set [ fixer ] . sort ( key = pytree . Base . depth , reverse = True ) if fixer . keep_line_order : #some fixers(eg fix_imports) must be applied #with the original file's line order match_set [ fixer ] . sort ( key = pytree . Base . get_lineno ) for node in list ( match_set [ fixer ] ) : if node in match_set [ fixer ] : match_set [ fixer ] . remove ( node ) try : find_root ( node ) except AssertionError : # this node has been cut off from a # previous transformation ; skip continue if node . fixers_applied and fixer in node . fixers_applied : # do not apply the same fixer again continue results = fixer . match ( node ) if results : new = fixer . transform ( node , results ) if new is not None : node . replace ( new ) #new.fixers_applied.append(fixer) for node in new . post_order ( ) : # do not apply the fixer again to # this or any subnode if not node . fixers_applied : node . fixers_applied = [ ] node . fixers_applied . append ( fixer ) # update the original match set for # the added code new_matches = self . BM . run ( new . leaves ( ) ) for fxr in new_matches : if not fxr in match_set : match_set [ fxr ] = [ ] match_set [ fxr ] . extend ( new_matches [ fxr ] ) for fixer in chain ( self . pre_order , self . post_order ) : fixer . finish_tree ( tree , name ) return tree . was_changed