-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
282 lines (156 loc) · 5.52 KB
/
core.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
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
# from flask import Flask, render_template, url_for, request, json
# from flask import make_response
STR_RESULT = 'result'
STR_META = 'meta'
def testone(json_):
return json_
def convert_json_to_tson(json_):
response = {}
metadata = {}
unique_key = 0
for key, values in json_.items():
if type(values) == dict:
print('dict')
elif type(values) == list:
meta = metadata.get(key)
if(meta is None):
response[str(unique_key)]=[]
sub = unique_key
metadata[key] = unique_key
unique_key += 1
else:
response[str(meta)]=[]
sub = meta
list_json={}
list_json, unique_key = traverselist(values, list_json, metadata, unique_key)
response[str(sub)].extend(list_json)
else:
meta = metadata.get(key)
print('meta_{}'.format(meta))
if(meta is None):
response[str(unique_key)] = values
metadata[key] = unique_key
unique_key += 1
else:
response[str(meta)] = values
tson_response = {}
tson_response[STR_RESULT] = response
tson_response[STR_META] = swap_metadata(metadata)
# res = make_response(json.dumps(tson_response, ensure_ascii = False))
# res.headers["Content-Type"] = "application/json; charset=utf-8"
# return res, tson_response
return tson_response
def traversedic(dic, response, metadata, unique_key):
dict_json = {}
for key, value in dic.items():
if (type(value) is dict):
print('dict')
elif (type(value) is list):
traverselist(conn, value, url)
else:
print(key)
meta = metadata.get(key)
print('meta_{}'.format(meta))
if(meta is None):
dict_json[str(unique_key)] = value
metadata[key] = unique_key
unique_key += 1
else:
dict_json[str(meta)] = value
# print('dict json')
# print(dict_json)
return dict_json, unique_key
def traverselist(listObj, response, metadata, unique_key):
list_json=[]
if len(listObj) > 0:
for value in listObj:
print ('list value')
print (value)
print ('---------')
if(type(value) is dict):
dict_json, unique_key = traversedic(value, response, metadata, unique_key)
print(unique_key)
list_json.append(dict_json)
# print(list_json)
else:
print ("-" + str(value))
print (type(value))
return list_json, unique_key
def swap_metadata(metadata):
meta_data = {}
for key, value in metadata.items():
meta_data[str(value)] = str(key)
return meta_data
def convert_tson_to_json(tson):
response = {}
try:
meta_data = tson[STR_META]
print(meta_data)
json_ = tson[STR_RESULT]
for key, values in json_.items():
if type(values) == dict:
print('dict')
elif type(values) == list:
meta = meta_data.get(key)
if(meta is not None):
response[str(meta)]=[]
else:
raise KeyError(key)
list_json = {}
list_json = traverse_to_list(values, list_json, meta_data)
response[str(meta)].extend(list_json)
else:
meta = meta_data.get(key)
if(meta is not None):
response[str(meta)] = values
else:
print('metadata not found for {}'.format(key))
print(response)
# res = make_response(json.dumps(response, ensure_ascii = False))
# res.headers["Content-Type"] = "application/json; charset=utf-8"
return response
except KeyError as e:
print(e)
failure_response = {}
failure_response['message'] = 'No Metadata or result object found for {}'.format(str(e))
# res = make_response(json.dumps(failure_response, ensure_ascii = False), 400)
# res.headers["Content-Type"] = "application/json; charset=utf-8"
# return res
return failure_response
def traverse_to_dic(dic, response, metadata):
dict_json = {}
for key, value in dic.items():
if (type(value) is dict):
print('dict')
elif (type(value) is list):
traverselist(conn, value, url)
else:
meta = metadata.get(key)
if(meta is not None):
dict_json[str(meta)] = value
else:
print('metadata not found for {}'.format(key))
return dict_json
def traverse_to_list(listObj, response, metadata):
list_json = []
if len(listObj) > 0:
for value in listObj:
print ('list value')
print (value)
print ('---------')
if(type(value) is dict):
dict_json= traverse_to_dic(value, response, metadata)
list_json.append(dict_json)
else:
print ("-" + str(value))
print (type(value))
return list_json
def startpy():
json_ = {
"one" : "two"
}
tson_ = convert_json_to_tson(json_)
print(tson_)
return None
if __name__ == '__main__':
startpy()