-
Notifications
You must be signed in to change notification settings - Fork 9
/
pyfil.py
executable file
·395 lines (316 loc) · 10.1 KB
/
pyfil.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python3
"""
Use python as a filter on stdin. If the expression iterator, print
each item on its own line. If the value is a builtin container type,
attempt to serialize it as json before printing.
pyfil automatically imports any modules used in expressions.
If you'd like to create any other objects to use in the execution
environment ~/.config/pyfil-env.py and put things in it.
default objects:
l = []
d = {}
These are empty containers you might wish to add items to during
iteration, for example.
x is always the return value of the previous expression unless --exec.
The execution environment also has a special object for stdin,
creatively named "stdin". This differs from sys.stdin in that it
removes trailing newlines when you iterate over it, and it has
a property, stdin.l, which returns a list of the lines, rather than an
iterator.
Certain other flags; --loop (or anything that implies --loop), --json,
--split or --field_sep; may create additional objects. Check the flag
descriptions for further details.
Home: https://github.com/ninjaaron/pyfil
"""
import builtins
import collections
import sys
import json
import os
import re
import ast
import argparse
from functools import update_wrapper, partial
from typing import Iterable, Callable, Iterator
EXIT_STATUS = 0
class LazyDict(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setattr__
__delattr__ = dict.__delitem__ # type: ignore
class reify:
""""stolen" from Pylons"""
def __init__(self, wrapped):
self.wrapped = wrapped
update_wrapper(self, wrapped)
def __get__(self, inst, objtype=None):
if inst is None:
return self
val = self.wrapped(inst)
setattr(inst, self.wrapped.__name__, val)
return val
class NameSpace(dict):
"""namespace that imports modules lazily."""
def __missing__(self, name):
try:
return __import__(name)
except ImportError:
raise NameError("name '{}' is not defined".format(name))
class StdIn:
"""class for wrapping sys.stdin"""
def __init__(self):
self.lines = (line.rstrip("\n") for line in sys.stdin)
def __iter__(self):
return self.lines
@reify
def l(self): # noqa: E743, E741
return sys.stdin.read().splitlines()
def __next__(self):
return next(self.lines)
def __getattr__(self, name):
return getattr(sys.stdin, name)
class SafeList(collections.UserList):
"class for getting fields from stdin without raising errors"
def __getitem__(self, index):
try:
return self.data[index]
except IndexError:
return ""
def __iter__(self):
return iter(self.data)
def handle_errors(e: Exception, args):
"""stupid simple error handling"""
if args.raise_errors:
raise e
elif args.silence_errors:
pass
else:
global EXIT_STATUS
EXIT_STATUS = 1
print(
"\x1b[31m{}\x1b[0m:".format(e.__class__.__name__),
e,
file=sys.stderr,
)
class SafeListEncode(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, SafeList):
return obj.data
return json.JSONEncoder.default(self, obj)
def print_obj(obj, indent=None):
"""print strings, serialize other stuff to json, or don't"""
if isinstance(obj, str):
print(obj)
else:
try:
print(
json.dumps(
obj, ensure_ascii=False, indent=indent, cls=SafeListEncode
)
)
except TypeError:
print(obj)
def parse_handler(handler: str):
exn, expr = map(str.strip, handler.split(":", maxsplit=1))
return getattr(builtins, exn), expr
def run_with_exception_handler(
func: Callable, exception, handler: str, expr: str,
):
try:
return func(expr)
except exception:
return func(handler)
def run_expressions(runner, expressions, namespace, args):
value = None
for expr in expressions:
try:
value = runner(expr)
except Exception as e:
handle_errors(e, args)
continue
if not args.exec:
namespace.update(x=value)
return value
def display_value(value, args):
if args.join is not None and isinstance(value, Iterable):
joiner = "'''" + args.join.replace("'", r"\'") + "'''"
print(ast.literal_eval(joiner).join(map(str, value)))
elif value is None:
pass
elif isinstance(value, Iterator):
for i in value:
print_obj(i)
else:
indent = None if (args.loop or args.force_oneline_json) else 2
print_obj(value, indent)
def run(expressions: Iterable[str], args, namespace, run_expression: Callable):
value = run_expressions(run_expression, expressions, namespace, args)
if not (args.quiet or args.exec):
display_value(value, args)
def get_args(arguments=None):
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
add = parser.add_argument
add(
"expression",
nargs="+",
help="expression(s) to be "
"executed. If multiple expression arguments are given, "
"and --exec is not used, the value of the previous "
"expression is available as 'x' in the following "
"expression. if --exec is used, all assignment must be "
"explicit.",
)
add(
"-l",
"--loop",
action="store_true",
help="for n, i in enumerate(stdin): expressions",
)
add(
"-x",
"--exec",
action="store_true",
help="use exec instead of eval. statements are allowed, "
"but automatic printing is lost. doesn't affect --post",
)
add(
"-q",
"--quiet",
action="store_true",
help="suppress automatic printing. doesn't affect --post",
)
add(
"-j",
"--json",
action="store_true",
help="load stdin as json into object 'j'; If used with "
"--loop, treat each line of stdin as a new object",
)
add(
"-J",
"--real-dict-json",
action="store_true",
help="like -j, but creates real dictionaries instead of "
"the wrapper that allows dot syntax.",
)
add(
"-o",
"--force-oneline-json",
action="store_true",
help="outside of loops and iterators, objects serialzed "
"to json print with two-space indent. this forces "
"this forces all json objects to print on a single "
"line.",
)
add(
"-b",
"--pre",
help="statement to evaluate before expression args. "
"multiple statements may be combined with ';'. "
"no automatic printing",
)
add(
"-e",
"--post",
help="expression to evaluate after the loop. always "
"handeled by eval, even if --exec, and always prints "
"return value, even if --quiet. implies --loop",
)
add(
"-s",
"--split",
action="store_true",
help="split lines from stdin on whitespace into list 'f'. implies --loop",
)
add(
"-F",
"--field-sep",
metavar="PATTERN",
help="regex used to split lines from stdin into list 'f'. implies --loop",
)
add(
"-n",
"--join",
metavar="STRING",
help="join items in iterables with STRING",
)
add(
"-R",
"--raise-errors",
action="store_true",
help="raise errors in evaluation and stop execution "
"(default: print message to stderr and continue)",
)
add(
"-S",
"--silence-errors",
action="store_true",
help="suppress error messages",
)
add(
"-H",
"--exception-handler",
help="specify exception handler with the format "
"'Exception: alternative expression to eval'",
)
return parser.parse_args(arguments)
def main():
args = get_args()
func = "exec" if args.exec else "eval"
expressions = [
compile(e if args.exec else "(%s)" % e, "<string>", func)
for e in args.expression
]
user_env = os.environ["HOME"] + "/.config/pyfil-env.py"
namespace = NameSpace(vars(builtins))
namespace.update(stdin=StdIn(), l=[], d={})
if os.path.exists(user_env):
exec(open(user_env).read(), namespace)
if args.json:
jdecode = json.JSONDecoder(object_hook=LazyDict).decode
elif args.real_dict_json:
jdecode = json.loads
args.json = True
if args.post or args.split or args.field_sep:
args.loop = True
_evaluate = exec if args.exec else eval
evaluate = lambda expr: _evaluate(expr, namespace)
if args.exception_handler:
exception, handler = parse_handler(args.exception_handler)
run_expression = partial(
run_with_exception_handler, evaluate, exception, handler
)
else:
run_expression = evaluate
if args.loop:
if args.pre:
exec(args.pre, namespace)
for n, i in enumerate(map(str.rstrip, sys.stdin)):
namespace.update(i=i, n=n)
if args.json:
namespace.update(j=jdecode(i))
if args.field_sep:
if len(args.field_sep) == 1:
f = SafeList(i.split(args.field_sep))
else:
f = SafeList(re.split(args.field_sep, i))
namespace.update(f=f)
elif args.split:
namespace.update(f=SafeList(i.split()))
run(expressions, args, namespace, run_expression)
if args.post:
if args.quiet or args.exec:
args.loop, args.quiet, args.exec = None, None, None
_evaluate = eval
run(("(%s)" % args.post,), args, namespace, run_expression)
else:
if args.pre:
exec(args.pre, namespace)
if args.json:
namespace.update(j=jdecode(sys.stdin.read()))
run(expressions, args, namespace, run_expression)
sys.exit(EXIT_STATUS)
if __name__ == "__main__":
main()