comparison planemo/lib/python3.7/site-packages/libpasteurize/fixes/fix_unpacking.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:
3 (a,)* *b (,c)* [,] = s
4 for (a,)* *b (,c)* [,] in d: ...
5 """
6
7 from lib2to3 import fixer_base
8 from itertools import count
9 from lib2to3.fixer_util import (Assign, Comma, Call, Newline, Name,
10 Number, token, syms, Node, Leaf)
11 from libfuturize.fixer_util import indentation, suitify, commatize
12 # from libfuturize.fixer_util import Assign, Comma, Call, Newline, Name, Number, indentation, suitify, commatize, token, syms, Node, Leaf
13
14 def assignment_source(num_pre, num_post, LISTNAME, ITERNAME):
15 u"""
16 Accepts num_pre and num_post, which are counts of values
17 before and after the starg (not including the starg)
18 Returns a source fit for Assign() from fixer_util
19 """
20 children = []
21 pre = unicode(num_pre)
22 post = unicode(num_post)
23 # This code builds the assignment source from lib2to3 tree primitives.
24 # It's not very readable, but it seems like the most correct way to do it.
25 if num_pre > 0:
26 pre_part = Node(syms.power, [Name(LISTNAME), Node(syms.trailer, [Leaf(token.LSQB, u"["), Node(syms.subscript, [Leaf(token.COLON, u":"), Number(pre)]), Leaf(token.RSQB, u"]")])])
27 children.append(pre_part)
28 children.append(Leaf(token.PLUS, u"+", prefix=u" "))
29 main_part = Node(syms.power, [Leaf(token.LSQB, u"[", prefix=u" "), Name(LISTNAME), Node(syms.trailer, [Leaf(token.LSQB, u"["), Node(syms.subscript, [Number(pre) if num_pre > 0 else Leaf(1, u""), Leaf(token.COLON, u":"), Node(syms.factor, [Leaf(token.MINUS, u"-"), Number(post)]) if num_post > 0 else Leaf(1, u"")]), Leaf(token.RSQB, u"]"), Leaf(token.RSQB, u"]")])])
30 children.append(main_part)
31 if num_post > 0:
32 children.append(Leaf(token.PLUS, u"+", prefix=u" "))
33 post_part = Node(syms.power, [Name(LISTNAME, prefix=u" "), Node(syms.trailer, [Leaf(token.LSQB, u"["), Node(syms.subscript, [Node(syms.factor, [Leaf(token.MINUS, u"-"), Number(post)]), Leaf(token.COLON, u":")]), Leaf(token.RSQB, u"]")])])
34 children.append(post_part)
35 source = Node(syms.arith_expr, children)
36 return source
37
38 class FixUnpacking(fixer_base.BaseFix):
39
40 PATTERN = u"""
41 expl=expr_stmt< testlist_star_expr<
42 pre=(any ',')*
43 star_expr< '*' name=NAME >
44 post=(',' any)* [','] > '=' source=any > |
45 impl=for_stmt< 'for' lst=exprlist<
46 pre=(any ',')*
47 star_expr< '*' name=NAME >
48 post=(',' any)* [','] > 'in' it=any ':' suite=any>"""
49
50 def fix_explicit_context(self, node, results):
51 pre, name, post, source = (results.get(n) for n in (u"pre", u"name", u"post", u"source"))
52 pre = [n.clone() for n in pre if n.type == token.NAME]
53 name.prefix = u" "
54 post = [n.clone() for n in post if n.type == token.NAME]
55 target = [n.clone() for n in commatize(pre + [name.clone()] + post)]
56 # to make the special-case fix for "*z, = ..." correct with the least
57 # amount of modification, make the left-side into a guaranteed tuple
58 target.append(Comma())
59 source.prefix = u""
60 setup_line = Assign(Name(self.LISTNAME), Call(Name(u"list"), [source.clone()]))
61 power_line = Assign(target, assignment_source(len(pre), len(post), self.LISTNAME, self.ITERNAME))
62 return setup_line, power_line
63
64 def fix_implicit_context(self, node, results):
65 u"""
66 Only example of the implicit context is
67 a for loop, so only fix that.
68 """
69 pre, name, post, it = (results.get(n) for n in (u"pre", u"name", u"post", u"it"))
70 pre = [n.clone() for n in pre if n.type == token.NAME]
71 name.prefix = u" "
72 post = [n.clone() for n in post if n.type == token.NAME]
73 target = [n.clone() for n in commatize(pre + [name.clone()] + post)]
74 # to make the special-case fix for "*z, = ..." correct with the least
75 # amount of modification, make the left-side into a guaranteed tuple
76 target.append(Comma())
77 source = it.clone()
78 source.prefix = u""
79 setup_line = Assign(Name(self.LISTNAME), Call(Name(u"list"), [Name(self.ITERNAME)]))
80 power_line = Assign(target, assignment_source(len(pre), len(post), self.LISTNAME, self.ITERNAME))
81 return setup_line, power_line
82
83 def transform(self, node, results):
84 u"""
85 a,b,c,d,e,f,*g,h,i = range(100) changes to
86 _3to2list = list(range(100))
87 a,b,c,d,e,f,g,h,i, = _3to2list[:6] + [_3to2list[6:-2]] + _3to2list[-2:]
88
89 and
90
91 for a,b,*c,d,e in iter_of_iters: do_stuff changes to
92 for _3to2iter in iter_of_iters:
93 _3to2list = list(_3to2iter)
94 a,b,c,d,e, = _3to2list[:2] + [_3to2list[2:-2]] + _3to2list[-2:]
95 do_stuff
96 """
97 self.LISTNAME = self.new_name(u"_3to2list")
98 self.ITERNAME = self.new_name(u"_3to2iter")
99 expl, impl = results.get(u"expl"), results.get(u"impl")
100 if expl is not None:
101 setup_line, power_line = self.fix_explicit_context(node, results)
102 setup_line.prefix = expl.prefix
103 power_line.prefix = indentation(expl.parent)
104 setup_line.append_child(Newline())
105 parent = node.parent
106 i = node.remove()
107 parent.insert_child(i, power_line)
108 parent.insert_child(i, setup_line)
109 elif impl is not None:
110 setup_line, power_line = self.fix_implicit_context(node, results)
111 suitify(node)
112 suite = [k for k in node.children if k.type == syms.suite][0]
113 setup_line.prefix = u""
114 power_line.prefix = suite.children[1].value
115 suite.children[2].prefix = indentation(suite.children[2])
116 suite.insert_child(2, Newline())
117 suite.insert_child(2, power_line)
118 suite.insert_child(2, Newline())
119 suite.insert_child(2, setup_line)
120 results.get(u"lst").replace(Name(self.ITERNAME, prefix=u" "))