-
Notifications
You must be signed in to change notification settings - Fork 29
/
sexp2json.py
executable file
·44 lines (34 loc) · 1.13 KB
/
sexp2json.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
#!/usr/bin/env python
"""
Convert S-expressions in files to JSONs.
"""
import sys
import json
import sexpdata
def tojsonable(obj):
if isinstance(obj, sexpdata.SExpBase):
return tojsonable(obj.value())
if isinstance(obj, list):
return map(tojsonable, obj)
return obj
def sexp2json(file, out, recursionlimit):
sys.setrecursionlimit(recursionlimit)
for path in file:
with open(path) as f:
json.dump(tojsonable(sexpdata.parse(f.read())), out, indent=2)
def main(args=None):
import argparse
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description=__doc__)
parser.add_argument('file', nargs='*')
parser.add_argument('--out', '-o', type=argparse.FileType('wt'),
default='-',
help='Output file.',)
parser.add_argument('--recursionlimit', '-l', type=int,
default=sys.getrecursionlimit(),
help='Set Python recursion limit.')
ns = parser.parse_args(args)
sexp2json(**vars(ns))
if __name__ == '__main__':
main()