Mercurial > repos > uga-galaxy-group > webservice_toolsuite_v1_1
comparison WebServiceExtensionsV1.1/WebServiceToolWorkflow_REST_SOAP/clientGenerator/creatorEngineComplex.py~ @ 0:049760c677de default tip
Galaxy WSExtensions added successfully
author | uga-galaxy-group |
---|---|
date | Tue, 05 Jul 2011 19:34:18 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:049760c677de |
---|---|
1 ''' | |
2 @author Rui Wang | |
3 @see LICENSE (MIT style license file). | |
4 ''' | |
5 | |
6 from types import * | |
7 import copy | |
8 import ZSI.TC | |
9 from introspect import * | |
10 from msHandler import * | |
11 from paramConverter import * | |
12 | |
13 __author__="Rui Wang" | |
14 | |
15 class ClientCreator(Introspector): | |
16 """all method to introspect module | |
17 to return operations&inputs | |
18 and to invoke operation""" | |
19 | |
20 | |
21 | |
22 def path2Ops(self, modulePath ): | |
23 '''given module(the service python file generated by wsdl2py) path, | |
24 return dictionary of operation name: object (functions of class-**SOAP)''' | |
25 allclass=self.path2Class(modulePath) | |
26 #locator='' | |
27 opclass='' | |
28 for c in allclass.keys(): | |
29 if c[len(c)-4:len(c)]=='SOAP': | |
30 opclass=c | |
31 | |
32 | |
33 #class object of which holds all ops | |
34 opclassOb=allclass[opclass] | |
35 #list of names of all ops | |
36 allattr=dir(opclassOb) | |
37 #print allattr | |
38 allops={} | |
39 for opt in allattr: | |
40 #every operation object | |
41 optemp=getattr(opclassOb, opt) | |
42 #whether it's def and not private such as __init__ | |
43 if callable(optemp) and optemp.__name__.startswith('__')==False: | |
44 allops[optemp.__name__]=optemp | |
45 | |
46 return allops | |
47 | |
48 def path2messageClassOb(self, modulePath): | |
49 '''given module path, | |
50 return all message class object''' | |
51 | |
52 | |
53 allclassOb=self.path2Class(modulePath) | |
54 | |
55 #find all message classes: has 'typecode', type(obtemp)==ClassType | |
56 allmsOb=[] | |
57 for c in allclassOb.values(): | |
58 if hasattr(c,'typecode'): | |
59 allmsOb.append(c) | |
60 return allmsOb | |
61 | |
62 def opname2inputClassOb(self, opName, modulePath): | |
63 '''given operation name, module path, | |
64 return input class object''' | |
65 | |
66 allopNames=self.path2Ops(modulePath) | |
67 if opName not in allopNames: | |
68 raise NameError, 'Warning: No operation has the given name!!' | |
69 | |
70 else: | |
71 #find all message classes: has 'typecode', type(obtemp)==ClassType | |
72 allmsOb=self.path2messageClassOb(modulePath) | |
73 #find input message class of given operation: typecode has pname=operation name | |
74 for ms in allmsOb: | |
75 pnameOb=getattr(getattr(ms, 'typecode'), 'pname') | |
76 if opName==str(pnameOb): | |
77 inputClass=ms | |
78 break | |
79 | |
80 return inputClass | |
81 | |
82 def opname2outputClassOb(self, opName, modulePath): | |
83 '''given operation name, module path, | |
84 return output class object''' | |
85 | |
86 allopNames=self.path2Ops(modulePath) | |
87 if opName not in allopNames: | |
88 raise NameError, 'Warning: No operation has the given name!!' | |
89 | |
90 else: | |
91 #find all message classes: has 'typecode', type(obtemp)==ClassType | |
92 allmsOb=self.path2messageClassOb(modulePath) | |
93 | |
94 #find output message class of given operation: typecode has pname=operation name+'Response' | |
95 for ms in allmsOb: | |
96 pnameOb=getattr(getattr(ms, 'typecode'), 'pname') | |
97 if opName+'Response'==str(pnameOb): | |
98 outputClass=ms | |
99 break | |
100 | |
101 return outputClass | |
102 | |
103 def opname2outputs(self, opName, modulePath): | |
104 '''given operation name, module path, | |
105 return a list of output nemes of the operation in the module''' | |
106 | |
107 allopNames=self.path2Ops(modulePath) | |
108 if opName not in allopNames: | |
109 raise NameError, 'Warning: No operation has the given name!!' | |
110 | |
111 else: | |
112 #get input class object | |
113 outputClass=self.opname2outputClassOb(opName, modulePath) | |
114 #find input parameters from message class : instrospect an instace | |
115 msinstance=outputClass() | |
116 | |
117 parameters={} | |
118 mshandler=MessageHandler() | |
119 parameters=mshandler.msParser(msinstance) | |
120 | |
121 return parameters | |
122 | |
123 def opname2inputs(self, opName, modulePath): | |
124 '''given operation name, module path, | |
125 return a list of all inputs name of the operation in the given module''' | |
126 | |
127 allopNames=self.path2Ops(modulePath) | |
128 if opName not in allopNames: | |
129 raise NameError, 'Warning: No operation has the given name!!' | |
130 else: | |
131 #get input class object | |
132 inputClass=self.opname2inputClassOb(opName, modulePath) | |
133 | |
134 parameters={} | |
135 mshandler=MessageHandler() | |
136 parameters=mshandler.msParser(inputClass()) #mshandler.flatten(inputClass()) | |
137 | |
138 print 'The parametrrs after flattening : ',parameters | |
139 | |
140 ofwhat=getattr(getattr(inputClass, 'typecode'), 'ofwhat') | |
141 paramtypes={} | |
142 i=0 | |
143 for k in parameters.keys(): | |
144 paramtypes[k]=str(ofwhat[i]) | |
145 i=i+1 | |
146 for l in paramtypes.keys(): | |
147 print "paramters",l | |
148 origPar=paramtypes[l] | |
149 copyPar=paramtypes[l] | |
150 #print copyPar#[-23:-1] | |
151 newPar=origPar.replace(copyPar[-23:-1],'').strip('<>') | |
152 paramtypes[l]=newPar | |
153 | |
154 | |
155 | |
156 # print ofwhat | |
157 ## for p in ofwhat: | |
158 ## if isinstance(p, ZSI.TC.ComplexType): | |
159 ## print 'complextype', p | |
160 ## elif isinstance(p, ZSI.TC.Array) : | |
161 ## print 'arraytype', p | |
162 ## elif isinstance(p,ZSI.TC.String): | |
163 ## print 'String',p | |
164 ## elif isinstance(p,ZSI.TC.Integer): | |
165 ## print 'Integer',p | |
166 ## else: | |
167 ## parameters[getattr(p, 'aname')]=None | |
168 ## | |
169 #print parameters | |
170 return parameters | |
171 #return paramtypes | |
172 #find input parameters from message class : instrospect an instace | |
173 # msinstance=inputClass()u can | |
174 ## print dir(msinstance) | |
175 # parameters=[] | |
176 # for i in dir(msinstance): | |
177 ## print i, type(getattr(msinstance, i)) | |
178 # if (type(getattr(msinstance, i)) is NoneType) and i!='__doc__': | |
179 # parameters.append(i) | |
180 ## print parameters | |
181 # return parameters | |
182 | |
183 def path2service(self, modulePath): | |
184 '''given module path, | |
185 return return service instance | |
186 instead of: dbfetchSrv = WSDBFetchServerLegacyServiceLocator().getWSDBFetchServerLegacy()''' | |
187 | |
188 allclass=self.path2Class(modulePath) | |
189 | |
190 locator='' | |
191 for c in allclass.keys(): | |
192 if c[len(c)-7:len(c)]=='Locator': | |
193 locator=c | |
194 | |
195 locatorClassOb=allclass[locator] | |
196 for obname in dir(locatorClassOb): | |
197 obtemp=getattr(locatorClassOb, obname) | |
198 if (type(obtemp) is MethodType)and not obtemp.__name__.endswith('Address'): | |
199 serverMethod=obtemp | |
200 break | |
201 # Create a service interface | |
202 service=serverMethod(locatorClassOb()) | |
203 return service | |
204 | |
205 def invokeOp(self, opName, modulePath, inputs): | |
206 allopNames=self.path2Ops(modulePath) | |
207 if opName not in allopNames: | |
208 raise NameError, 'Warning: No operation has the given name!!' | |
209 else: | |
210 # Create a service interface | |
211 serviceInstance=self.path2service(modulePath) | |
212 | |
213 #get input class object of given opName | |
214 inputClass=self.opname2inputClassOb(opName, modulePath) | |
215 | |
216 mshandler=MessageHandler() | |
217 if len(inputs) != 0: | |
218 inputClassInstance=mshandler.msAssign(inputClass(), inputs) | |
219 else: | |
220 inputClassInstance= inputClass() | |
221 #get input name list of given opName | |
222 # inputNames=self.opname2inputs(opName, modulePath) | |
223 | |
224 #set value for inputs | |
225 #request._query = 'UNIPROT:ADH1A_HUMAN' | |
226 #request._format = 'fasta' | |
227 # #request._style = 'raw' | |
228 # if inputNames!= None: | |
229 # for inName in inputs.keys(): | |
230 # if inName not in inputNames: | |
231 # raise TypeError, 'the input name is wrong!!!!' | |
232 # #return None | |
233 # else: | |
234 # setattr(inputClassInstance, inName, inputs[inName]) | |
235 | |
236 | |
237 #get dictionary of operation name:object(def) | |
238 opDict=self.path2Ops(modulePath) | |
239 | |
240 #get operation object(def) | |
241 opOb=opDict[opName] | |
242 | |
243 #invoke operation: response = dbfetchSrv.fetchData(request) | |
244 responseInstance=opOb(serviceInstance, inputClassInstance) | |
245 | |
246 #get output from outputmessage: result = response._fetchDataReturn | |
247 resultDict={} | |
248 resultDict=mshandler.flatten(responseInstance) #mshandler.msParser(responseInstance) | |
249 flat = nested2flatDict(resultDict) | |
250 # outputNames=self.opname2outputs(opName, modulePath) | |
251 # for out in outputNames: | |
252 # resultDict[out]=getattr(responseInstance, out) | |
253 | |
254 return flat#resultDict | |
255 #return responseInstance | |
256 | |
257 #testing this module | |
258 if __name__=="__main__": | |
259 test=ClientCreator() | |
260 | |
261 #picr web service | |
262 # print 'all operations of picr: \n', test.path2Ops('picr.AccessionMapperService_client').keys() | |
263 # print 'inputs of picr \n', test.opname2inputs('getUPIForSequence', 'picr.AccessionMapperService_client') | |
264 # print 'outputs of picr \n', test.opname2outputs('getUPIForSequence', 'picr.AccessionMapperService_client') | |
265 | |
266 | |
267 seq = """>Q8E5Q5_STRA3 | |
268 MKLSKRYRFWQKVIKALGVLALIATLVLVVYLYKLGILNDSNELKDLVHKYEFWGPMIFI | |
269 VAQIVQIVFPVIPGGVTTVAGFLIFGPTLGFIYNYIGIIIGSVILFWLVKFYGRKFVLLF | |
270 MDQKTFDKYESKLETSGYEKFFIFCMASPISPADIMVMITGLSNMSIKRFVTIIMITKPI | |
271 SIIGYSYLWIYGGDILKNFLN""" | |
272 # inp = {'_content': [{'_type': 'sequence', '_content': 'MKLSKRYRFWQKVIKALGVLALIATLVLVVYLYKLGILNDSNELKDLVHKYEFWGPMIFI'}, {'_type': 'sequence', '_content': 'MDQKTFDKYESKLETSGYEKFFIFCMASPISPADIMVMITGLSNMSIKRFVTIIMITKPI'}], '_params': {'_database': 'swissprot', '_email': 'chaitanya@gmail.com', '_program': 'blastp'}} | |
273 inp = {'_parameters': {'_stype': 'protein', '_sequence': 'MKLSKRYRFWQKVIKALGVLALIATLVLVVYLYKLGILNDSNELKDLVHKYEFWGPMIFI','_database':{ '_string' : ['uniprotkb','uniprotkb_swissprot']}, '_program': 'blastp'}, '_email': 'chaitanya@gmail.com'} | |
274 inputDict={'_params':{ '_program' : 'blastp', '_database' :'swissprot', '_email' :'riververy@yahoo.com', '_async': 1}, '_content':[{'_type':'sequence', '_content':'MKLSKRYRFWQKVIKALGVLALIATLVLVVYLYKLGILNDSNELKDLVHKYEFWGPMIFI'}]} | |
275 # inputDict = {} | |
276 inputs ={'_jobId':'wublast-S20110517-051830-0552-49531676-oy','_type':'out'} | |
277 inp = {} | |
278 #print test.invokeOp('run', 'wublast.wublast_services', inp) | |
279 print test.invokeOp('getParameters', 'wublast.wublast_services', inp) | |
280 #print test.opname2inputs('getResult','wublast.wublast_services') | |
281 # print 'all operations: \n', test.path2Ops('blast.WSWUBlast_client').keys() | |
282 # print 'inputs of runWUBlast \n', test.opname2inputs('runWUBlast', 'blast.WSWUBlast_client') | |
283 # print 'outputs of runWUBlast \n', test.opname2outputs('runWUBlast', 'blast.WSWUBlast_client') | |
284 | |
285 | |
286 # modul=test.path2Module('ebiDbfetch.WSDBFetchServerLegacyService_services') | |
287 # print dir(modul) | |
288 # print 'all operations: \n', test.path2Ops('ebiDbfetch.WSDBFetchServerLegacyService_services').keys() | |
289 # print 'inputs of fetchData:\n', test.opname2inputs('fetchData', 'ebiDbfetch.WSDBFetchServerLegacyService_services') | |
290 # print 'all classes:\n', test.path2Class('ebiDbfetch.WSDBFetchServerLegacyService_services').keys() | |
291 # print 'service instance ob:\n', test.path2service('ebiDbfetch.WSDBFetchServerLegacyService_services') | |
292 # print 'outputs of fetchData:\n', test.opname2outputs('fetchData', 'ebiDbfetch.WSDBFetchServerLegacyService_services') | |
293 # inputDict={'_format':'fasta', '_query':'UNIPROT:ADH1A_HUMAN', '_style':'raw'} | |
294 # print test.invokeOp('fetchData', 'ebiDbfetch.WSDBFetchServerLegacyService_services', inputDict).values()[0] | |
295 | |
296 # print 'all operations: \n', test.path2Ops('dbfetch.WSDBFetchServerLegacyService_services').keys() | |
297 # print 'inputs of fetchData:\n', test.opname2inputs('fetchData', 'dbfetch.WSDBFetchServerLegacyService_services') | |
298 # inputDict={'_format':'fasta', '_query':'UNIPROT:ADH1A_HUMAN', '_style':'raw'} | |
299 # print test.invokeOp('fetchData', 'dbfetch.WSDBFetchServerLegacyService_services', inputDict).values()[0] | |
300 | |
301 #dbfetch.WSDBFetchServerLegacyService_client | |
302 # print 'all operations: \n', test.path2Ops('dbfetch.WSDBFetchServerLegacyService_client').keys() | |
303 # print 'inputs of fetchData:\n', test.opname2inputs('fetchData', 'dbfetch.WSDBFetchServerLegacyService_client') | |
304 # inputDict={'_format':'fasta', '_query':'UNIPROT:ADH1A_HUMAN', '_style':'raw'} | |
305 # print test.invokeOp('fetchData', 'dbfetch.WSDBFetchServerLegacyService_client', inputDict).values()[0] | |
306 | |
307 | |
308 |