Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/libpasteurize/fixes/fix_imports2.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 u""" | |
2 Fixer for complicated imports | |
3 """ | |
4 | |
5 from lib2to3 import fixer_base | |
6 from lib2to3.fixer_util import Name, String, FromImport, Newline, Comma | |
7 from libfuturize.fixer_util import touch_import_top | |
8 | |
9 | |
10 TK_BASE_NAMES = (u'ACTIVE', u'ALL', u'ANCHOR', u'ARC',u'BASELINE', u'BEVEL', u'BOTH', | |
11 u'BOTTOM', u'BROWSE', u'BUTT', u'CASCADE', u'CENTER', u'CHAR', | |
12 u'CHECKBUTTON', u'CHORD', u'COMMAND', u'CURRENT', u'DISABLED', | |
13 u'DOTBOX', u'E', u'END', u'EW', u'EXCEPTION', u'EXTENDED', u'FALSE', | |
14 u'FIRST', u'FLAT', u'GROOVE', u'HIDDEN', u'HORIZONTAL', u'INSERT', | |
15 u'INSIDE', u'LAST', u'LEFT', u'MITER', u'MOVETO', u'MULTIPLE', u'N', | |
16 u'NE', u'NO', u'NONE', u'NORMAL', u'NS', u'NSEW', u'NUMERIC', u'NW', | |
17 u'OFF', u'ON', u'OUTSIDE', u'PAGES', u'PIESLICE', u'PROJECTING', | |
18 u'RADIOBUTTON', u'RAISED', u'READABLE', u'RIDGE', u'RIGHT', | |
19 u'ROUND', u'S', u'SCROLL', u'SE', u'SEL', u'SEL_FIRST', u'SEL_LAST', | |
20 u'SEPARATOR', u'SINGLE', u'SOLID', u'SUNKEN', u'SW', u'StringTypes', | |
21 u'TOP', u'TRUE', u'TclVersion', u'TkVersion', u'UNDERLINE', | |
22 u'UNITS', u'VERTICAL', u'W', u'WORD', u'WRITABLE', u'X', u'Y', u'YES', | |
23 u'wantobjects') | |
24 | |
25 PY2MODULES = { | |
26 u'urllib2' : ( | |
27 u'AbstractBasicAuthHandler', u'AbstractDigestAuthHandler', | |
28 u'AbstractHTTPHandler', u'BaseHandler', u'CacheFTPHandler', | |
29 u'FTPHandler', u'FileHandler', u'HTTPBasicAuthHandler', | |
30 u'HTTPCookieProcessor', u'HTTPDefaultErrorHandler', | |
31 u'HTTPDigestAuthHandler', u'HTTPError', u'HTTPErrorProcessor', | |
32 u'HTTPHandler', u'HTTPPasswordMgr', | |
33 u'HTTPPasswordMgrWithDefaultRealm', u'HTTPRedirectHandler', | |
34 u'HTTPSHandler', u'OpenerDirector', u'ProxyBasicAuthHandler', | |
35 u'ProxyDigestAuthHandler', u'ProxyHandler', u'Request', | |
36 u'StringIO', u'URLError', u'UnknownHandler', u'addinfourl', | |
37 u'build_opener', u'install_opener', u'parse_http_list', | |
38 u'parse_keqv_list', u'randombytes', u'request_host', u'urlopen'), | |
39 u'urllib' : ( | |
40 u'ContentTooShortError', u'FancyURLopener',u'URLopener', | |
41 u'basejoin', u'ftperrors', u'getproxies', | |
42 u'getproxies_environment', u'localhost', u'pathname2url', | |
43 u'quote', u'quote_plus', u'splitattr', u'splithost', | |
44 u'splitnport', u'splitpasswd', u'splitport', u'splitquery', | |
45 u'splittag', u'splittype', u'splituser', u'splitvalue', | |
46 u'thishost', u'unquote', u'unquote_plus', u'unwrap', | |
47 u'url2pathname', u'urlcleanup', u'urlencode', u'urlopen', | |
48 u'urlretrieve',), | |
49 u'urlparse' : ( | |
50 u'parse_qs', u'parse_qsl', u'urldefrag', u'urljoin', | |
51 u'urlparse', u'urlsplit', u'urlunparse', u'urlunsplit'), | |
52 u'dbm' : ( | |
53 u'ndbm', u'gnu', u'dumb'), | |
54 u'anydbm' : ( | |
55 u'error', u'open'), | |
56 u'whichdb' : ( | |
57 u'whichdb',), | |
58 u'BaseHTTPServer' : ( | |
59 u'BaseHTTPRequestHandler', u'HTTPServer'), | |
60 u'CGIHTTPServer' : ( | |
61 u'CGIHTTPRequestHandler',), | |
62 u'SimpleHTTPServer' : ( | |
63 u'SimpleHTTPRequestHandler',), | |
64 u'FileDialog' : TK_BASE_NAMES + ( | |
65 u'FileDialog', u'LoadFileDialog', u'SaveFileDialog', | |
66 u'dialogstates', u'test'), | |
67 u'tkFileDialog' : ( | |
68 u'Directory', u'Open', u'SaveAs', u'_Dialog', u'askdirectory', | |
69 u'askopenfile', u'askopenfilename', u'askopenfilenames', | |
70 u'askopenfiles', u'asksaveasfile', u'asksaveasfilename'), | |
71 u'SimpleDialog' : TK_BASE_NAMES + ( | |
72 u'SimpleDialog',), | |
73 u'tkSimpleDialog' : TK_BASE_NAMES + ( | |
74 u'askfloat', u'askinteger', u'askstring', u'Dialog'), | |
75 u'SimpleXMLRPCServer' : ( | |
76 u'CGIXMLRPCRequestHandler', u'SimpleXMLRPCDispatcher', | |
77 u'SimpleXMLRPCRequestHandler', u'SimpleXMLRPCServer', | |
78 u'list_public_methods', u'remove_duplicates', | |
79 u'resolve_dotted_attribute'), | |
80 u'DocXMLRPCServer' : ( | |
81 u'DocCGIXMLRPCRequestHandler', u'DocXMLRPCRequestHandler', | |
82 u'DocXMLRPCServer', u'ServerHTMLDoc',u'XMLRPCDocGenerator'), | |
83 } | |
84 | |
85 MAPPING = { u'urllib.request' : | |
86 (u'urllib2', u'urllib'), | |
87 u'urllib.error' : | |
88 (u'urllib2', u'urllib'), | |
89 u'urllib.parse' : | |
90 (u'urllib2', u'urllib', u'urlparse'), | |
91 u'dbm.__init__' : | |
92 (u'anydbm', u'whichdb'), | |
93 u'http.server' : | |
94 (u'CGIHTTPServer', u'SimpleHTTPServer', u'BaseHTTPServer'), | |
95 u'tkinter.filedialog' : | |
96 (u'tkFileDialog', u'FileDialog'), | |
97 u'tkinter.simpledialog' : | |
98 (u'tkSimpleDialog', u'SimpleDialog'), | |
99 u'xmlrpc.server' : | |
100 (u'DocXMLRPCServer', u'SimpleXMLRPCServer'), | |
101 } | |
102 | |
103 # helps match 'http', as in 'from http.server import ...' | |
104 simple_name = u"name='%s'" | |
105 # helps match 'server', as in 'from http.server import ...' | |
106 simple_attr = u"attr='%s'" | |
107 # helps match 'HTTPServer', as in 'from http.server import HTTPServer' | |
108 simple_using = u"using='%s'" | |
109 # helps match 'urllib.request', as in 'import urllib.request' | |
110 dotted_name = u"dotted_name=dotted_name< %s '.' %s >" | |
111 # helps match 'http.server', as in 'http.server.HTTPServer(...)' | |
112 power_twoname = u"pow=power< %s trailer< '.' %s > trailer< '.' using=any > any* >" | |
113 # helps match 'dbm.whichdb', as in 'dbm.whichdb(...)' | |
114 power_onename = u"pow=power< %s trailer< '.' using=any > any* >" | |
115 # helps match 'from http.server import HTTPServer' | |
116 # also helps match 'from http.server import HTTPServer, SimpleHTTPRequestHandler' | |
117 # also helps match 'from http.server import *' | |
118 from_import = u"from_import=import_from< 'from' %s 'import' (import_as_name< using=any 'as' renamed=any> | in_list=import_as_names< using=any* > | using='*' | using=NAME) >" | |
119 # helps match 'import urllib.request' | |
120 name_import = u"name_import=import_name< 'import' (%s | in_list=dotted_as_names< imp_list=any* >) >" | |
121 | |
122 ############# | |
123 # WON'T FIX # | |
124 ############# | |
125 | |
126 # helps match 'import urllib.request as name' | |
127 name_import_rename = u"name_import_rename=dotted_as_name< %s 'as' renamed=any >" | |
128 # helps match 'from http import server' | |
129 from_import_rename = u"from_import_rename=import_from< 'from' %s 'import' (%s | import_as_name< %s 'as' renamed=any > | in_list=import_as_names< any* (%s | import_as_name< %s 'as' renamed=any >) any* >) >" | |
130 | |
131 | |
132 def all_modules_subpattern(): | |
133 u""" | |
134 Builds a pattern for all toplevel names | |
135 (urllib, http, etc) | |
136 """ | |
137 names_dot_attrs = [mod.split(u".") for mod in MAPPING] | |
138 ret = u"( " + u" | ".join([dotted_name % (simple_name % (mod[0]), | |
139 simple_attr % (mod[1])) for mod in names_dot_attrs]) | |
140 ret += u" | " | |
141 ret += u" | ".join([simple_name % (mod[0]) for mod in names_dot_attrs if mod[1] == u"__init__"]) + u" )" | |
142 return ret | |
143 | |
144 | |
145 def build_import_pattern(mapping1, mapping2): | |
146 u""" | |
147 mapping1: A dict mapping py3k modules to all possible py2k replacements | |
148 mapping2: A dict mapping py2k modules to the things they do | |
149 This builds a HUGE pattern to match all ways that things can be imported | |
150 """ | |
151 # py3k: urllib.request, py2k: ('urllib2', 'urllib') | |
152 yield from_import % (all_modules_subpattern()) | |
153 for py3k, py2k in mapping1.items(): | |
154 name, attr = py3k.split(u'.') | |
155 s_name = simple_name % (name) | |
156 s_attr = simple_attr % (attr) | |
157 d_name = dotted_name % (s_name, s_attr) | |
158 yield name_import % (d_name) | |
159 yield power_twoname % (s_name, s_attr) | |
160 if attr == u'__init__': | |
161 yield name_import % (s_name) | |
162 yield power_onename % (s_name) | |
163 yield name_import_rename % (d_name) | |
164 yield from_import_rename % (s_name, s_attr, s_attr, s_attr, s_attr) | |
165 | |
166 | |
167 class FixImports2(fixer_base.BaseFix): | |
168 | |
169 run_order = 4 | |
170 | |
171 PATTERN = u" | \n".join(build_import_pattern(MAPPING, PY2MODULES)) | |
172 | |
173 def transform(self, node, results): | |
174 touch_import_top(u'future', u'standard_library', node) |