-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonio.py
executable file
·64 lines (52 loc) · 1.29 KB
/
jsonio.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import json
def dict2obj(aDict):
top = type('new', (object,), aDict)
seqs = tuple, list, set, frozenset
for i, j in aDict.items():
if isinstance(j, dict):
setattr(top, i, dict2obj(j))
elif isinstance(j, seqs):
setattr(top, i,
type(j)(dict2obj(sj) if isinstance(sj, dict) else sj for sj in j))
else:
setattr(top, i, j)
return top
def put(data, filename):
jsondata = json.dumps(data, indent=4, skipkeys=True, sort_keys=True)
print 'A'
try:
print 'B'
jsondata = json.dumps(data, indent=4, skipkeys=True, sort_keys=True)
print 'C'
fd = open(filename, 'w')
print 'D'
fd.write(jsondata)
print 'E'
fd.close()
print 'F'
except:
print 'ERROR writing', filename
pass
def get(filename):
aDict = {}
try:
fd = open(filename, 'r')
text = fd.read()
fd.close()
aDict = json.loads(text)
except:
print 'COULD NOT LOAD:', filename
return aDict
def getObj(filename):
return dict2obj(get(filename))
if __name__ == '__main__':
o = get(sys.argv[1]);
if o:
print "Got : %s" % (dict2obj(o).installed.client_secret)
put(o, "new_" + sys.argv[1]);
# print "Done."
else:
print "Huh? %s" % (sys.argv[1])