This repository has been archived by the owner on Feb 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
serve_diff.py
executable file
·65 lines (50 loc) · 1.96 KB
/
serve_diff.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
#!/usr/bin/env python
import SimpleHTTPServer
import SocketServer
import sys
import difflib
import json
import argparse
differ = difflib.HtmlDiff(tabsize=2, wrapcolumn=100)
def process_dict_item(k, v):
try:
return k, int(v)
except (TypeError, ValueError):
if k == 'EbsOptimized' and v == 'true':
return 'EbsOptimized', 1
if k == 'NoEcho' and v == 'true':
return 'NoEcho', 1
return k, replace_quoted_ints_in_values(v)
def replace_quoted_ints_in_values(o):
if isinstance(o, dict):
return dict(process_dict_item(k, v) for k,v in o.items())
elif isinstance(o, list):
return list(replace_quoted_ints_in_values(l) for l in o)
else:
return o
def normalize_file(f, replace_ints=True):
j = json.load(f)
if replace_ints:
j = replace_quoted_ints_in_values(j)
return json.dumps(j, indent=2, sort_keys=True)
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
replace_ints = not args.do_not_ignore_quoted_numbers
s1, s2 = [normalize_file(open(f), replace_ints).splitlines() for f in (args.file1, args.file2)]
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(differ.make_file(s1,s2))
return
if __name__ == '__main__':
SocketServer.TCPServer.allow_reuse_address = True
parser = argparse.ArgumentParser(description='Show differences between two json files, serve it per http.')
parser.add_argument('--port', '-p', type=int, default=8000)
parser.add_argument('--do-not-ignore-quoted-numbers', '-i', action='store_true',
help="Show boring changes like 500 -> \"500\"")
parser.add_argument('file1')
parser.add_argument('file2')
args = parser.parse_args()
print "serving on http://localhost:%d" % args.port
httpd = SocketServer.TCPServer(("", args.port), Handler)
httpd.serve_forever()