Example #1
0
 def transform(self, node, results):
     if _literal_re.match(node.value):
         touch_import(None, "six", node)
         new = node.clone()
         new.value = new.value[1:]
         new.prefix = ""
         node.replace(Call(Name("six.u", prefix=node.prefix), [new]))
Example #2
0
    def transform_iter(self, node, results):
        """Call six.(iter|view)items() and friends."""
        # Make sure six is imported.
        libmodernize.touch_import(None, "six", node)

        # Copy of self.transform() from fissix.fix_dict with some changes to
        # use the six.* methods.

        head = results["head"]
        method = results["method"][0]  # Extract node for method name
        tail = results["tail"]
        syms = self.syms
        method_name = method.value
        name = fixer_util.Name("six." + method_name, prefix=node.prefix)
        assert method_name.startswith(("iter", "view")), repr(method)
        assert method_name[4:] in ("keys", "items", "values"), repr(method)
        head = [n.clone() for n in head]
        tail = [n.clone() for n in tail]
        new = pytree.Node(syms.power, head)
        new.prefix = ""
        new = fixer_util.Call(name, [new])
        if tail:
            new = pytree.Node(syms.power, [new] + tail)
        new.prefix = node.prefix
        return new
Example #3
0
    def transform_iter(self, node, results):
        """Call six.(iter|view)items() and friends."""
        # Make sure six is imported.
        libmodernize.touch_import(None, u'six', node)

        # Copy of self.transform() from fissix.fix_dict with some changes to
        # use the six.* methods.

        head = results['head']
        method = results['method'][0]  # Extract node for method name
        tail = results['tail']
        syms = self.syms
        method_name = method.value
        name = fixer_util.Name(u'six.' + method_name, prefix=node.prefix)
        assert method_name.startswith((u'iter', u'view')), repr(method)
        assert method_name[4:] in (u'keys', u'items', u'values'), repr(method)
        head = [n.clone() for n in head]
        tail = [n.clone() for n in tail]
        new = pytree.Node(syms.power, head)
        new.prefix = u''
        new = fixer_util.Call(name, [new])
        if tail:
            new = pytree.Node(syms.power, [new] + tail)
        new.prefix = node.prefix
        return new
 def transform_iter(self, method_name, node, base):
     """Call six.(iter|view)items() and friends."""
     libmodernize.touch_import(None, u'six', node)
     new_node = [n.clone() for n in base]
     new_node[0].prefix = u''
     name = fixer_util.Name(u'six.' + method_name, prefix=node.prefix)
     node.replace(fixer_util.Call(name, new_node))
Example #5
0
 def transform_iter(self, method_name, node, base):
     """Call six.(iter|view)items() and friends."""
     libmodernize.touch_import(None, u'six', node)
     new_node = [n.clone() for n in base]
     new_node[0].prefix = u''
     name = fixer_util.Name(u'six.' + method_name, prefix=node.prefix)
     node.replace(fixer_util.Call(name, new_node))
Example #6
0
 def transform(self, node, results):
     result = super(FixMap, self).transform(node, results)
     if not libmodernize.is_listcomp(result):
         # Always use the import even if no change is required so as to have
         # improved performance in iterator contexts even on Python 2.7.
         libmodernize.touch_import(u'six.moves', u'map', node)
     return result
Example #7
0
 def transform(self, node, results):
     if _literal_re.match(node.value):
         touch_import(None, u'six', node)
         new = node.clone()
         new.value = new.value[1:]
         new.prefix = ''
         node.replace(Call(Name(u'six.u', prefix=node.prefix), [new]))
Example #8
0
 def transform(self, node, results):
     result = super(FixFilter, self).transform(node, results)
     if not libmodernize.is_listcomp(result):
         # Keep performance improvement from six.moves.filter in iterator
         # contexts on Python 2.7.
         libmodernize.touch_import(u'six.moves', u'filter', node)
     return result
Example #9
0
 def transform(self, node, results):
     result = super(FixMap, self).transform(node, results)
     if not libmodernize.is_listcomp(result):
         # Always use the import even if no change is required so as to have
         # improved performance in iterator contexts even on Python 2.7.
         libmodernize.touch_import(u'six.moves', u'map', node)
     return result
 def transform(self, node, results):
     libmodernize.touch_import("scalyr_agent", "compat", node)
     method = results["method"]
     head = results["head"]
     head.value = head.value.replace("os", "compat")
     method.value = "os_getenv_unicode"
     node.changed()
    def transform(self, node, results):
        libmodernize.touch_import("scalyr_agent", "compat", node)
        head = results["head"]
        method = results["method"][0]
        head.value = "compat"
        method.value = "struct_{}_unicode".format(method.value)

        node.changed()
 def transform_iter(self, method_name, node, base):
     """Call six.iteritems() and friends."""
     if method_name.startswith(u'view'):
         method_name = u'iter' + method_name[4:]
     libmodernize.touch_import(None, u'six', node)
     new_node = [n.clone() for n in base]
     new_node[0].prefix = u''
     name = fixer_util.Name(u'six.' + method_name, prefix=node.prefix)
     node.replace(fixer_util.Call(name, new_node))
 def transform_iter(self, method_name, node, base):
     """Call six.iteritems() and friends."""
     if method_name.startswith(u"view"):
         method_name = u"iter" + method_name[4:]
     libmodernize.touch_import(None, u"six", node)
     new_node = [n.clone() for n in base]
     new_node[0].prefix = u""
     name = fixer_util.Name(u"six." + method_name, prefix=node.prefix)
     node.replace(fixer_util.Call(name, new_node))
Example #14
0
    def transform(self, node, results):
        exc = results["exc"].clone()
        val = results["val"].clone()
        tb = results["tb"].clone()

        exc.prefix = u""
        val.prefix = tb.prefix = u" "

        touch_import(None, u'six', node)
        return Call(Name(u"six.reraise"), [exc, Comma(), val, Comma(), tb],
                    prefix=node.prefix)
Example #15
0
    def transform(self, node, results):
        exc = results["exc"].clone()
        val = results["val"].clone()
        tb = results["tb"].clone()

        exc.prefix = u""
        val.prefix = tb.prefix = u" "

        touch_import(None, u'six', node)
        return Call(Name(u"six.reraise"),
                    [exc, Comma(), val, Comma(), tb],
                    prefix=node.prefix)
Example #16
0
    def transform(self, node, results):
        if self.should_skip(node):
            return

        touch_import("six.moves", "input", node)
        name = results["name"]
        if name.value == "raw_input":
            name.replace(Name("input", prefix=name.prefix))
        else:
            new_node = node.clone()
            new_node.prefix = ""
            return Call(Name("eval"), [new_node], prefix=node.prefix)
Example #17
0
    def transform(self, node, results):
        if self.should_skip(node):
            return

        touch_import(u'six.moves', u'input', node)
        name = results['name']
        if name.value == 'raw_input':
            name.replace(Name('input', prefix=name.prefix))
        else:
            new_node = node.clone()
            new_node.prefix = ''
            return Call(Name('eval'), [new_node], prefix=node.prefix)
    def transform(self, node, results):
        imports = results["imports"]
        if imports.type == syms.import_as_name or not imports.children:
            children = [imports]
        else:
            children = imports.children
        for child in children[::2]:
            if child.type == token.NAME:
                name_node = child
            elif child.type == token.STAR:
                # Just leave the import as is.
                return
            else:
                assert child.type == syms.import_as_name
                name_node = child.children[0]
            member_name = name_node.value
            if member_name in (
                "imap",
                "izip",
                "ifilter",
                "ifilterfalse",
                "izip_longest",
            ):
                child.value = None
                libmodernize.touch_import("six.moves", member_name[1:], node)
                child.remove()

        # Make sure the import statement is still sane
        children = imports.children[:] or [imports]
        remove_comma = True
        for child in children:
            if remove_comma and child.type == token.COMMA:
                child.remove()
            else:
                remove_comma ^= True

        while children and children[-1].type == token.COMMA:
            children.pop().remove()

        # If there are no imports left, just get rid of the entire statement
        if (
            not (imports.children or getattr(imports, "value", None))
            or imports.parent is None
        ):
            p = node.prefix
            node = BlankLine()
            node.prefix = p
            return node
Example #19
0
    def transform(self, node, results):
        prefix = None
        func = results["func"][0]
        if "it" in results and func.value not in ("ifilterfalse", "izip_longest"):
            dot, it = (results["dot"], results["it"])
            # Remove the 'itertools'
            prefix = it.prefix
            it.remove()
            # Replace the node wich contains ('.', 'function') with the
            # function (to be consistant with the second part of the pattern)
            dot.remove()
            func.parent.replace(func)
            libmodernize.touch_import("six.moves", func.value[1:], node)

        prefix = prefix or func.prefix
        func.replace(Name(func.value[1:], prefix=prefix))
    def transform(self, node, results):
        prefix = None
        func = results['func'][0]
        if ('it' in results and
            func.value not in (u'ifilterfalse', u'izip_longest')):
            dot, it = (results['dot'], results['it'])
            # Remove the 'itertools'
            prefix = it.prefix
            it.remove()
            # Replace the node wich contains ('.', 'function') with the
            # function (to be consistant with the second part of the pattern)
            dot.remove()
            func.parent.replace(func)
            libmodernize.touch_import(u'six.moves', func.value[1:], node)

        prefix = prefix or func.prefix
        func.replace(Name(func.value[1:], prefix=prefix))
Example #21
0
    def transform(self, node, results):
        prefix = None
        func = results['func'][0]
        if ('it' in results
                and func.value not in (u'ifilterfalse', u'izip_longest')):
            dot, it = (results['dot'], results['it'])
            # Remove the 'itertools'
            prefix = it.prefix
            it.remove()
            # Replace the node wich contains ('.', 'function') with the
            # function (to be consistant with the second part of the pattern)
            dot.remove()
            func.parent.replace(func)
            libmodernize.touch_import(u'six.moves', func.value[1:], node)

        prefix = prefix or func.prefix
        func.replace(Name(func.value[1:], prefix=prefix))
 def transform(self, node, results):
     next_sibling = node.next_sibling
     if (next_sibling and next_sibling.type == token.EQUAL
             and next_sibling.value == "="):
         return
     prev_sibling = node.prev_sibling
     if prev_sibling and prev_sibling.type == token.NAME:
         if prev_sibling.value in ("del", "in"):
             return
     method = results["method"]
     method_node = method.parent
     if str(method_node.next_sibling) in (".clear", ".update"):
         return
     libmodernize.touch_import("scalyr_agent", "compat", node)
     head = results["head"]
     head.value = head.value.replace("os", "compat")
     method.value = "os_environ_unicode"
     node.changed()
    def transform(self, node, results):
        imports = results['imports']
        if imports.type == syms.import_as_name or not imports.children:
            children = [imports]
        else:
            children = imports.children
        for child in children[::2]:
            if child.type == token.NAME:
                member = child.value
                name_node = child
            elif child.type == token.STAR:
                # Just leave the import as is.
                return
            else:
                assert child.type == syms.import_as_name
                name_node = child.children[0]
            member_name = name_node.value
            if member_name in ('imap', 'izip', 'ifilter',
                               'ifilterfalse', 'izip_longest'):
                child.value = None
                libmodernize.touch_import(u'six.moves', member_name[1:], node)
                child.remove()

        # Make sure the import statement is still sane
        children = imports.children[:] or [imports]
        remove_comma = True
        for child in children:
            if remove_comma and child.type == token.COMMA:
                child.remove()
            else:
                remove_comma ^= True

        while children and children[-1].type == token.COMMA:
            children.pop().remove()

        # If there are no imports left, just get rid of the entire statement
        if (not (imports.children or getattr(imports, 'value', None)) or
            imports.parent is None):
            p = node.prefix
            node = BlankLine()
            node.prefix = p
            return node
Example #24
0
 def transform(self, node, results):
     if self.should_skip(node):
         return
     touch_import(u'six.moves', u'range',  node)
     return super(FixXrangeSix, self).transform(node, results)
Example #25
0
 def transform(self, node, results):
     if self.should_skip(node):
         return
     touch_import("six.moves", "range", node)
     return super().transform(node, results)
Example #26
0
 def transform(self, node, results):
     result = super().transform(node, results)
     # Always use six.moves.zip so that even Python 2.7 gets performance
     # boost from using itertools in iterator contexts.
     libmodernize.touch_import("six.moves", "zip", node)
     return result
 def transform(self, node, results):
     touch_import(None, 'six', node)
     pair = results['pair']
     pair.replace(fixer_util.Name('six.integer_types', prefix=pair.prefix))
Example #28
0
 def transform(self, node, results):
     libmodernize.touch_import(None, "six", node)
     return fixer_util.Name("six.string_types", prefix=node.prefix)
Example #29
0
 def transform(self, node, results):
     result = super(FixZip, self).transform(node, results)
     # Always use six.moves.zip so that even Python 2.7 gets performance
     # boost from using itertools in iterator contexts.
     libmodernize.touch_import(u'six.moves', u'zip', node)
     return result
 def transform(self, node, results):
     touch_import(None, 'six', node)
     pair = results['pair']
     pair.replace(fixer_util.Name('six.integer_types', prefix=pair.prefix))
Example #31
0
 def transform(self, node, results):
     libmodernize.touch_import(None, u'six', node)
     return fixer_util.Name(u'six.text_type', prefix=node.prefix)
 def transform(self, node, results):
     if self.should_skip(node):
         return
     if is_probably_builtin(node):
         libmodernize.touch_import(u'six', u'unichr', node)
Example #33
0
 def transform(self, node, results):
     libmodernize.touch_import("io", "open", node)
    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 #35
0
 def transform(self, node, results):
     libmodernize.touch_import(u'io', u'open', node)
Example #36
0
 def transform(self, node, results):
     if self.should_skip(node):
         return
     if is_probably_builtin(node):
         libmodernize.touch_import("six", "unichr", node)
Example #37
0
 def transform(self, node, results):
     touch_import(None, "six", node)
     pair = results["pair"]
     pair.replace(fixer_util.Name("six.integer_types", prefix=pair.prefix))
Example #38
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'))
 def transform(self, node, results):
     libmodernize.touch_import(None, u"six", node)
     return fixer_util.Name(u"six.string_types", prefix=node.prefix)