Пример #1
0
 def parenthesize_test(self, node_to_split):
     first_child = node_to_split.children[0]
     if first_child != LParen():
         # node_to_split.children[0] is the "print" literal strip the
         # current 1st child, since we will be prepending an LParen
         if first_child.prefix != first_child.prefix.strip():
             first_child.prefix = first_child.prefix.strip()
             first_child.changed()
         left_paren = LParen()
         left_paren.prefix = " "
         node_to_split.insert_child(0, left_paren)
         node_to_split.append_child(RParen())
         node_to_split.changed()
Пример #2
0
 def parenthesize_print_stmt(self, node_to_split):
     # print "hello there"
     # return a, b
     second_child = node_to_split.children[1]
     if second_child != LParen():
         # node_to_split.children[0] is the "print" literal strip the
         # current 1st child, since we will be prepending an LParen
         if second_child.prefix != second_child.prefix.strip():
             second_child.prefix = second_child.prefix.strip()
             second_child.changed()
         node_to_split.insert_child(1, LParen())
         node_to_split.append_child(RParen())
         node_to_split.changed()
Пример #3
0
 def parenthesize_test(self, node_to_split):
     first_child = node_to_split.children[0]
     if first_child != LParen():
         # node_to_split.children[0] is the "print" literal strip the
         # current 1st child, since we will be prepending an LParen
         if first_child.prefix != first_child.prefix.strip():
             first_child.prefix = first_child.prefix.strip()
             first_child.changed()
         left_paren = LParen()
         left_paren.prefix = " "
         node_to_split.insert_child(0, left_paren)
         node_to_split.append_child(RParen())
         node_to_split.changed()
Пример #4
0
def add_sep_part(sep, pos, lst):
    if sep is not None and not isNone(sep) and \
       not (sep.type == token.STRING and sep.value in ("' '", '" "')):
        temp = []
        for arg in pos:
            temp.append(_unicode(arg.clone()))
            if sys.version_info >= (2, 6):
                warnings.warn(
                    "Calling unicode() on what may be a bytes object")
            temp.append(Comma())
        del temp[-1]
        sep = sep.clone()
        sep.prefix = " "
        args = Node(syms.listmaker, temp)
        new_list = Node(syms.atom,
                        [Leaf(token.LSQB, "["), args,
                         Leaf(token.RSQB, "]")])
        join_arg = Node(syms.trailer, [LParen(), new_list, RParen()])
        sep_join = Node(syms.power,
                        [sep, Node(syms.trailer, [Dot(), Name("join")])])
        lst.append(sep_join)
        lst.append(join_arg)
    else:
        if pos:
            pos[0].prefix = " "
        for arg in pos:
            lst.append(arg.clone())
            lst.append(Comma())
        del lst[-1]
Пример #5
0
 def parenthesize_call_stmt(self, node_to_split):
     # a.b().c()
     first_child = node_to_split.children[0]
     if first_child != LParen():
         # Since this can be at the beginning of a line, we can't just
         # strip the prefix, we need to keep leading whitespace
         first_child.prefix = "%s(" % first_child.prefix
         first_child.changed()
         node_to_split.append_child(RParen())
         node_to_split.changed()
Пример #6
0
 def parenthesize_after_arg(self, node_to_split, value):
     # parenthesize the leaves after the first node with the value
     value_index = 0
     for index, child in enumerate(node_to_split.children):
         if child.value == value:
             value_index = index + 1
             break
     value_child = node_to_split.children[value_index]
     if value_child != LParen():
         # strip the current 1st child, since we will be prepending an
         # LParen
         if value_child.prefix != value_child.prefix.strip():
             value_child.prefix = value_child.prefix.strip()
             value_child.changed()
         # We set a space prefix since this is after the '='
         left_paren = LParen()
         left_paren.prefix = " "
         node_to_split.insert_child(value_index, left_paren)
         node_to_split.append_child(RParen())
         node_to_split.changed()
Пример #7
0
 def parenthesize_after_arg(self, node_to_split, value):
     # parenthesize the leaves after the first node with the value
     value_index = 0
     for index, child in enumerate(node_to_split.children):
         if child.value == value:
             value_index = index + 1
             break
     value_child = node_to_split.children[value_index]
     if value_child != LParen():
         # strip the current 1st child, since we will be prepending an
         # LParen
         if value_child.prefix != value_child.prefix.strip():
             value_child.prefix = value_child.prefix.strip()
             value_child.changed()
         # We set a space prefix since this is after the '='
         left_paren = LParen()
         left_paren.prefix = " "
         node_to_split.insert_child(value_index, left_paren)
         node_to_split.append_child(RParen())
         node_to_split.changed()
Пример #8
0
def make_operand(node):
    """Convert a node into something we can put in a statement.

    Adds parentheses if needed.
    """
    if isinstance(node, Leaf) or NOPAREN_PATTERN.match(node):
        # No parentheses required in simple stuff
        result = [node.clone()]
    else:
        # Parentheses required in complex statements (eg. assertEqual(x + y, 17))
        result = [LParen(), node.clone(), RParen()]
        result[0].prefix = node.prefix
        result[1].prefix = ""
    return result
Пример #9
0
def Def(name, args, *body, prefix=""):
    return Node(syms.funcdef, [
        Name("def", prefix=prefix),
        maybe_name(name),
        Node(syms.parameters, [
            LParen(),
            args,
            RParen(),
        ]),
        Colon(),
        Node(syms.suite, [
            Newline(),
            Indent(),
            *body,
            Dedent(),
        ])
    ])
Пример #10
0
def add_end_part(end, file, parent, loc):
    if isNone(end):
        return
    if end.type == token.STRING and end.value in ("' '", '" "', "u' '", 'u" "',
                                                  "b' '", 'b" "'):
        return
    if file is None:
        touch_import(None, "sys", parent)
        file = Node(syms.power,
                    [Name("sys"),
                     Node(syms.trailer, [Dot(), Name("stdout")])])
    end_part = Node(syms.power, [
        file,
        Node(syms.trailer, [Dot(), Name("write")]),
        Node(syms.trailer, [LParen(), end, RParen()])
    ])
    end_part.prefix = " "
    parent.insert_child(loc, Leaf(token.SEMI, ";"))
    parent.insert_child(loc + 1, end_part)
Пример #11
0
 def transform(self, node, results):
     head = results['head']
     method = results['method'][0]
     tail = results['tail']
     syms = self.syms
     method_name = method.value
     if method_name == 'torsion':
         method_name = 'dihedral'
     head = [n.clone() for n in head]
     tail = [n.clone() for n in tail]
     args = head + [
         pytree.Node(syms.trailer, [
             Dot(),
             Name(method_name, prefix=method.prefix),
             Dot(),
             Name('value'),
             LParen(),
             RParen()
         ])
     ]
     new = pytree.Node(syms.power, args)
     return new
Пример #12
0
 def transform(self, node, results):
     syms = self.syms
     prefix = node.prefix
     args = results.get(u"args")
     arg = results.get(u"arg")
     if args:
         args = [arg.clone() for arg in args]
         args = Node(syms.atom, [
             Leaf(token.LSQB, u"["),
             Node(syms.listmaker, args),
             Leaf(token.RSQB, u"]")
         ])
     elif arg:
         arg = arg.clone()
         arg = Node(syms.atom, [
             Leaf(token.LSQB, u"["),
             Node(syms.listmaker, [arg]),
             Leaf(token.RSQB, u"]")
         ])
     return Node(
         syms.power,
         [Name(u"set"), LParen(), args or arg,
          RParen()],
         prefix=prefix)
Пример #13
0
def insert_object(node, idx):
    node.insert_child(idx, RParen())
    node.insert_child(idx, Name(u"object"))
    node.insert_child(idx, LParen())
Пример #14
0
 def parenthesize_expr_stmt(self, node_to_split):
     # x = "foo" + bar
     if node_to_split.children[0] != LParen():
         node_to_split.insert_child(0, LParen())
         node_to_split.append_child(RParen())
         node_to_split.changed()