-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoml_test.py
executable file
·40 lines (36 loc) · 1.14 KB
/
toml_test.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
#!/usr/bin/env python2
import datetime
import json
import sys
import toml_ply
def tag(value):
if isinstance(value, dict):
d = { }
for k, v in value.iteritems():
d[k] = tag(v)
return d
elif isinstance(value, list):
a = []
for v in value:
a.append(tag(v))
return {'type': 'array', 'value': a}
elif isinstance(value, basestring):
return {'type': 'string', 'value': str(value)}
elif isinstance(value, bool):
return {'type': 'bool', 'value': str(value).lower()}
elif isinstance(value, int):
return {'type': 'integer', 'value': str(value)}
elif isinstance(value, float):
return {'type': 'float', 'value': repr(value)}
elif isinstance(value, datetime.datetime):
sdate = value.strftime('%Y-%m-%dT%H:%M:%SZ')
return {'type': 'datetime', 'value': sdate}
assert False, 'Unknown type: %s' % type(value)
if __name__ == '__main__':
try:
tdata = toml_ply.loads(sys.stdin.read())
except Exception, e:
print e
sys.exit(1)
tagged = tag(tdata)
print json.dumps(tagged, ensure_ascii=True)