Example #1
0
    def transform(self, node, results):
        fixup_parse_tree(node)

        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")

        for child in arglist.children:
            if child.value == 'Torsion':
                child.value = 'Dihedral'
            if child.value == 'Improper_Torsion':
                child.value = 'ImproperDihedral'
Example #2
0
 def transform(self, node, results):
     FIXME
     name, val, trc = (results.get(u"name"), results.get(u"val"),
                       results.get(u"trc"))
     chain = results.get(u"chain")
     if chain is not None:
         self.warning(
             node,
             u"explicit exception chaining is not supported in Python 2")
         chain.prev_sibling.remove()
         chain.remove()
     if trc is not None:
         val = val[0] if val else Leaf(token.NAME, u"None")
         val.prefix = trc.prefix = u" "
         kids = [
             Leaf(token.NAME, u"raise"),
             name.clone(),
             Comma(),
             val.clone(),
             Comma(),
             trc.clone()
         ]
         raise_stmt = Node(syms.raise_stmt, kids)
         node.replace(raise_stmt)
    def transform(self, node, results):
        u"""
        This replaces function type annotations with a commented version.
        """

        params = results.get(u"params")
        body = results.get(u"body")
        ret = results.get(u"ret")
        lhs = results.get(u"lhs")
        ann = results.get(u"ann")
        rhs = results.get(u"rhs")
        annnode = results.get(u"annnode")
        
        if params is not None and body is not None:
            ret_type = u"None"
            types = []
            for child in params.children:
                if child.type == syms.typedargslist:
                    types = handle_typedargslist(child)
                elif child.type == syms.tname:
                    types = retrieve_type(child)
                    handle_tname_or_name(child)
            if ret is not None:
                assert ret.prev_sibling.type == token.RARROW, u"invalid return annotation"
                ret_type = ret.value
                ret.prev_sibling.remove()
                ret.remove()
            type_sig = "# type: ({}) -> {}".format(", ".join(types), ret_type)
            type_sig_comment = Leaf(token.COMMENT, type_sig)

            indents = [l for l in body.leaves() if l.type == token.INDENT]
            if not len(indents):
                indent_node = Leaf(token.INDENT, "    ")
                body.insert_child(0, indent_node)
                body.insert_child(0, Leaf(token.NEWLINE, "\n"))
            else:
                indent_node = indents[0]
            body.insert_child(0, type_sig_comment)
            body.insert_child(0, indent_node)
            body.insert_child(0, Leaf(token.NEWLINE, "\n"))
        elif ann is not None:
            type_ann_comment = Leaf(token.COMMENT, "  # type: {}".format(
                ''.join(x.value for x in ann.leaves())
            ))
            lhs.parent.append_child(Leaf(token.EQUAL, " ="))
            if rhs is None:
                lhs.parent.append_child(Leaf(token.NAME, " None"))
            else:
                lhs.parent.append_child(rhs)
            annnode.remove()
            lhs.parent.append_child(type_ann_comment)
Example #4
0
def Import(name_leafs):

    for leaf in name_leafs:
        # Pull the leaves out of their old tree
        leaf.remove()

    def add_commas(leafs):
        yield leafs[0]
        for a in leafs[1:]:
            yield Comma()
            yield a

    children = [Leaf(token.NAME, u'import'),
                Node(syms.dotted_as_names, list(add_commas(name_leafs)))]
    imp = Node(syms.import_name, children)
    return imp
Example #5
0
def assignment_source(num_pre, num_post, LISTNAME, ITERNAME):
    u"""
    Accepts num_pre and num_post, which are counts of values
    before and after the starg (not including the starg)
    Returns a source fit for Assign() from fixer_util
    """
    children = []
    pre = unicode(num_pre)
    post = unicode(num_post)
    # This code builds the assignment source from lib2to3 tree primitives.
    # It's not very readable, but it seems like the most correct way to do it.
    if num_pre > 0:
        pre_part = Node(syms.power, [
            Name(LISTNAME),
            Node(syms.trailer, [
                Leaf(token.LSQB, u"["),
                Node(syms.subscript, [Leaf(token.COLON, u":"),
                                      Number(pre)]),
                Leaf(token.RSQB, u"]")
            ])
        ])
        children.append(pre_part)
        children.append(Leaf(token.PLUS, u"+", prefix=u" "))
    main_part = Node(syms.power, [
        Leaf(token.LSQB, u"[", prefix=u" "),
        Name(LISTNAME),
        Node(syms.trailer, [
            Leaf(token.LSQB, u"["),
            Node(syms.subscript, [
                Number(pre) if num_pre > 0 else Leaf(1, u""),
                Leaf(token.COLON, u":"),
                Node(syms.factor, [Leaf(token.MINUS, u"-"),
                                   Number(post)]) if num_post > 0 else Leaf(
                                       1, u"")
            ]),
            Leaf(token.RSQB, u"]"),
            Leaf(token.RSQB, u"]")
        ])
    ])
    children.append(main_part)
    if num_post > 0:
        children.append(Leaf(token.PLUS, u"+", prefix=u" "))
        post_part = Node(syms.power, [
            Name(LISTNAME, prefix=u" "),
            Node(syms.trailer, [
                Leaf(token.LSQB, u"["),
                Node(syms.subscript, [
                    Node(syms.factor, [Leaf(token.MINUS, u"-"),
                                       Number(post)]),
                    Leaf(token.COLON, u":")
                ]),
                Leaf(token.RSQB, u"]")
            ])
        ])
        children.append(post_part)
    source = Node(syms.arith_expr, children)
    return source
Example #6
0
    def transform(self, node, results):
        if not has_metaclass(node):
            return  # pragma: no cover

        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")  # pragma: no cover

        touch_import(None, u'six', node)

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

        arguments = [metaclass]

        if arglist.children:
            bases = arglist.clone()
            bases.prefix = u' '
            arguments.extend([Comma(), bases])

        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 = last_metaclass.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'))
Example #7
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'))