-
Notifications
You must be signed in to change notification settings - Fork 4
/
pprint.py
35 lines (28 loc) · 791 Bytes
/
pprint.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
import sys
import json
import xml.dom.minidom as xml
def _json(infile, outfile):
try:
obj = json.load(infile)
except ValueError as e:
raise SystemExit(e)
json.dump(obj, outfile, sort_keys=True, indent=4, separators=(',', ': '))
def _xml(infile, outfile):
try:
obj = xml.parse(infile)
except Exception as e:
raise SystemExit(e)
pretty_xml_as_string = obj.toprettyxml()
outfile.write(pretty_xml_as_string)
def main():
filetype = sys.argv[1]
infile = open(sys.argv[2], 'rb')
outfile = sys.stdout
if filetype in ['javascript']:
_json(infile, outfile)
elif filetype in ['html', 'xml']:
_xml(infile, outfile)
else:
outfile.write(infile.read())
if __name__ == '__main__':
main()