1u"""
2Fixer for standard library imports renamed in Python 3
3"""
4
5from lib2to3 import fixer_base
6from lib2to3.fixer_util import Name, is_probably_builtin, Newline, does_tree_import
7from lib2to3.pygram import python_symbols as syms
8from lib2to3.pgen2 import token
9from lib2to3.pytree import Node, Leaf
10
11from libfuturize.fixer_util import touch_import_top
12# from ..fixer_util import NameImport
13
14# used in simple_mapping_to_pattern()
15MAPPING = {u"reprlib": u"repr",
16           u"winreg": u"_winreg",
17           u"configparser": u"ConfigParser",
18           u"copyreg": u"copy_reg",
19           u"queue": u"Queue",
20           u"socketserver": u"SocketServer",
21           u"_markupbase": u"markupbase",
22           u"test.support": u"test.test_support",
23           u"dbm.bsd": u"dbhash",
24           u"dbm.ndbm": u"dbm",
25           u"dbm.dumb": u"dumbdbm",
26           u"dbm.gnu": u"gdbm",
27           u"html.parser": u"HTMLParser",
28           u"html.entities": u"htmlentitydefs",
29           u"http.client": u"httplib",
30           u"http.cookies": u"Cookie",
31           u"http.cookiejar": u"cookielib",
32#          "tkinter": "Tkinter",
33           u"tkinter.dialog": u"Dialog",
34           u"tkinter._fix": u"FixTk",
35           u"tkinter.scrolledtext": u"ScrolledText",
36           u"tkinter.tix": u"Tix",
37           u"tkinter.constants": u"Tkconstants",
38           u"tkinter.dnd": u"Tkdnd",
39           u"tkinter.__init__": u"Tkinter",
40           u"tkinter.colorchooser": u"tkColorChooser",
41           u"tkinter.commondialog": u"tkCommonDialog",
42           u"tkinter.font": u"tkFont",
43           u"tkinter.ttk": u"ttk",
44           u"tkinter.messagebox": u"tkMessageBox",
45           u"tkinter.turtle": u"turtle",
46           u"urllib.robotparser": u"robotparser",
47           u"xmlrpc.client": u"xmlrpclib",
48           u"builtins": u"__builtin__",
49}
50
51# generic strings to help build patterns
52# these variables mean (with http.client.HTTPConnection as an example):
53# name = http
54# attr = client
55# used = HTTPConnection
56# fmt_name is a formatted subpattern (simple_name_match or dotted_name_match)
57
58# helps match 'queue', as in 'from queue import ...'
59simple_name_match = u"name='%s'"
60# helps match 'client', to be used if client has been imported from http
61subname_match = u"attr='%s'"
62# helps match 'http.client', as in 'import urllib.request'
63dotted_name_match = u"dotted_name=dotted_name< %s '.' %s >"
64# helps match 'queue', as in 'queue.Queue(...)'
65power_onename_match = u"%s"
66# helps match 'http.client', as in 'http.client.HTTPConnection(...)'
67power_twoname_match = u"power< %s trailer< '.' %s > any* >"
68# helps match 'client.HTTPConnection', if 'client' has been imported from http
69power_subname_match = u"power< %s any* >"
70# helps match 'from http.client import HTTPConnection'
71from_import_match = u"from_import=import_from< 'from' %s 'import' imported=any >"
72# helps match 'from http import client'
73from_import_submod_match = u"from_import_submod=import_from< 'from' %s 'import' (%s | import_as_name< %s 'as' renamed=any > | import_as_names< any* (%s | import_as_name< %s 'as' renamed=any >) any* > ) >"
74# helps match 'import urllib.request'
75name_import_match = u"name_import=import_name< 'import' %s > | name_import=import_name< 'import' dotted_as_name< %s 'as' renamed=any > >"
76# helps match 'import http.client, winreg'
77multiple_name_import_match = u"name_import=import_name< 'import' dotted_as_names< names=any* > >"
78
79def all_patterns(name):
80    u"""
81    Accepts a string and returns a pattern of possible patterns involving that name
82    Called by simple_mapping_to_pattern for each name in the mapping it receives.
83    """
84
85    # i_ denotes an import-like node
86    # u_ denotes a node that appears to be a usage of the name
87    if u'.' in name:
88        name, attr = name.split(u'.', 1)
89        simple_name = simple_name_match % (name)
90        simple_attr = subname_match % (attr)
91        dotted_name = dotted_name_match % (simple_name, simple_attr)
92        i_from = from_import_match % (dotted_name)
93        i_from_submod = from_import_submod_match % (simple_name, simple_attr, simple_attr, simple_attr, simple_attr)
94        i_name = name_import_match % (dotted_name, dotted_name)
95        u_name = power_twoname_match % (simple_name, simple_attr)
96        u_subname = power_subname_match % (simple_attr)
97        return u' | \n'.join((i_name, i_from, i_from_submod, u_name, u_subname))
98    else:
99        simple_name = simple_name_match % (name)
100        i_name = name_import_match % (simple_name, simple_name)
101        i_from = from_import_match % (simple_name)
102        u_name = power_onename_match % (simple_name)
103        return u' | \n'.join((i_name, i_from, u_name))
104
105
106class FixImports(fixer_base.BaseFix):
107
108    PATTERN = u' | \n'.join([all_patterns(name) for name in MAPPING])
109    PATTERN = u' | \n'.join((PATTERN, multiple_name_import_match))
110
111    def transform(self, node, results):
112        touch_import_top(u'future', u'standard_library', node)
113