-
Notifications
You must be signed in to change notification settings - Fork 0
/
reap.py
executable file
·153 lines (126 loc) · 3.91 KB
/
reap.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
import ast, io, contextlib, argparse
import sys, math, string, time, random, re, itertools, os, logging, traceback, collections
marker = '#\xA0'
line_re = re.compile(
r'''
^
(?P<code>.*?)
(?P<space>[ \t]*)
(?P<annotation>([#]\xA0.*)?)
(?P<end>\n|\r\n|)
$
''',
re.VERBOSE,
)
values = {}
def positive(value):
i = int(value)
if i < 1:
raise argparse.ArgumentTypeError('value must be positive')
return i
parser = argparse.ArgumentParser()
parser.add_argument(
'--no-annotations',
help='turn off value annotations',
action='store_true',
)
parser.add_argument(
'--no-output',
help='do not append output written to stdout',
action='store_true',
)
parser.add_argument(
'--history',
help='number of values to include in annotations',
type=positive,
default=1,
)
parser.add_argument(
'--verbose',
help='bloviate',
action='store_true',
)
args = parser.parse_args()
output_sink = io.StringIO()
if args.no_output:
stdout_sink = io.StringIO()
else:
stdout_sink = output_sink
def tap(value, line, offset):
if value != None:
offsets = values.setdefault(line, {})
history = offsets.setdefault(offset, [])
history.append(value)
return value
class Tap(ast.NodeTransformer):
def rewrite(self, value, lineno, col_offset):
return ast.Call(
func=ast.Name('tap', ctx=ast.Load()),
args=[self.generic_visit(value), ast.Constant(value=lineno - 1), ast.Constant(value=col_offset)],
keywords=[],
)
def visit_Assign(self, original):
replacement = self.rewrite(original.value, original.lineno, original.col_offset)
return ast.Assign(targets=original.targets, value=replacement)
def visit_Expr(self, original):
replacement = self.rewrite(original.value, original.lineno, original.col_offset)
return ast.Expr(value=replacement)
def visit_Return(self, original):
replacement = self.rewrite(original.value, original.lineno, original.col_offset)
return ast.Return(value=replacement)
text = sys.stdin.read()
with contextlib.redirect_stdout(stdout_sink):
with contextlib.redirect_stderr(output_sink):
try:
module = ast.parse(text, filename='<input>', mode='exec')
rewrite = Tap().visit(module)
fixed = ast.fix_missing_locations(rewrite)
code = compile(rewrite, filename='<input>', mode='exec')
exec(code)
except:
print(traceback.format_exc())
class Line:
def __init__(self, index, code, space, annotation, end, values):
self.index = index
self.code = code
self.space = space
self.annotation = annotation
self.end = end
self.values = values
def __repr__(self):
return f'{self.index}: {repr(self.code)} {repr(self.space)} {repr(self.annotation)} {repr(self.end)}, {repr(self.values)}'
def width(self):
return len(self.code)
def format(self, width):
if args.no_annotations:
return f'{self.code}{self.end}'
else:
annotation = '; '.join([
', '.join([repr(value) for value in history[-args.history:]])
for offset, history in sorted(self.values.items())
])
return f'{self.code: <{width}} {marker}{annotation}{self.end}'
lines = list(
Line(index=i, values=values.get(i, {}), **line_re.match(line).groupdict())
for i, line
in enumerate(text.splitlines(True))
)
if args.verbose:
for line in lines:
sys.stderr.write(f'{line.index}: ')
sys.stderr.write(f'{repr(line.code)}, ')
sys.stderr.write(f'{repr(line.space)}, ')
sys.stderr.write(f'{repr(line.annotation)}, ')
sys.stderr.write(f'{repr(line.end)}, ')
sys.stderr.write(os.linesep)
output = output_sink.getvalue()
width = max((line.width() for line in lines), default=0)
for line in lines:
sys.stdout.write(line.format(width))
if output and lines and not lines[-1].end:
sys.stdout.write(os.linesep)
for line in output.splitlines(True):
if line.rstrip():
sys.stdout.write(marker)
sys.stdout.write(line)