Skip to content

Commit ab4b7a8

Browse files
committed
Merge pull request #80 from brettcannon/fix_imports
Fix imports
2 parents d4db526 + 14b16f6 commit ab4b7a8

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

libmodernize/fixes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
'libmodernize.fixes.fix_basestring',
3131
'libmodernize.fixes.fix_dict_six',
3232
'libmodernize.fixes.fix_filter',
33+
'libmodernize.fixes.fix_imports_six',
3334
'libmodernize.fixes.fix_input_six',
3435
'libmodernize.fixes.fix_map',
3536
'libmodernize.fixes.fix_metaclass',

libmodernize/fixes/fix_imports_six.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from lib2to3.fixes import fix_imports
2+
3+
4+
class FixImportsSix(fix_imports.FixImports):
5+
6+
mapping = {
7+
'__builtin__': 'six.moves.builtins',
8+
'_winreg': 'six.moves.winreg',
9+
'BaseHTTPServer': 'six.moves.BaseHTTPServer',
10+
'CGIHTTPServer': 'six.moves.CGIHTTPServer',
11+
'ConfigParser': 'six.moves.configparser',
12+
'copy_reg': 'six.moves.copyreg',
13+
'Cookie': 'six.moves.http_cookies',
14+
'cookielib': 'six.moves.http_cookiejar',
15+
'cPickle': 'six.moves.cPickle',
16+
'Dialog': 'six.moves.tkinter_dialog',
17+
'dummy_thread': 'six.moves._dummy_thread',
18+
# cStringIO.StringIO()
19+
# email.MIMEBase
20+
# email.MIMEMultipart
21+
# email.MIMENonMultipart
22+
# email.MIMEText
23+
'FileDialog': 'six.moves.tkinter_filedialog',
24+
'gdbm': 'six.moves.dbm_gnu',
25+
'htmlentitydefs': 'six.moves.html_entities',
26+
'HTMLParser': 'six.moves.html_parser',
27+
'httplib': 'six.moves.http_client',
28+
# intern()
29+
# itertools.ifilter()
30+
# itertools.ifilterfalse()
31+
# itertools.imap()
32+
# itertools.izip()
33+
# itertools.zip_longest()
34+
# pipes.quote
35+
'Queue': 'six.moves.queue',
36+
# reduce()
37+
# reload()
38+
'repr': 'six.moves.reprlib',
39+
'robotparser': 'six.moves.urllib_robotparser',
40+
'ScrolledText': 'six.moves.tkinter_scrolledtext',
41+
'SimpleDialog': 'six.moves.tkinter_simpledialog',
42+
'SimpleHTTPServer': 'six.moves.SimpleHTTPServer',
43+
'SimpleXMLRPCServer': 'six.moves.xmlrpc_server',
44+
'SocketServer': 'six.moves.socketserver',
45+
'thread': 'six.moves._thread',
46+
'Tix': 'six.moves.tkinter_tix',
47+
'tkColorChooser': 'six.moves.tkinter_colorchooser',
48+
'tkCommonDialog': 'six.moves.tkinter_commondialog',
49+
'Tkconstants': 'six.moves.tkinter_constants',
50+
'Tkdnd': 'six.moves.tkinter_dnd',
51+
'tkFileDialog': 'six.moves.tkinter_filedialog',
52+
'tkFont': 'six.moves.tkinter_font',
53+
'Tkinter': 'six.moves.tkinter',
54+
'tkMessageBox': 'six.moves.tkinter_messagebox',
55+
'tkSimpleDialog': 'six.moves.tkinter_tksimpledialog',
56+
'ttk': 'six.moves.tkinter_ttk',
57+
# urllib
58+
# UserDict.UserDict
59+
# UserList.UserList
60+
# UserString.UserString
61+
'xmlrpclib': 'six.moves.xmlrpc_client',
62+
}

tests/test_fix_imports_six.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from __future__ import absolute_import
2+
3+
import sys
4+
5+
try:
6+
from six.moves import tkinter
7+
except ImportError:
8+
tkinter = None
9+
10+
from libmodernize.fixes import fix_imports_six
11+
12+
from utils import check_on_input
13+
14+
15+
MOVED_MODULE = ("""\
16+
import ConfigParser
17+
ConfigParser.ConfigParser()
18+
""", """\
19+
import six.moves.configparser
20+
six.moves.configparser.ConfigParser()
21+
""")
22+
23+
MOVED_MODULE_FROMLIST = ("""\
24+
from ConfigParser import ConfigParser
25+
ConfigParser()
26+
""", """\
27+
from six.moves.configparser import ConfigParser
28+
ConfigParser()
29+
""")
30+
31+
32+
def test_moved_module():
33+
check_on_input(*MOVED_MODULE)
34+
35+
def test_moved_module_fromlist():
36+
check_on_input(*MOVED_MODULE_FROMLIST)
37+
38+
def test_validate_mapping():
39+
for py2_name, six_name in fix_imports_six.FixImportsSix.mapping.items():
40+
try:
41+
__import__(py2_name)
42+
__import__(six_name)
43+
except ImportError as exc:
44+
if 'tkinter' in six_name and tkinter is not None:
45+
raise
46+
elif 'winreg' in six_name and sys.platform.startswith('win'):
47+
raise

tests/test_fixes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def check_existence(prefix, module_names):
1515
dotted_prefix = prefix + '.'
1616
for module_name in module_names:
1717
if not module_name.startswith(dotted_prefix):
18-
msg = '{0!r} does not start with {1!r}'.format(module_name, lib2to3_prefix)
18+
msg = '{0!r} does not start with {1!r}'.format(module_name, prefix)
1919
raise AssertionError(msg)
2020
try:
2121
__import__(module_name)

0 commit comments

Comments
 (0)