forked from 9999years/pyworsener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworsener.py
executable file
·136 lines (111 loc) · 3.49 KB
/
worsener.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3.7
from typing import Callable, Optional
import token
from bowler import Query, LN, Capture, Filename, TOKEN, SYMBOL
from fissix.pygram import python_symbols
from fissix.pytree import Node, Leaf
from fissix.fixer_util import Call, Name, String, Comma, LParen, RParen, ArgList, Attr
ModifyFn = Callable[[LN, Capture, Filename], Optional[LN]]
def LBrace():
return Leaf(token.LBRACE, "[")
def RBrace():
return Leaf(token.RBRACE, "]")
def Plus():
return Leaf(token.PLUS, "+")
def Space():
return String(" ")
def TupleNode(*args):
parts = []
for arg in args:
if parts:
parts.append(Space())
arg = arg.clone()
arg.prefix = arg.prefix.lstrip()
parts.append(arg)
parts.append(Comma())
if len(parts) != 2:
parts.pop()
return Node(SYMBOL.atom, [LParen(), *parts, RParen()])
def ListNode(*args):
parts = []
for arg in args:
if parts:
parts.append(Space())
arg = arg.clone()
arg.prefix = arg.prefix.lstrip()
parts.append(arg)
parts.append(Comma())
parts.pop()
return Node(SYMBOL.atom, [LBrace(), *parts, RBrace()])
def modify_attr(node: LN, capture: Capture, filename: Filename) -> Optional[LN]:
node.replace(
Call(
Name("getattr"),
args=[
capture["obj"].clone(),
Comma(),
Space(),
String('"' + capture["attr"].value + '"'),
],
)
)
def filter_dict_literal(node: LN, capture: Capture, filename: Filename) -> bool:
return not any(it.type == SYMBOL.comp_for for it in capture.get("body"))
def modify_dict_literal(node: LN, capture: Capture, filename: Filename) -> Optional[LN]:
toks = iter(capture.get("body"))
items = []
prefix = ""
while True:
try:
tok = next(toks)
if tok.type == TOKEN.DOUBLESTAR:
body = next(toks).clone()
body.prefix = prefix + tok.prefix + body.prefix
items.append(body)
else:
colon = next(toks)
value = next(toks).clone()
value.prefix = colon.prefix + value.prefix
if items and isinstance(items[-1], list):
items[-1].append(TupleNode(tok, value))
else:
items.append([TupleNode(tok, value)])
comma = next(toks)
prefix = comma.prefix
except StopIteration:
break
listitems = []
for item in items:
if listitems:
listitems.extend([Space(), Plus(), Space()])
if isinstance(item, list):
listitems.append(ListNode(*item))
else:
call = Node(SYMBOL.test, [*Attr(item, Name("items")), ArgList([])])
listitems.append(call)
args = listitems
if len(listitems) > 1:
args = [Node(SYMBOL.arith_expr, args)]
args.append(String(node.children[-1].prefix))
return Call(Name("dict"), args, prefix=node.prefix)
def main():
(
Query("fake.py")
.select(
"""
power<
obj=NAME
trailer<
'.' attr=NAME
>
>
"""
)
.modify(modify_attr)
.select("""atom< "{" dictsetmaker< body=any* > "}" >""")
.filter(filter_dict_literal)
.modify(modify_dict_literal)
.diff()
)
if __name__ == "__main__":
main()