Exemplo n.º 1
0
def parenthesize_expression(value):
    if value.type in [syms.comparison, syms.not_test]:
        parenthesized = parenthesize(value.clone())
        parenthesized.prefix = parenthesized.children[1].prefix
        parenthesized.children[1].prefix = ''
        value = parenthesized
    return value
def parenthesize_expression(value):
    if value.type in _NEEDS_PARENTHESIS:
        parenthesized = parenthesize(value.clone())
        parenthesized.prefix = parenthesized.children[1].prefix
        parenthesized.children[1].prefix = ''
        value = parenthesized
    return value
Exemplo n.º 3
0
def parenthesize_expression(value):
    if value.type in [syms.comparison, syms.not_test]:
        parenthesized = parenthesize(value.clone())
        parenthesized.prefix = parenthesized.children[1].prefix
        parenthesized.children[1].prefix = ''
        value = parenthesized
    return value
Exemplo n.º 4
0
 def transform(self, node, results):
     comp_for = results.get("comp_for").clone()
     is_dict = bool(results.get("col"))  # is it a dict?
     n1 = results.get("n1").clone()
     if is_dict:
         n2 = results.get("n2").clone()
         n2.prefix = " "
         impl_assign = tup((n1, n2))
     else:
         impl_assign = n1
     our_gencomp = Node(syms.listmaker, [(impl_assign), (comp_for)])
     if is_dict:
         new_node = Node(syms.power, [Name("dict"), parenthesize(Node(syms.atom, [our_gencomp]))])
     else:
         new_node = Node(syms.power, [Name("set"), parenthesize(Node(syms.atom, [our_gencomp]))])
     new_node.prefix = node.prefix
     return new_node
Exemplo n.º 5
0
    def _transform_dest(self, assert_arg_test_node: PyNode,
                        results: {str: PyNode}) -> bool:
        test = results["test"]
        test = test.clone()
        if test.type == GENERATOR_TYPE:
            test = parenthesize(test)
        test.prefix = " "

        # the destination node for 'a' is in conv_data:
        dest_node = self._get_node(assert_arg_test_node, self._arg_paths)
        dest_node.replace(test)

        return True
Exemplo n.º 6
0
def wrap_parens(arg_node: PyNode, checker_fn: callable) -> PyNode or PyLeaf:
    """
    If a node that represents an argument to assert_ function should be grouped, return a new node that adds
    parentheses around arg_node. Otherwise, return arg_node.
    :param arg_node: the arg_node to parenthesize
    :return: the arg_node for the parenthesized expression, or the arg_node itself
    """
    if isinstance(arg_node, PyNode) and checker_fn(arg_node):
        # log.info('adding parens: "{}" ({}), "{}" ({})'.format(first_child, first_child.type, sibling, sibling.type))
        # sometimes arg_node has parent, need to remove it before giving to parenthesize() then re-insert:
        parent = arg_node.parent
        if parent is not None:
            pos_parent = arg_node.remove()
            new_node = parenthesize(arg_node)
            parent.insert_child(pos_parent, new_node)
        else:
            new_node = parenthesize(arg_node)

        new_node.prefix = arg_node.prefix
        arg_node.prefix = ''
        return new_node

    return arg_node
Exemplo n.º 7
0
def wrap_parens(arg_node: PyNode, checker_fn: callable) -> PyNode or PyLeaf:
    """
    If a node that represents an argument to assert_ function should be grouped, return a new node that adds
    parentheses around arg_node. Otherwise, return arg_node.
    :param arg_node: the arg_node to parenthesize
    :return: the arg_node for the parenthesized expression, or the arg_node itself
    """
    if isinstance(arg_node, PyNode) and checker_fn(arg_node):
        # log.info('adding parens: "{}" ({}), "{}" ({})'.format(first_child, first_child.type, sibling, sibling.type))
        # sometimes arg_node has parent, need to remove it before giving to parenthesize() then re-insert:
        parent = arg_node.parent
        if parent is not None:
            pos_parent = arg_node.remove()
            new_node = parenthesize(arg_node)
            parent.insert_child(pos_parent, new_node)
        else:
            new_node = parenthesize(arg_node)

        new_node.prefix = arg_node.prefix
        arg_node.prefix = ''
        return new_node

    return arg_node
Exemplo n.º 8
0
 def transform(self, node, results):
     comp_for = results.get("comp_for").clone()
     is_dict = bool(results.get("col"))  # is it a dict?
     n1 = results.get("n1").clone()
     if is_dict:
         n2 = results.get("n2").clone()
         n2.prefix = " "
         impl_assign = tup((n1, n2))
     else:
         impl_assign = n1
     our_gencomp = Node(syms.listmaker, [(impl_assign), (comp_for)])
     if is_dict:
         new_node = Node(
             syms.power,
             [Name("dict"),
              parenthesize(Node(syms.atom, [our_gencomp]))])
     else:
         new_node = Node(
             syms.power,
             [Name("set"),
              parenthesize(Node(syms.atom, [our_gencomp]))])
     new_node.prefix = node.prefix
     return new_node
Exemplo n.º 9
0
    def transform(self, node: PyNode, results: {str: PyNode}) -> PyNode:
        assert results
        dest_tree = self.dest_tree.clone()
        assert_arg_test_node = self._get_node(dest_tree, (0, 0, 1))
        assert_args = assert_arg_test_node.parent

        if self._transform_dest(assert_arg_test_node, results):
            assert_arg_test_node = self._get_node(dest_tree, (0, 0, 1))
            if contains_newline(assert_arg_test_node):
                prefixes = assert_arg_test_node.prefix.split('\n', 1)
                assert_arg_test_node.prefix = '\n'+prefixes[1] if len(prefixes) > 1 else ''
                new_node = parenthesize(assert_arg_test_node.clone())
                new_node.prefix = prefixes[0] or ' '
                assert_arg_test_node.replace(new_node)

            self.__handle_opt_msg(assert_args, results)

            dest_tree.prefix = node.prefix
            return dest_tree

        else:
            return node
Exemplo n.º 10
0
    def transform(self, node: PyNode, results: {str: PyNode}) -> PyNode:
        assert results
        dest_tree = self.dest_tree.clone()
        assert_arg_test_node = self._get_node(dest_tree, (0, 0, 1))
        assert_args = assert_arg_test_node.parent

        if self._transform_dest(assert_arg_test_node, results):
            assert_arg_test_node = self._get_node(dest_tree, (0, 0, 1))
            if contains_newline(assert_arg_test_node):
                prefixes = assert_arg_test_node.prefix.split('\n', 1)
                assert_arg_test_node.prefix = '\n' + prefixes[1] if len(
                    prefixes) > 1 else ''
                # NOTE: parenthesize(node) needs an unparent node, so give it a clone:
                new_node = parenthesize(assert_arg_test_node.clone())
                new_node.prefix = prefixes[0] or ' '
                assert_arg_test_node.replace(new_node)

            self.__handle_opt_msg(assert_args, results)

            dest_tree.prefix = node.prefix
            return dest_tree

        else:
            return node
Exemplo n.º 11
0
    def transform(self, node, results):
        if not has_metaclass(node):
            return

        fixup_parse_tree(node)

        # find metaclasses, keep the last one
        last_metaclass = None
        for suite, i, stmt in find_metas(node):
            last_metaclass = stmt
            stmt.remove()

        text_type = node.children[0].type  # always Leaf(nnn, 'class')

        # figure out what kind of classdef we have
        if len(node.children) == 7:
            # Node(classdef, ['class', 'name', '(', arglist, ')', ':', suite])
            #                 0        1       2    3        4    5    6
            if node.children[3].type == syms.arglist:
                arglist = node.children[3]
            # Node(classdef, ['class', 'name', '(', 'Parent', ')', ':', suite])
            else:
                parent = node.children[3].clone()
                arglist = Node(syms.arglist, [parent])
                node.set_child(3, arglist)
        elif len(node.children) == 6:
            # Node(classdef, ['class', 'name', '(',  ')', ':', suite])
            #                 0        1       2     3    4    5
            arglist = Node(syms.arglist, [])
            node.insert_child(3, arglist)
        elif len(node.children) == 4:
            # Node(classdef, ['class', 'name', ':', suite])
            #                 0        1       2    3
            arglist = Node(syms.arglist, [])
            node.insert_child(2, Leaf(token.RPAR, u')'))
            node.insert_child(2, arglist)
            node.insert_child(2, Leaf(token.LPAR, u'('))
        else:
            raise ValueError("Unexpected class definition")

        # now stick the metaclass in the arglist
        meta_txt = last_metaclass.children[0].children[0]
        meta_txt.value = 'metaclass'
        orig_meta_prefix = meta_txt.prefix

        # Was: touch_import(None, u'future.utils', node)
        touch_import(u'future.utils', u'with_metaclass', node)

        metaclass = last_metaclass.children[0].children[2].clone()
        metaclass.prefix = u''

        arguments = [metaclass]

        if arglist.children:
            if len(arglist.children) == 1:
                base = arglist.children[0].clone()
                base.prefix = u' '
            else:
                # Unfortunately six.with_metaclass() only allows one base
                # class, so we have to dynamically generate a base class if
                # there is more than one.
                bases = parenthesize(arglist.clone())
                bases.prefix = u' '
                base = Call(Name('type'), [
                    String("'NewBase'"),
                    Comma(), bases,
                    Comma(),
                    Node(syms.atom,
                         [Leaf(token.LBRACE, u'{'),
                          Leaf(token.RBRACE, u'}')],
                         prefix=u' ')
                ],
                            prefix=u' ')
            arguments.extend([Comma(), base])

        arglist.replace(
            Call(Name(u'with_metaclass', prefix=arglist.prefix), arguments))

        fixup_indent(suite)

        # check for empty suite
        if not suite.children:
            # one-liner that was just __metaclass_
            suite.remove()
            pass_leaf = Leaf(text_type, u'pass')
            pass_leaf.prefix = orig_meta_prefix
            node.append_child(pass_leaf)
            node.append_child(Leaf(token.NEWLINE, u'\n'))

        elif len(suite.children) > 1 and \
                (suite.children[-2].type == token.INDENT and
                 suite.children[-1].type == token.DEDENT):
            # there was only one line in the class body and it was __metaclass__
            pass_leaf = Leaf(text_type, u'pass')
            suite.insert_child(-1, pass_leaf)
            suite.insert_child(-1, Leaf(token.NEWLINE, u'\n'))
Exemplo n.º 12
0
    def transform(self, node, results):
        if not has_metaclass(node):
            return

        fixup_parse_tree(node)

        # find metaclasses, keep the last one
        last_metaclass = None
        for suite, i, stmt in find_metas(node):
            last_metaclass = stmt
            stmt.remove()

        text_type = node.children[0].type # always Leaf(nnn, 'class')

        # figure out what kind of classdef we have
        if len(node.children) == 7:
            # Node(classdef, ['class', 'name', '(', arglist, ')', ':', suite])
            #                 0        1       2    3        4    5    6
            if node.children[3].type == syms.arglist:
                arglist = node.children[3]
            # Node(classdef, ['class', 'name', '(', 'Parent', ')', ':', suite])
            else:
                parent = node.children[3].clone()
                arglist = Node(syms.arglist, [parent])
                node.set_child(3, arglist)
        elif len(node.children) == 6:
            # Node(classdef, ['class', 'name', '(',  ')', ':', suite])
            #                 0        1       2     3    4    5
            arglist = Node(syms.arglist, [])
            node.insert_child(3, arglist)
        elif len(node.children) == 4:
            # Node(classdef, ['class', 'name', ':', suite])
            #                 0        1       2    3
            arglist = Node(syms.arglist, [])
            node.insert_child(2, Leaf(token.RPAR, u')'))
            node.insert_child(2, arglist)
            node.insert_child(2, Leaf(token.LPAR, u'('))
        else:
            raise ValueError("Unexpected class definition")

        touch_import(None, u'six', node)

        metaclass = last_metaclass.children[0].children[2].clone()
        metaclass.prefix = u''

        arguments = [metaclass]

        if arglist.children:
            if len(arglist.children) == 1:
                base = arglist.children[0].clone()
                base.prefix = u' '
            else:
                # Unfortunately six.with_metaclass() only allows one base
                # class, so we have to dynamically generate a base class if
                # there is more than one.
                bases = parenthesize(arglist.clone())
                bases.prefix = u' '
                base = Call(Name('type'), [
                    String("'NewBase'"),
                    Comma(),
                    bases,
                    Comma(),
                    Node(
                        syms.atom,
                        [Leaf(token.LBRACE, u'{'), Leaf(token.RBRACE, u'}')],
                        prefix=u' '
                    )
                ], prefix=u' ')
            arguments.extend([Comma(), base])

        arglist.replace(Call(
            Name(u'six.with_metaclass', prefix=arglist.prefix),
            arguments
        ))

        fixup_indent(suite)

        # check for empty suite
        if not suite.children:
            # one-liner that was just __metaclass_
            suite.remove()
            pass_leaf = Leaf(text_type, u'pass')
            pass_leaf.prefix = orig_meta_prefix
            node.append_child(pass_leaf)
            node.append_child(Leaf(token.NEWLINE, u'\n'))

        elif len(suite.children) > 1 and \
                 (suite.children[-2].type == token.INDENT and
                  suite.children[-1].type == token.DEDENT):
            # there was only one line in the class body and it was __metaclass__
            pass_leaf = Leaf(text_type, u'pass')
            suite.insert_child(-1, pass_leaf)
            suite.insert_child(-1, Leaf(token.NEWLINE, u'\n'))
Exemplo n.º 13
0
def tup(args):
    return parenthesize(Node(syms.testlist_gexp, commatize(args)))
Exemplo n.º 14
0
def tup(args):
    return parenthesize(Node(syms.testlist_gexp, commatize(args)))