-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonJsonObj.py
More file actions
282 lines (225 loc) · 6.13 KB
/
PythonJsonObj.py
File metadata and controls
282 lines (225 loc) · 6.13 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import utility as utl
import sys
import constant as constant
DEBUG = True
DEBUG = False
def debug(str):
#print str
pass
class PyJSOb:
def __init__(self,tag=None,path=None, type="OBJECT"):
self.key = tag
self.path = path
self.label = None
self.children = []
self.prim_mem = []
self.obj_mem = []
self.array_mem = []
self.type = type
self.label = constant.DEFAULT_LABEL
self.label_path = "None"
# if a label is applied it means which ob-path the label comes from
def set_label_path(self,path):
self.label_path = path
def set_type(self, type="OBJECT"):
self.type = type
def set_key (self,tag):
self.key = tag
def add_child (self,child):
self.children.append(child)
def add_prim_mem(self,key=None, value=None):
if key:
self.prim_mem.append({key:value})
else:
self.prim_mem.append(value)
def add_obj_mem(self,key = None, value=None):
if key:
#value.set_key(key)
self.obj_mem.append( {key:value} )
else:
self.obj_mem.append(obj)
def add_array_mem(self,key=None, value=[]):
self.set_type("ARRAY")
#if key:
if value:
self.array_mem.append(value)
#else:
# self.array_mem.append(value)
def set_label (self,label):
self.label = label
def get_prim_mem(self):
return self.prim_mem
def set_path(self,path):
self.abs_path = path
def _print_array(self, arr):
json = ""
#if arr.key:
# json = json + "\"" + arr.key + "\"" + " : "
if arr.type == "ARRAY":
json = json + " [ "
commaFlg = False
if DEBUG:
lv = "\"{}\"".format(arr.label)
json = json + lv + ","
lv = "\"{}\"".format(arr.label_path)
json = json + lv + ","
lv = "\"{}\"".format(arr.abs_path)
json = json + lv + ","
lv = "\"{}\"".format(id(arr))
json = json + lv
commaFlg = True
# print primitive members first.
for a in arr.prim_mem:
if commaFlg :
json = json + " , "
if type(a) is str:
json = json + "\"" + str(a) + "\""
elif type(a) is int or type(a) is float:
json = json + str(a)
else:
#print a, type(a)
json = json + "\"" + str(a) + "\""
commaFlg = True
'''# print object members
for o in arr.obj_mem:
if commaFlg:
json = json + " , "
json = json + self._print_obj(o)
commaFlg = True
'''
# print array members
#print arr.array_mem
for a in arr.array_mem:
if commaFlg:
json = json + " , "
if a.type == "ARRAY":
json = json + self._print_array(a)
elif a.type == "OBJECT":
json = json + self._print_obj(a)
else:
pass
commaFlg = True
if arr.type == "ARRAY":
json = json + "]"
return json
def _print_obj(self,obj):
json = ""
commaFlg=False
#if obj.key :
# json = "\"" + obj.key + "\"" + " : "
json = json + " { "
# get primitive member first.
if DEBUG:
lv = "\"{}\":\"{}\"".format("label",obj.label)
json = json + lv + ","
lv = "\"{}\" : \"{}\"".format("label_from",obj.label_path)
json = json + lv + ","
lv = "\"{}\" : \"{}\"".format("path",obj.abs_path)
json = json + lv + ","
lv = "\"{}\" : \"{}\"".format("object_id",id(obj))
json = json + lv
commaFlg = True
for kv in obj.prim_mem:
for (k,v) in kv.items():
if commaFlg:
json = json + " , "
json = json + "\""+str(k) + "\"" + " : " + "\"" + str(v) + "\""
commaFlg = True
# get obj member...
for o in obj.obj_mem:
for (k,v) in o.items():
if commaFlg :
json = json + " , "
if v.type == "OBJECT": #!!!!!
#json = json + self._print_obj(o)
json = "{} \"{}\":{}".format(json,k,self._print_obj(v))
elif v.type == "ARRAY":
#json = json + self._print_array(o)
json = "{} \"{}\" : {}".format(json,k,self._print_array(v))
else:
pass
commaFlg = True
#json = json + " , " ## some fix
# get array member
'''
for a in obj.array_mem:
if commaFlg :
json = json + " , "
json = json + self._print_array(a)
commaFlg = True
'''
json = json + " } "
return json
def print_json (self):
if self.type == "OBJECT":
return self._print_obj(self)
elif self.type == "ARRAY":
return self._print_array(self)
else:
return {"error":"Neting obj nor Array"}
def pretty_print(self):
return utl.pretty_print( self.print_json())
'''
this class represent the whole object tree of the json data
'''
class PyObjTree:
def __init__(self, jsonObj):
self.root = self.buildTree(jsonObj,"")
#return self.root
def get_root(self):
return self.root
def buildTree(self,jsonObj,path):
#py_obj = PyJSOb(type="OBJECT")
#print jsonObj
#jsonObj.set_path(path)
if type(jsonObj) is dict:
py_obj = PyJSOb(type="OBJECT")
#print "setting path = {}----------------".format(path)
py_obj.set_path(path)
#print "setting path = {} on object id {}----------------".format(py_obj.abs_path,id(py_obj))
for (k,v) in jsonObj.items():
#print k, v
t_path = "{}/{}".format(path,k)
if type(v) is dict:
py_obj.add_obj_mem( key=k, value = self.buildTree(v,t_path) )
elif type(v) is list:
py_obj.add_obj_mem(key=k, value = self.buildTree(v,t_path))
elif type(v) is str or type(v) is int or type(v) is float:
#print v
py_obj.add_prim_mem(key=k, value = v)
else:
py_obj.add_prim_mem(key=k, value = v)
#print type(v)
pass
return py_obj
elif type (jsonObj) is list:
py_obj = PyJSOb(type="ARRAY")
py_obj.set_path(path)
index = 0
for arr in jsonObj:
t_path = "{}[{}]".format(path,index)
if type(arr) is list:
py_obj.add_array_mem( value=self.buildTree(arr,t_path))
elif type(arr) is dict:
py_obj.add_array_mem(value=self.buildTree(arr,t_path))
elif type(arr) is str or type(arr) is int or type(arr) is float:
py_obj.add_prim_mem(value = arr)
else:
py_obj.add_prim_mem(value = arr)
index = index + 1
return py_obj
else:
pass
def test():
if len(sys.argv) < 2:
print "give all arguments"
exit()
job = utl.LoadJSON(path=sys.argv[1]).get_json()
tree = PyObjTree(job)
pyjsob = PyJSOb()
to = tree.root
to.get_prim_mem()
#print to.print_json()
print to.pretty_print()
if __name__ == "__main__":
test()