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 transform_arglist_to_keywords(self, arglist_node, alg_object): """Takes a node that points to argument list and transforms it to all keyword=values @param arglist_node The node that points to the argument list @param alg_object The algorithm object that corresponds to this list """ ordered_props = alg_object.orderedProperties() # Special case where the arglist has no children if len(arglist_node.children) == 0: arglist_node = Node(syms.argument, [Leaf(token.NAME,ordered_props[0]),Leaf(token.EQUAL,"="), Leaf(arglist_node.type,arglist_node.value)]) return arglist_node # Quick check: A 3 arg leaf list with an equals at the 2nd element needs nothing doing if len(arglist_node.children) == 3 and arglist_node.children[1].type == token.EQUAL: return arglist_node # Construct our argument list from the children to make sure we separate out whole comma-separated # sections i.e get embedded lists correct args = [[]] # Each list will be delimited by a comma nargs = 0 index = 0 for node in arglist_node.children: if node.type == token.COMMA: args.append(node) args.append([]) # new arg list index += 2 # include comma nargs += 1 else: args[index].append(node) # Ordered props prop_index = 0 # List has commas so standard enumerate won't do the trick arg_nodes = [] # Holds the final node list for arg_list in args: if isinstance(arg_list, Leaf): # Must be comma from construction above arg_nodes.append(arg_list) else: first = arg_list[0] if not (isinstance(first, Node) and first.type == syms.argument): prop_name = ordered_props[prop_index] children=[Leaf(token.NAME,prop_name),Leaf(token.EQUAL,"=")] children.extend(arg_list) for c in children: c.parent = None # A new node requires all old parent nodes to be None arg_nodes.append(Node(syms.argument, children)) else: for node in arg_list: arg_nodes.append(node) # Move to the next property prop_index += 1 arglist_node.children = arg_nodes return arglist_node