def _replace_node(node0: Node, node1: Node) -> None: suite0 = node0.children[-1] assert node_name(suite0) == "suite" for i, x in enumerate(suite0.children): if node_name( x) == "simple_stmt" and x.children[0].type == token.STRING: break has_comment0 = i < (len(suite0.children) - 1) suite1 = node1.children[-1] assert node_name(suite1) == "suite" before_comments1 = [] for j, x in enumerate(suite1.children): before_comments1.append(x) if node_name( x) == "simple_stmt" and x.children[0].type == token.STRING: break has_comment1 = j < (len(suite1.children) - 1) if has_comment1: if has_comment0: suite0.children[i] = suite1.children[j] else: assert suite0.children[0].type == token.NEWLINE assert suite0.children[1].type == token.INDENT suite0.children = before_comments1 + suite0.children[1:] suite0.parent.changed() node0.children = [*node1.children[:-1], node0.children[-1]] node0.parent.changed()
def merge(t0, t1): """update parameters and docstring by t1, if not existed, inserted""" for name, node1 in v1.defs.items(): node0 = v0.defs.get(name) if node0 is None: # insert ancestors = [] target = node1 while target is not None: ancestors.append(node_name(target)) target = target.parent assert ancestors[-1] == "file_input" is_toplevel_def = ancestors[-2] in ("funcdef", "classdef", "async_funcdef") if not is_toplevel_def: continue node1.prefix = "\n\n" t0.append_child(node1) else: # update suite0 = node0.children[-1] assert node_name(suite0) == "suite" for i, x in enumerate(suite0.children): if node_name(x) == "simple_stmt" and x.children[ 0].type == token.STRING: break has_comment0 = i < (len(suite0.children) - 1) suite1 = node1.children[-1] assert node_name(suite1) == "suite" before_comments1 = [] for j, x in enumerate(suite1.children): before_comments1.append(x) if node_name(x) == "simple_stmt" and x.children[ 0].type == token.STRING: break has_comment1 = j < (len(suite1.children) - 1) if has_comment1: if has_comment0: suite0.children[i] = suite1.children[j] else: assert suite0.children[0].type == token.NEWLINE assert suite0.children[1].type == token.INDENT suite0.children = before_comments1 + suite0.children[1:] suite0.parent.changed() node0.children = [*node1.children[:-1], node0.children[-1]] node0.parent.changed() return t0
def visit(self, node): try: super().visit(node) if self.path and node_name(node) in ("funcdef", "classdef"): if self.path[-1] == node.children[1].value: self.path.pop() finally: self.level -= 1
def _insert_node(t0, node) -> None: ancestors = [] target = node while target is not None: ancestors.append(node_name(target)) target = target.parent assert ancestors[-1] == "file_input" is_toplevel_def = ancestors[-2] in ("funcdef", "classdef", "async_funcdef") if not is_toplevel_def: return node.prefix = "\n\n" t0.append_child(node)
def run(t): visitor = Visitor() visitor.visit(t) for node in visitor.r: target = node defs = [] while target: if node_name(target) == "funcdef": defs.append(target) elif target.parent is None: break target = target.parent yield defs
def find_parents(node: Node) -> Node: r = [node] seen = set() target = node while target: if id(target) in seen: break if node_name(target) in ("funcdef", "classdef"): r.append(target) elif target.parent is None: break seen.add(id(target)) target = target.parent return reversed(r)
def patch(t): visitor = Visitor() visitor.visit(t) seen = set() for node in visitor.r: target = node while target: if node_name(target) == "funcdef": if id(target) not in seen: seen.add(id(target)) # xxx: insert_before(target, Decorator("profile")) target.prefix = u.find_indentation(target) elif target.parent is None: break target = target.parent
def rfind(t, lineno): visitor = Visitor(lineno) visitor.visit(t) r = [lineno] history = set([lineno]) seen = set() for node in visitor.r: target = node while target: if id(target) in seen: break if node_name(target) in ("funcdef", "classdef"): i = target.get_lineno() if i not in history: history.add(i) r.append(target.get_lineno()) elif target.parent is None: break seen.add(id(target)) target = target.parent return reversed(r)