-
Notifications
You must be signed in to change notification settings - Fork 50
/
jsongadget.py
607 lines (500 loc) · 23.8 KB
/
jsongadget.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#!/usr/bin/env python
# Copyright (C) 2014 Open Data ("Open Data" refers to
# one or more of the following companies: Open Data Partners LLC,
# Open Data Research LLC, or Open Data Capital LLC.)
#
# This file is part of Hadrian.
#
# Licensed under the Hadrian Personal Use and Evaluation License (PUEL);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://raw.githubusercontent.com/opendatagroup/hadrian/master/LICENSE
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import json
import StringIO
import titus.producer.tools as t
from titus.inspector.defs import *
def depthGreaterThan(obj, target):
"""Helper function for determining if an object's depth is greater than a given target.
A string, number, boolean, or null has depth 0, a list or dict of such objects has depth 1, etc.
:type obj: Pythonized JSON
:param obj: object to inspect
:type target: non-negative integer
:param target: target depth
:rtype: bool
:return: ``True`` if the depth of ``obj`` is greater than ``target``; ``False`` otherwise.
"""
if isinstance(obj, dict):
return any(depthGreaterThan(x, target - 1) for x in obj.values()) or \
(len(obj) == 0 and target <= 0)
elif isinstance(obj, (list, tuple)):
return any(depthGreaterThan(x, target - 1) for x in obj) or \
(len(obj) == 0 and target <= 0)
else:
return target < 0
class LookCommand(Command):
"""The 'json look' command in pfainspector."""
def __init__(self, mode):
self.name = "look"
self.syntax = "look <name> [maxDepth=8] [indexWidth=30]"
self.help = "look at a named PFA document or subexpression in memory\n " + self.syntax
self.mode = mode
def complete(self, established, active):
"""Handle tab-complete for this command's arguments.
:type established: string
:param established: part of the text that has been established
:type active: string
:param active: part of the text to be completed
:rtype: list of strings
:return: potential completions
"""
options = ["maxDepth=", "indexWidth="]
words = getcomplete(established)
if len(words) == 0:
if active in self.mode.pfaFiles:
return [active + "["]
else:
return sorted(x for x in self.mode.pfaFiles if x.startswith(active))
elif len(words) == 1 and isinstance(words[0], parser.Extract) and words[0].partial:
if words[0].text in self.mode.pfaFiles:
return [x for x in extcomplete(self.mode.pfaFiles[words[0].text].obj, words[0].items) if x.startswith(active)]
else:
return []
elif not words[-1].partial:
return [x for x in options if x.startswith(active)]
else:
return []
def action(self, args):
"""Perform the action associated with this command.
:type args: list of titus.inspector.parser.Ast
:param args: arguments passed to the command
:rtype: ``None``
:return: nothing; results must be printed to the screen
"""
if len(args) == 1 and args[0] == parser.Word("help"):
print self.help
else:
options = {"maxDepth": 8, "indexWidth": 30}
while len(args) > 0 and isinstance(args[-1], parser.Option):
opt = args.pop()
if opt.word.text in ["maxDepth", "indexWidth"]:
try:
options[opt.word.text] = opt.value.value()
except TypeError:
raise InspectorError("illegal value for {0}".format(opt.word.text))
else:
raise InspectorError("option {0} unrecognized".format(opt.word.text))
if not isinstance(options["maxDepth"], (int, long)) or options["maxDepth"] <= 0:
raise InspectorError("maxDepth must be a positive integer")
if not isinstance(options["indexWidth"], (int, long)) or options["indexWidth"] <= 0:
raise InspectorError("indexWidth must be a positive integer")
if len(args) == 1 and isinstance(args[0], parser.Word):
if args[0].text not in self.mode.pfaFiles:
raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text))
node = self.mode.pfaFiles[args[0].text].obj
elif len(args) == 1 and isinstance(args[0], parser.Extract):
if args[0].text not in self.mode.pfaFiles:
raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text))
node = self.mode.pfaFiles[args[0].text].obj
items = args[0].items
node = extaction(args[0], node, items)
else:
self.syntaxError()
if not depthGreaterThan(node, 0):
print json.dumps(node)
else:
content = StringIO.StringIO()
if not depthGreaterThan(node, 1):
t.look(node, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=0, stream=content)
elif not depthGreaterThan(node, 2):
t.look(node, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=1, stream=content)
else:
t.look(node, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=2, stream=content)
content = content.getvalue()
if content.count("\n") <= 100:
print content
else:
proc = pipe("less")
try:
proc.stdin.write(content)
except IOError as err:
if str(err) != "[Errno 32] Broken pipe":
raise
pipewait(proc)
class CountCommand(Command):
"""The 'json count' command in pfainspector."""
def __init__(self, mode):
self.name = "count"
self.syntax = "count <name> <pattern>"
self.help = "count instances in a PFA document or subexpression that match a regular expression\n " + self.syntax
self.mode = mode
def complete(self, established, active):
"""Handle tab-complete for this command's arguments.
:type established: string
:param established: part of the text that has been established
:type active: string
:param active: part of the text to be completed
:rtype: list of strings
:return: potential completions
"""
words = getcomplete(established)
if len(words) == 0:
if active in self.mode.pfaFiles:
return [active + "["]
else:
return sorted(x for x in self.mode.pfaFiles.keys() if x.startswith(active))
elif len(words) == 1 and isinstance(words[0], parser.Extract) and words[0].partial:
if words[0].text in self.mode.pfaFiles:
return [x for x in extcomplete(self.mode.pfaFiles[words[0].text].obj, words[0].items) if x.startswith(active)]
else:
return []
else:
return []
def action(self, args):
"""Perform the action associated with this command.
:type args: list of titus.inspector.parser.Ast
:param args: arguments passed to the command
:rtype: ``None``
:return: nothing; results must be printed to the screen
"""
if len(args) == 1 and args[0] == parser.Word("help"):
print self.help
else:
if len(args) == 2 and isinstance(args[0], parser.Word):
if args[0].text not in self.mode.pfaFiles:
raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text))
node = self.mode.pfaFiles[args[0].text].obj
elif len(args) == 2 and isinstance(args[0], parser.Extract):
if args[0].text not in self.mode.pfaFiles:
raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text))
node = self.mode.pfaFiles[args[0].text].obj
items = args[0].items
node = extaction(args[0], node, items)
else:
self.syntaxError()
regex = args[-1].regex()
print "{0} matches".format(t.count(regex, node))
class IndexCommand(Command):
"""The 'json index' command in pfainspector."""
def __init__(self, mode):
self.name = "index"
self.syntax = "index <name> <pattern>"
self.help = "list indexes of a PFA document or subexpression that match a regular expression\n " + self.syntax
self.mode = mode
def complete(self, established, active):
"""Handle tab-complete for this command's arguments.
:type established: string
:param established: part of the text that has been established
:type active: string
:param active: part of the text to be completed
:rtype: list of strings
:return: potential completions
"""
words = getcomplete(established)
if len(words) == 0:
if active in self.mode.pfaFiles:
return [active + "["]
else:
return sorted(x for x in self.mode.pfaFiles.keys() if x.startswith(active))
elif len(words) == 1 and isinstance(words[0], parser.Extract) and words[0].partial:
if words[0].text in self.mode.pfaFiles:
return [x for x in extcomplete(self.mode.pfaFiles[words[0].text].obj, words[0].items) if x.startswith(active)]
else:
return []
else:
return []
def action(self, args):
"""Perform the action associated with this command.
:type args: list of titus.inspector.parser.Ast
:param args: arguments passed to the command
:rtype: ``None``
:return: nothing; results must be printed to the screen
"""
if len(args) == 1 and args[0] == parser.Word("help"):
print self.help
else:
if len(args) == 2 and isinstance(args[0], parser.Word):
if args[0].text not in self.mode.pfaFiles:
raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text))
node = self.mode.pfaFiles[args[0].text].obj
elif len(args) == 2 and isinstance(args[0], parser.Extract):
if args[0].text not in self.mode.pfaFiles:
raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text))
node = self.mode.pfaFiles[args[0].text].obj
items = args[0].items
node = extaction(args[0], node, items)
else:
self.syntaxError()
regex = args[-1].regex()
def display(i):
if isinstance(i, basestring):
if " " in i:
return json.dumps(i)
else:
return i
else:
return str(i)
print "Indexes that match the pattern:"
count = 0
for index in t.indexes(regex, node):
print " [" + ", ".join(display(i) for i in index) + "]"
count += 1
if count == 0:
print " (none)"
class FindCommand(Command):
"""The 'json find' command in pfainspector."""
def __init__(self, mode):
self.name = "find"
self.syntax = "find <name> <pattern> [maxDepth=3] [indexWidth=30]"
self.help = "show all matches of a regular expression in a PFA document or subexpression\n " + self.syntax
self.mode = mode
def complete(self, established, active):
"""Handle tab-complete for this command's arguments.
:type established: string
:param established: part of the text that has been established
:type active: string
:param active: part of the text to be completed
:rtype: list of strings
:return: potential completions
"""
options = ["maxDepth=", "indexWidth="]
words = getcomplete(established)
if len(words) == 0:
if active in self.mode.pfaFiles:
return [active + "["]
else:
return sorted(x for x in self.mode.pfaFiles if x.startswith(active))
elif len(words) == 1 and isinstance(words[0], parser.Extract) and words[0].partial:
if words[0].text in self.mode.pfaFiles:
return [x for x in extcomplete(self.mode.pfaFiles[words[0].text].obj, words[0].items) if x.startswith(active)]
else:
return []
elif not words[-1].partial:
return [x for x in options if x.startswith(active)]
else:
return []
def action(self, args):
"""Perform the action associated with this command.
:type args: list of titus.inspector.parser.Ast
:param args: arguments passed to the command
:rtype: ``None``
:return: nothing; results must be printed to the screen
"""
if len(args) == 1 and args[0] == parser.Word("help"):
print self.help
else:
options = {"maxDepth": 3, "indexWidth": 30}
while len(args) > 0 and isinstance(args[-1], parser.Option):
opt = args.pop()
if opt.word.text in ["maxDepth", "indexWidth"]:
try:
options[opt.word.text] = opt.value.value()
except TypeError:
raise InspectorError("illegal value for {0}".format(opt.word.text))
else:
raise InspectorError("option {1} unrecognized".format(opt.word.text))
if not isinstance(options["maxDepth"], (int, long)) or options["maxDepth"] <= 0:
raise InspectorError("maxDepth must be a positive integer")
if not isinstance(options["indexWidth"], (int, long)) or options["indexWidth"] <= 0:
raise InspectorError("indexWidth must be a positive integer")
if len(args) == 2 and isinstance(args[0], parser.Word):
if args[0].text not in self.mode.pfaFiles:
raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text))
node = self.mode.pfaFiles[args[0].text].obj
elif len(args) == 2 and isinstance(args[0], parser.Extract):
if args[0].text not in self.mode.pfaFiles:
raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text))
node = self.mode.pfaFiles[args[0].text].obj
items = args[0].items
node = extaction(args[0], node, items)
else:
self.syntaxError()
regex = args[-1].regex()
def display(i):
if isinstance(i, basestring):
if " " in i:
return json.dumps(i)
else:
return i
else:
return str(i)
content = StringIO.StringIO()
count = 0
for index in t.indexes(regex, node):
content.write("At index [" + ", ".join(display(i) for i in index) + "]:\n")
matched = t.get(node, index)
if not depthGreaterThan(matched, 0):
content.write(json.dumps(matched) + "\n")
elif not depthGreaterThan(matched, 1):
t.look(matched, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=0, stream=content)
elif not depthGreaterThan(matched, 2):
t.look(matched, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=1, stream=content)
else:
t.look(matched, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=2, stream=content)
content.write("\n")
count += 1
if count == 0:
print " (none)"
content = content.getvalue()
if content.count("\n") <= 100:
print content
else:
proc = pipe("less")
try:
proc.stdin.write(content)
except IOError as err:
if str(err) != "[Errno 32] Broken pipe":
raise
pipewait(proc)
class ChangeCommand(Command):
"""The 'json change' command in pfainspector."""
def __init__(self, mode):
self.name = "change"
self.syntax = "change <name> <pattern> to <replacement>"
self.help = "replace instances in a PFA document or subexpression that match a regular expression\n " + self.syntax
self.mode = mode
def complete(self, established, active):
"""Handle tab-complete for this command's arguments.
:type established: string
:param established: part of the text that has been established
:type active: string
:param active: part of the text to be completed
:rtype: list of strings
:return: potential completions
"""
words = getcomplete(established)
if len(words) == 0:
if active in self.mode.pfaFiles:
return [active + "["]
else:
return sorted(x for x in self.mode.pfaFiles.keys() if x.startswith(active))
elif len(words) == 1 and isinstance(words[0], parser.Extract) and words[0].partial:
if words[0].text in self.mode.pfaFiles:
return [x for x in extcomplete(self.mode.pfaFiles[words[0].text].obj, words[0].items) if x.startswith(active)]
else:
return []
elif len(words) == 2 and isinstance(words[0], (parser.Word, parser.Extract)) and words[0].text in self.mode.pfaFiles:
return ["to "]
else:
return []
def action(self, args):
"""Perform the action associated with this command.
:type args: list of titus.inspector.parser.Ast
:param args: arguments passed to the command
:rtype: ``None``
:return: nothing; results must be printed to the screen
"""
if len(args) == 1 and args[0] == parser.Word("help"):
print self.help
else:
if len(args) == 4 and isinstance(args[0], parser.Word):
if args[0].text not in self.mode.pfaFiles:
raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text))
model = self.mode.pfaFiles[args[0].text]
node = model.obj
safecopy = copy.deepcopy(node)
def rollback():
self.mode.pfaFiles[args[0].text].obj = safecopy
regex = args[1].regex()
replacement = args[3].replacement()
elif len(args) == 4 and isinstance(args[0], parser.Extract):
if args[0].text not in self.mode.pfaFiles:
raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text))
model = self.mode.pfaFiles[args[0].text]
node = model.obj
safecopy = copy.deepcopy(node)
def rollback():
self.mode.pfaFiles[args[0].text].obj = safecopy
items = args[0].items
node = extaction(args[0], node, items)
regex = args[1].regex()
replacement = args[3].replacement()
else:
self.syntaxError()
def display(i):
if isinstance(i, basestring):
if " " in i:
return json.dumps(i)
else:
return i
else:
return str(i)
def replace(value, groups):
if isinstance(value, parser.Replacement):
try:
return groups[value.name]
except KeyError:
raise InspectorError("group ({0}) not found in regular expression".format(value.name))
elif isinstance(value, dict):
return dict((k, replace(v, groups)) for k, v in value.items())
elif isinstance(value, (list, tuple)):
return [replace(x, groups) for x in value]
else:
return value
def removeAts(obj):
if isinstance(obj, dict):
return dict((k, removeAts(v)) for k, v in obj.items() if k != "@")
elif isinstance(obj, (list, tuple)):
return [removeAts(x) for x in obj]
else:
return obj
ask = True
self.mode.pause()
try:
for index, match in t.search(regex, node):
replacedReplacement = replace(replacement, match.groups)
if ask:
print "At index [" + ", ".join(display(i) for i in index) + "]:"
print "Original: " + json.dumps(removeAts(t.get(node, index)))
print "Change to: " + json.dumps(replacedReplacement)
action = None
while action is None:
response = raw_input("(Y/n/all/stop/revert): ")
normalized = response.strip().lower()
if normalized in ("", "y", "yes"):
action = "yes"
elif normalized in ("n", "no"):
action = "no"
elif normalized == "all":
action = "all"
elif normalized == "stop":
action = "stop"
elif normalized == "revert":
action = "revert"
print
else:
action = "yes"
if action == "yes":
t.assign(node, index, replacedReplacement)
elif action == "all":
t.assign(node, index, replacedReplacement)
ask = False
elif action == "stop":
break
elif action == "revert":
rollback()
break
except:
rollback()
self.mode.resume()
raise
else:
model.reset()
self.mode.resume()
class JsonGadget(Gadget):
"""The 'json' gadget in pfainspector."""
def __init__(self, mode):
self.commandGroup = CommandGroup("json", [
LookCommand(mode),
CountCommand(mode),
IndexCommand(mode),
FindCommand(mode),
ChangeCommand(mode)
])