Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into fix_file_open
Browse files Browse the repository at this point in the history
  • Loading branch information
brettcannon committed Sep 19, 2014
2 parents 03ce194 + c3cd373 commit 79f1a37
Show file tree
Hide file tree
Showing 42 changed files with 158 additions and 27 deletions.
2 changes: 2 additions & 0 deletions __main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import

from libmodernize import main

if __name__ == '__main__':
Expand Down
16 changes: 12 additions & 4 deletions libmodernize/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from lib2to3.fixer_util import FromImport, Newline, find_root
from __future__ import absolute_import

from lib2to3 import fixer_util
from lib2to3.pytree import Leaf, Node
from lib2to3.pygram import python_symbols as syms
from lib2to3.pgen2 import token
Expand Down Expand Up @@ -41,7 +43,7 @@ def check_future_import(node):

def add_future(node, symbol):

root = find_root(node)
root = fixer_util.find_root(node)

for idx, node in enumerate(root.children):
if node.type == syms.simple_stmt and \
Expand All @@ -56,6 +58,12 @@ def add_future(node, symbol):
# already imported
return

import_ = FromImport('__future__', [Leaf(token.NAME, symbol, prefix=" ")])
children = [import_, Newline()]
import_ = fixer_util.FromImport('__future__',
[Leaf(token.NAME, symbol, prefix=" ")])
children = [import_, fixer_util.Newline()]
root.insert_child(idx, Node(syms.simple_stmt, children))


def touch_import(package, name, node):
add_future(node, 'absolute_import')
fixer_util.touch_import(package, name, node)
1 change: 0 additions & 1 deletion libmodernize/fixes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
'lib2to3.fixes.fix_funcattrs',
'lib2to3.fixes.fix_has_key',
'lib2to3.fixes.fix_idioms',
'lib2to3.fixes.fix_import',
'lib2to3.fixes.fix_methodattrs',
'lib2to3.fixes.fix_ne',
'lib2to3.fixes.fix_numliterals',
Expand Down
9 changes: 6 additions & 3 deletions libmodernize/fixes/fix_basestring.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from __future__ import absolute_import

from lib2to3 import fixer_base
from lib2to3.fixer_util import touch_import, Name
from lib2to3 import fixer_util
import libmodernize


class FixBasestring(fixer_base.BaseFix):
BM_compatible = True
PATTERN = """'basestring'"""

def transform(self, node, results):
touch_import(None, u'six', node)
return Name(u'six.string_types', prefix=node.prefix)
libmodernize.touch_import(None, u'six', node)
return fixer_util.Name(u'six.string_types', prefix=node.prefix)
4 changes: 3 additions & 1 deletion libmodernize/fixes/fix_dict_six.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Fixer for iterkeys() -> six.iterkeys(), and similarly for iteritems and itervalues."""
from __future__ import absolute_import

# Local imports
from lib2to3 import fixer_base
from lib2to3 import fixer_util
from lib2to3 import patcomp
from lib2to3.fixes import fix_dict
import libmodernize


class FixDictSix(fix_dict.FixDict):
Expand All @@ -13,7 +15,7 @@ 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:]
fixer_util.touch_import(None, u'six', node)
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)
Expand Down
4 changes: 3 additions & 1 deletion libmodernize/fixes/fix_file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import

from lib2to3 import fixer_base
from lib2to3.fixer_util import Name

Expand All @@ -6,7 +8,7 @@ class FixFile(fixer_base.BaseFix):

BM_compatible = True
order = 'pre'

PATTERN = """
power< name='file'
trailer<
Expand Down
4 changes: 3 additions & 1 deletion libmodernize/fixes/fix_filter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright 2008 Armin Ronacher.
# Licensed to PSF under a Contributor Agreement.
from __future__ import absolute_import

from lib2to3 import fixer_base
from lib2to3.fixer_util import Call, Name, touch_import, in_special_context
from lib2to3.fixer_util import Call, Name, in_special_context
from libmodernize import touch_import


class FixFilter(fixer_base.ConditionalFix):
Expand Down
18 changes: 18 additions & 0 deletions libmodernize/fixes/fix_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import absolute_import

from lib2to3.fixes import fix_import
import libmodernize


class FixImport(fix_import.FixImport):

# Make sure this runs before any other fixer to guarantee that any other
# added absolute_import doesn't block this fixer's execution.
run_order = 1

def transform(self, node, results):
results = super(FixImport, self).transform(node, results)
if results is None:
return
libmodernize.add_future(node, 'absolute_import')
return results
4 changes: 3 additions & 1 deletion libmodernize/fixes/fix_input_six.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#
# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
# 2011, 2012, 2013, 2014 Python Software Foundation. All rights reserved.
from __future__ import absolute_import

from lib2to3 import fixer_base
from lib2to3.fixer_util import Call, Name, touch_import
from lib2to3.fixer_util import Call, Name
from libmodernize import touch_import


class FixInputSix(fixer_base.ConditionalFix):
Expand Down
4 changes: 3 additions & 1 deletion libmodernize/fixes/fix_map.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright 2008 Armin Ronacher.
# Licensed to PSF under a Contributor Agreement.
from __future__ import absolute_import

from lib2to3 import fixer_base
from lib2to3.fixer_util import Call, Name, touch_import, in_special_context
from lib2to3.fixer_util import Call, Name, in_special_context
from libmodernize import touch_import


class FixMap(fixer_base.ConditionalFix):
Expand Down
4 changes: 3 additions & 1 deletion libmodernize/fixes/fix_metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
# 2011, 2012, 2013 Python Software Foundation. All rights reserved.

# Author: Jack Diederich, Daniel Neuhäuser
from __future__ import absolute_import

# Local imports
from lib2to3 import fixer_base
from lib2to3.pygram import token
from lib2to3.fixer_util import Name, syms, Node, Leaf, touch_import, Call, \
from lib2to3.fixer_util import Name, syms, Node, Leaf, Call, \
String, Comma, parenthesize
from libmodernize import touch_import


def has_metaclass(parent):
Expand Down
3 changes: 2 additions & 1 deletion libmodernize/fixes/fix_next.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Fixer for it.next() -> advance_iterator(it)"""
from __future__ import absolute_import

# Local imports
from lib2to3 import fixer_base
from lib2to3.fixer_util import touch_import, Name, Call
from lib2to3.fixer_util import Name, Call

bind_warning = "Calls to builtin next() possibly shadowed by global binding"

Expand Down
5 changes: 4 additions & 1 deletion libmodernize/fixes/fix_open.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import absolute_import

from lib2to3 import fixer_base
from lib2to3.fixer_util import touch_import
import libmodernize


class FixOpen(fixer_base.BaseFix):
Expand All @@ -13,3 +15,4 @@ class FixOpen(fixer_base.BaseFix):

def transform(self, node, results):
touch_import(u'io', u'open', node)

1 change: 1 addition & 0 deletions libmodernize/fixes/fix_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
No changes are applied if print_function is imported from __future__
"""
from __future__ import absolute_import

# Local imports
from lib2to3 import patcomp, pytree, fixer_base
Expand Down
1 change: 1 addition & 0 deletions libmodernize/fixes/fix_raise.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
this.
"""
# Author: Collin Winter, Armin Ronacher
from __future__ import absolute_import

# Local imports
from lib2to3 import pytree, fixer_base
Expand Down
4 changes: 3 additions & 1 deletion libmodernize/fixes/fix_raise_six.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"""
# Author : Markus Unterwaditzer
from __future__ import absolute_import

# Local imports
from lib2to3 import fixer_base
from lib2to3.fixer_util import Name, Call, Comma, touch_import
from lib2to3.fixer_util import Name, Call, Comma
from libmodernize import touch_import

class FixRaiseSix(fixer_base.BaseFix):

Expand Down
5 changes: 4 additions & 1 deletion libmodernize/fixes/fix_unicode.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from __future__ import absolute_import

import re
from lib2to3.pgen2 import token
from lib2to3 import fixer_base
from lib2to3.fixer_util import touch_import, Name, Call
from lib2to3.fixer_util import Name, Call
from libmodernize import touch_import

_mapping = {u"unichr" : u"chr", u"unicode" : u"str"}
_literal_re = re.compile(u"[uU][rR]?[\\'\\\"]")
Expand Down
2 changes: 2 additions & 0 deletions libmodernize/fixes/fix_unicode_future.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import

from lib2to3.fixes import fix_unicode
from libmodernize import add_future

Expand Down
10 changes: 6 additions & 4 deletions libmodernize/fixes/fix_unicode_type.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from lib2to3 import fixer_base
from lib2to3.fixer_util import touch_import, Name
from __future__ import absolute_import

from lib2to3 import fixer_base
from lib2to3 import fixer_util
import libmodernize

class FixUnicodeType(fixer_base.BaseFix):
BM_compatible = True
PATTERN = """'unicode'"""

def transform(self, node, results):
touch_import(None, u'six', node)
return Name(u'six.text_type', prefix=node.prefix)
libmodernize.touch_import(None, u'six', node)
return fixer_util.Name(u'six.text_type', prefix=node.prefix)
4 changes: 3 additions & 1 deletion libmodernize/fixes/fix_xrange_six.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Copyright 2008 Armin Ronacher.
# Licensed to PSF under a Contributor Agreement.
from __future__ import absolute_import

from lib2to3 import fixer_base
from lib2to3.fixer_util import Call, Name, touch_import
from lib2to3.fixer_util import Call, Name
from lib2to3.fixes import fix_xrange
from libmodernize import touch_import


class FixXrangeSix(fixer_base.ConditionalFix, fix_xrange.FixXrange):
Expand Down
4 changes: 3 additions & 1 deletion libmodernize/fixes/fix_zip.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright 2008 Armin Ronacher.
# Licensed to PSF under a Contributor Agreement.
from __future__ import absolute_import

from lib2to3 import fixer_base
from lib2to3.fixer_util import Name, Call, touch_import, in_special_context
from lib2to3.fixer_util import Name, Call, in_special_context
from libmodernize import touch_import


class FixZip(fixer_base.ConditionalFix):
Expand Down
2 changes: 1 addition & 1 deletion libmodernize/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import print_function
from __future__ import absolute_import, print_function

import sys
import logging
Expand Down
2 changes: 2 additions & 0 deletions modernize.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from __future__ import absolute_import

from libmodernize.main import main
main()
3 changes: 3 additions & 0 deletions tests/test_fix_basestring.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import absolute_import

from utils import check_on_input


BASESTRING = ("""\
isinstance(x, basestring)
""", """\
from __future__ import absolute_import
import six
isinstance(x, six.string_types)
""")
Expand Down
4 changes: 4 additions & 0 deletions tests/test_fix_dict_six.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import

from utils import check_on_input


Expand All @@ -6,13 +8,15 @@
DICT_ITER = ("""\
x.iter{type}()
""", """\
from __future__ import absolute_import
import six
six.iter{type}(x)
""")

DICT_VIEW = ("""\
x.view{type}()
""", """\
from __future__ import absolute_import
import six
six.iter{type}(x)
""")
Expand Down
2 changes: 2 additions & 0 deletions tests/test_fix_file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import

from utils import check_on_input


Expand Down
4 changes: 4 additions & 0 deletions tests/test_fix_filter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import absolute_import

from utils import check_on_input


FILTER_CALL = ("""\
filter(None, [1])
""", """\
from __future__ import absolute_import
from six.moves import filter
list(filter(None, [1]))
""")
Expand All @@ -30,6 +33,7 @@
for a in filter(None, [1]):
pass
""", """\
from __future__ import absolute_import
from six.moves import filter
for a in filter(None, [1]):
pass
Expand Down
Loading

0 comments on commit 79f1a37

Please sign in to comment.