comparison planemo/lib/python3.7/site-packages/libpasteurize/fixes/fix_imports.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 standard library imports renamed in Python 3
3 """
4
5 from lib2to3 import fixer_base
6 from lib2to3.fixer_util import Name, is_probably_builtin, Newline, does_tree_import
7 from lib2to3.pygram import python_symbols as syms
8 from lib2to3.pgen2 import token
9 from lib2to3.pytree import Node, Leaf
10
11 from libfuturize.fixer_util import touch_import_top
12 # from ..fixer_util import NameImport
13
14 # used in simple_mapping_to_pattern()
15 MAPPING = {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 ...'
59 simple_name_match = u"name='%s'"
60 # helps match 'client', to be used if client has been imported from http
61 subname_match = u"attr='%s'"
62 # helps match 'http.client', as in 'import urllib.request'
63 dotted_name_match = u"dotted_name=dotted_name< %s '.' %s >"
64 # helps match 'queue', as in 'queue.Queue(...)'
65 power_onename_match = u"%s"
66 # helps match 'http.client', as in 'http.client.HTTPConnection(...)'
67 power_twoname_match = u"power< %s trailer< '.' %s > any* >"
68 # helps match 'client.HTTPConnection', if 'client' has been imported from http
69 power_subname_match = u"power< %s any* >"
70 # helps match 'from http.client import HTTPConnection'
71 from_import_match = u"from_import=import_from< 'from' %s 'import' imported=any >"
72 # helps match 'from http import client'
73 from_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'
75 name_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'
77 multiple_name_import_match = u"name_import=import_name< 'import' dotted_as_names< names=any* > >"
78
79 def 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
106 class 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)