0
|
1 '''
|
|
2 @author Rui Wang
|
|
3 @see LICENSE (MIT style license file).
|
|
4 '''
|
|
5
|
|
6
|
|
7 import ZSI.TC
|
|
8 import copy
|
|
9
|
|
10 __author__="Rui Wang, Chaitanya Guttula"
|
|
11
|
|
12 class MessageHandler:
|
|
13 '''handle message class'''
|
|
14
|
|
15 def msAssign(self, msInstance, nameValueDic):
|
|
16 '''given message class instance, _name:value tree structure dictionary,
|
|
17 assign the values to the correct leaves in the message tree,
|
|
18 return message class instance with values'''
|
|
19
|
|
20 #get the list of all parts of message
|
|
21 partList=getattr(getattr(msInstance, 'typecode'), 'ofwhat')
|
|
22 for part in partList:
|
|
23 #_name of part of message
|
|
24 partName=getattr(part, 'aname')
|
|
25 if partName not in nameValueDic.keys():
|
|
26 continue
|
|
27 #value assigned to part, can be simple type(directly assign) or tree structure(pass to complextypeAssign() or arraytypeAssign())
|
|
28 partValue=nameValueDic[partName]
|
|
29 if isinstance(part, ZSI.TC.ComplexType):
|
|
30 complextypeHolderClassInstance=self.complextypeAssign(part, partValue)
|
|
31 setattr(msInstance, partName, complextypeHolderClassInstance)
|
|
32 elif isinstance(part, ZSI.TC.Array) :
|
|
33 subArray=self.arraytypeAssign(part, partValue)
|
|
34 setattr(msInstance, partName, subArray)
|
|
35 print 'the arraay type is : ',subArray
|
|
36 else:
|
|
37 #simple type such as string...directly assign the value to the part
|
|
38 setattr(msInstance, partName, partValue)
|
|
39 return msInstance
|
|
40
|
|
41 def complextypeAssign(self, complextypeInstance, subNameValueDic):
|
|
42 '''given complex type instance, subtree of _name:value dictionary
|
|
43 traverse the tree structure recursively,
|
|
44 return top complextype holder class instance with values filled in'''
|
|
45
|
|
46 if not isinstance(complextypeInstance, ZSI.TC.ComplexType):
|
|
47 raise TypeError, 'First parameter has to be instance of ZSI.TC.ComplexType'
|
|
48 else:
|
|
49 complextypeHolderClassInstance=getattr(complextypeInstance,'pyclass')()
|
|
50
|
|
51 #element instance list of complex type
|
|
52 elementList=getattr(complextypeInstance, 'ofwhat')
|
|
53 for ele in elementList:
|
|
54
|
|
55 #_name of element of complex type
|
|
56 eleName=getattr(ele, 'aname')
|
|
57 if eleName not in subNameValueDic.keys():
|
|
58 continue
|
|
59
|
|
60 #value assigned to element, can be simple type(directly assign) or tree structure(pass to complextypeAssign() or arraytypeAssign())
|
|
61 eleValue=subNameValueDic[eleName]
|
|
62 if isinstance(ele, ZSI.TC.ComplexType):
|
|
63 subclomplextypeClassInstance=self.complextypeAssign(ele, eleValue)
|
|
64 setattr(complextypeHolderClassInstance, eleName, subclomplextypeClassInstance)
|
|
65 elif isinstance(ele, ZSI.TC.Array) :
|
|
66 subArray=self.arraytypeAssign(ele, eleValue)
|
|
67 setattr(complextypeHolderClassInstance, eleName, subArray)
|
|
68 else:
|
|
69
|
|
70 #simple type such as string...directly assign the value to the element
|
|
71 setattr(complextypeHolderClassInstance, eleName, eleValue)
|
|
72
|
|
73 return complextypeHolderClassInstance
|
|
74
|
|
75 def arraytypeAssign(self,arraytypeInstance, subValueArray):
|
|
76 '''given arraytype class instance, [values...],
|
|
77 recursively handle values,
|
|
78 return [arraytypeInstance filled with values]'''
|
|
79
|
|
80
|
|
81 if not isinstance(arraytypeInstance, ZSI.TC.Array):
|
|
82 raise TypeError, 'First parameter has to be instance of ZSI.TC.Array'
|
|
83 else:
|
|
84 resultArray=[]
|
|
85 if subValueArray is None:
|
|
86 return None
|
|
87 if subValueArray is []:
|
|
88 return []
|
|
89
|
|
90 #element instance of array type
|
|
91 elementInstance=getattr(arraytypeInstance, 'ofwhat')
|
|
92 if isinstance(elementInstance, ZSI.TC.ComplexType):
|
|
93 for eleValue in subValueArray:
|
|
94 subclomplextypeClassInstance=self.complextypeAssign(copy.deepcopy(elementInstance), eleValue)
|
|
95 resultArray.append(subclomplextypeClassInstance)
|
|
96 elif isinstance(elementInstance, ZSI.TC.Array) :
|
|
97 for eleValue in subValueArray:
|
|
98 subArray=self.arraytypeAssign(copy.deepcopy(elementInstance), eleValue)
|
|
99 resultArray.append(subArray)
|
|
100
|
|
101 else:
|
|
102
|
|
103 #simple type such as string...directly return the [value,...]
|
|
104 resultArray=subValueArray
|
|
105
|
|
106 return resultArray
|
|
107
|
|
108 def msParser(self, msInstance):
|
|
109 '''given message class instance,
|
|
110 parse the message class tree,
|
|
111 return message tree of _name:value dictionary'''
|
|
112
|
|
113 nameValueDic={}
|
|
114
|
|
115
|
|
116 #get the list of all parts of message
|
|
117 partList=getattr(getattr(msInstance, 'typecode'), 'ofwhat')
|
|
118 for part in partList:
|
|
119
|
|
120 #_name of part of message
|
|
121 partName=getattr(part, 'aname')
|
|
122 partValueDefault=getattr(msInstance, partName)
|
|
123
|
|
124 #value assigned to part, can be simple type(directly assign) or tree structure(pass to complextypeAssign() or arraytypeAssign())
|
|
125 # partValue=nameValueDic[partName]
|
|
126 # nameValueDic[partName]=None
|
|
127 if isinstance(part, ZSI.TC.ComplexType):
|
|
128 nameValueDic[partName]=self.complextypeParser(part)
|
|
129 elif isinstance(part, ZSI.TC.Array) :
|
|
130 nameValueDic[partName]=self.arraytypeParser(part, partValueDefault)
|
|
131 else:
|
|
132 #simple type such as string...directly retrieve the value of the part
|
|
133 nameValueDic[partName]=partValueDefault
|
|
134 return nameValueDic
|
|
135
|
|
136 def complextypeParser(self, complextypeInstance):
|
|
137 '''given complex type class instance,
|
|
138 traverse the tree structure recursively,
|
|
139 return subtree of _name:value dictionary'''
|
|
140
|
|
141 if not isinstance(complextypeInstance, ZSI.TC.ComplexType):
|
|
142 raise TypeError, 'First parameter has to be instance of ZSI.TC.ComplexType'
|
|
143 else:
|
|
144 subNameValueDic={}
|
|
145 complextypeHolderClassInstance=getattr(complextypeInstance,'pyclass')()
|
|
146 #element instance list of complex type
|
|
147 elementList=getattr(complextypeInstance, 'ofwhat')
|
|
148 for ele in elementList:
|
|
149 #_name of element of complex type
|
|
150 eleName=getattr(ele, 'aname')
|
|
151 eleValueDefault=getattr(complextypeHolderClassInstance,eleName)
|
|
152 print eleName
|
|
153 #value assigned to element, can be simple type(directly assign) or tree structure(pass to complextypeAssign() or arraytypeAssign())
|
|
154 # eleValue=subNameValueDic[eleName]
|
|
155 if isinstance(ele, ZSI.TC.ComplexType):
|
|
156 if(getattr(ele,'maxOccurs')=='unbounded'): # Handling element with maxOccurances as unbounded and type as complextype
|
|
157 print 'arraytype',eleName
|
|
158 tempArray=[]
|
|
159 sub = self.complextypeParser(ele)
|
|
160 tempArray.append(sub)
|
|
161 print 'array is : ',tempArray
|
|
162 subNameValueDic[eleName]=tempArray #tempArray.append(sub)#self.arraytypeParser(ele, eleValueDefault)
|
|
163 else:
|
|
164 print 'complextype', eleName
|
|
165 subNameValueDic[eleName]=self.complextypeParser(ele)
|
|
166 elif isinstance(ele, ZSI.TC.Array):
|
|
167 print 'arraytype', eleValueDefault
|
|
168 subNameValueDic[eleName]=self.arraytypeParser(ele, eleValueDefault)
|
|
169 else:
|
|
170 subNameValueDic[eleName] = eleValueDefault
|
|
171
|
|
172 return subNameValueDic
|
|
173
|
|
174
|
|
175 def arraytypeParser(self,arraytypeInstance, defaultValueArray):
|
|
176 '''given arraytype class instance, and its default value [defaultvalues...]
|
|
177 if element of array is not simple type then recursively handle traverse the type tree,
|
|
178 return [values...]
|
|
179 else return defaultValueArray[defaultvalues...]'''
|
|
180
|
|
181
|
|
182 if (not isinstance(arraytypeInstance, ZSI.TC.Array)): #or (getattr(arraytypeInstance,'maxOccurs')!='unbounded') :
|
|
183 raise TypeError, 'First parameter has to be instance of ZSI.TC.Array'
|
|
184 else:
|
|
185 subValueArray=[]
|
|
186
|
|
187
|
|
188 #element instance of array type
|
|
189 elementInstance=getattr(arraytypeInstance, 'ofwhat')
|
|
190 if isinstance(elementInstance, ZSI.TC.ComplexType):
|
|
191 if defaultValueArray is None:
|
|
192 subclomplextypeClassInstance=self.complextypeParser(elementInstance)
|
|
193 subValueArray.append(subclomplextypeClassInstance)
|
|
194 else:
|
|
195 for eleValue in defaultValueArray:
|
|
196 subclomplextypeClassInstance=self.complextypeParser(eleValue)
|
|
197 subValueArray.append(subclomplextypeClassInstance)
|
|
198 elif isinstance(elementInstance, ZSI.TC.Array) :
|
|
199 if defaultValueArray is None:
|
|
200 subarraytypeClassInstance=self.arraytypeParser(elementInstance, None)
|
|
201 subValueArray.append(subarraytypeClassInstance)
|
|
202 else:
|
|
203 for eleValue in defaultValueArray:
|
|
204 subArray=self.arraytypeParser(copy.deepcopy(elementInstance), eleValue)
|
|
205 subValueArray.append(subArray)
|
|
206
|
|
207 else:
|
|
208 #simple type such as string...directly return the [value,...]of the arraytypeInstance (default value)
|
|
209 subValueArray=defaultValueArray
|
|
210
|
|
211 return subValueArray
|
|
212
|
|
213
|
|
214 def flatten(self,obj):
|
|
215 if obj is None:
|
|
216 return None
|
|
217 elif hasattr(obj, '__dict__') and obj.__dict__:
|
|
218 return dict([(k, self.flatten(v)) for (k, v) in obj.__dict__.items()])
|
|
219 if isinstance(obj, (dict,)):
|
|
220 return dict([(k, self.flatten(v)) for (k, v) in obj.items()])
|
|
221 elif isinstance(obj, (list,)):
|
|
222 return [self.flatten(x) for x in obj]
|
|
223 elif isinstance(obj, (tuple,)):
|
|
224 return tuple([flatten(x) for x in obj])
|
|
225 else:
|
|
226 return obj
|
|
227
|
|
228 def getParameter(self, msInstance,param):
|
|
229 '''given message class instance, and parameter
|
|
230 parse the message class tree,
|
|
231 return the element '''
|
|
232 nameValueDic={}
|
|
233 plist = []
|
|
234 if(param.find('|0|')>-1):
|
|
235 s = param.split('|0|')
|
|
236 print 'found |0|',s
|
|
237 for l in s:
|
|
238 if(l.find('|')>-1):
|
|
239 t = l.split('|')
|
|
240 print 'found |',t
|
|
241 for i in t:
|
|
242 print 'i',i
|
|
243 plist.append(i)
|
|
244 else:
|
|
245 plist.append(l)
|
|
246 elif(param.find('|')>-1):
|
|
247 plist = param.split('|')
|
|
248 else:
|
|
249 partlist = getattr(getattr(msInstance , 'typecode'),'ofwhat')
|
|
250 for part in partlist:
|
|
251 if param == getattr(part,'aname'):
|
|
252 print part
|
|
253 return part
|
|
254
|
|
255
|
|
256 element = None
|
|
257 #get the list of all parts of message
|
|
258 partList=getattr(getattr(msInstance, 'typecode'), 'ofwhat')
|
|
259 for part in partList:
|
|
260 #_name of part of message
|
|
261 partName=getattr(part, 'aname')
|
|
262
|
|
263 if(partName == plist[0]):
|
|
264
|
|
265 partname=getattr(part,'pname')
|
|
266 partValueDefault=getattr(msInstance, partName)
|
|
267 break
|
|
268 #value assigned to part, can be simple type(directly assign) or tree structure(pass to complextypeAssign() or arraytypeAssign())
|
|
269 # partValue=nameValueDic[partName]
|
|
270 # nameValueDic[partName]=None
|
|
271 if isinstance(part, ZSI.TC.ComplexType):
|
|
272 print 'complextype', getattr(part,'aname')#plist[-1]
|
|
273 sp = ( self.complextypParser(part,plist[-1]))
|
|
274 if(sp !=None):
|
|
275 element = sp
|
|
276
|
|
277 elif isinstance(part, ZSI.TC.Array) :
|
|
278 element = self.arraytypParser(part, partValueDefault,plist[-1])
|
|
279 #nameValueDic[partName]=self.arraytypeParser(part, partValueDefault,param)
|
|
280 else:
|
|
281 #simple type such as string...directly retrieve the value of the part
|
|
282 if(partName == plist[-1]):
|
|
283 element = part
|
|
284 print 'spart',element
|
|
285 res = getattr(element,'nillable')
|
|
286 print res
|
|
287 return element
|
|
288
|
|
289 def complextypParser(self, complextypeInstance,param):
|
|
290 '''given complex type class instance, and paramater name
|
|
291 traverse the tree structure recursively,
|
|
292 return the element with parameter name'''
|
|
293 part =None
|
|
294 if not isinstance(complextypeInstance, ZSI.TC.ComplexType):
|
|
295 raise TypeError, 'First parameter has to be instance of ZSI.TC.ComplexType'
|
|
296 else:
|
|
297 subNameValueDic={}
|
|
298 complextypeHolderClassInstance=getattr(complextypeInstance,'pyclass')()
|
|
299 #element instance list of complex type
|
|
300 elementList=getattr(complextypeInstance, 'ofwhat')
|
|
301
|
|
302 for ele in elementList:
|
|
303 #_name of element of complex type
|
|
304 eleName=getattr(ele, 'aname')
|
|
305 eleValueDefault=getattr(complextypeHolderClassInstance, eleName)
|
|
306 #value assigned to element, can be simple type(directly assign) or tree structure(pass to complextypeAssign() or arraytypeAssign())
|
|
307 # eleValue=subNameValueDic[eleName]
|
|
308 if isinstance(ele, ZSI.TC.ComplexType):
|
|
309
|
|
310 if getattr(ele,'aname') == param:
|
|
311 print 'returning the middle complex type'
|
|
312 return ele
|
|
313 else:
|
|
314 part = self.complextypParser(ele,param)
|
|
315 return part
|
|
316 #subNameValueDic[eleName]=self.complextypeParser(ele)
|
|
317 # setattr(complextypeHolderClassInstance, eleName, subclomplextypeClassInstance)
|
|
318 elif isinstance(ele, ZSI.TC.Array) :
|
|
319 part = self.arraytypParser(ele, eleValueDefault,param)
|
|
320 return part
|
|
321
|
|
322 else:
|
|
323 if(param == eleName):
|
|
324 print 'matched',param
|
|
325 part = (ele)
|
|
326 return part
|
|
327 #simple type such as string...directly retrieve the value of the element
|
|
328 subNameValueDic[eleName]=eleValueDefault
|
|
329 return None#subNameValueDic
|
|
330
|
|
331
|
|
332
|
|
333 def arraytypParser(self,arraytypeInstance, defaultValueArray,param):
|
|
334 '''given arraytype class instance, and its default value [defaultvalues...]
|
|
335 if element of array is not simple type then recursively handle traverse the type tree,
|
|
336 return [values...]
|
|
337 else return defaultValueArray[defaultvalues...]'''
|
|
338
|
|
339
|
|
340 if not isinstance(arraytypeInstance, ZSI.TC.Array):
|
|
341 raise TypeError, 'First parameter has to be instance of ZSI.TC.Array'
|
|
342 else:
|
|
343 subValueArray=[]
|
|
344
|
|
345 #element instance of array type
|
|
346 elementInstance=getattr(arraytypeInstance, 'ofwhat')
|
|
347 if isinstance(elementInstance, ZSI.TC.ComplexType):
|
|
348 if defaultValueArray is None:
|
|
349 part = self.complextypParser(elementInstance,param)
|
|
350 return part
|
|
351 # subclomplextypeClassInstance=self.complextypeParser(elementInstance)
|
|
352 # subValueArray.append(subclomplextypeClassInstance)
|
|
353 else:
|
|
354 for eleValue in defaultValueArray:
|
|
355 part = self.complextypParser(eleValue,defaulValueArray,param)
|
|
356 subclomplextypeClassInstance=self.complextypeParser(eleValue)
|
|
357 # subValueArray.append(subclomplextypeClassInstance)
|
|
358 # setattr(elementInstance, eleName, subclomplextypeClassInstance)
|
|
359 elif isinstance(elementInstance, ZSI.TC.Array) :
|
|
360 # print 'arraytype', elementInstance
|
|
361 if defaultValueArray is None:
|
|
362 part = self.arraytypParser(elementInstance, None,param)
|
|
363 # subarraytypeClassInstance=self.arraytypeParser(elementInstance, None)
|
|
364 # subValueArray.append(subarraytypeClassInstance)
|
|
365 else:
|
|
366 for eleValue in defaultValueArray:
|
|
367 part = self.arraytypParser(copy.deepcopy(elementInstance), eleValue,param)
|
|
368 # subArray=self.arraytypeParser(copy.deepcopy(elementInstance), eleValue)
|
|
369 # subValueArray.append(subArray)
|
|
370
|
|
371 else:
|
|
372 #simple type such as string...directly return the [value,...]of the arraytypeInstance (default value)
|
|
373 return elementInstance
|
|
374 return None
|
|
375
|
|
376
|
|
377
|
|
378 #if __name__=="__main__":
|
|
379
|
|
380
|