/usr/share/pyshared/ZSI/generate/wsdl2dispatch.py is in python-zsi 2.1~a1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | import inspect
from cStringIO import StringIO
import ZSI, string, sys, getopt, urlparse, types, warnings
from ZSI.wstools import WSDLTools
from ZSI.ServiceContainer import ServiceSOAPBinding, SimpleWSResource, WSAResource
from ZSI.generate import WsdlGeneratorError, Wsdl2PythonError
from utility import TextProtect, GetModuleBaseNameFromWSDL, \
NCName_to_ClassName, GetPartsSubNames, TextProtectAttributeName
from containers import BindingDescription
from wsdl2python import MessageWriter, WriteServiceModule,\
MessageTypecodeContainer, SchemaDescription
# Split last token
rsplit = lambda x,sep,: (x[:x.rfind(sep)], x[x.rfind(sep)+1:],)
if sys.version_info[0:2] == (2, 4, 0, 'final', 0)[0:2]:
rsplit = lambda x,sep,: x.rsplit(sep, 1)
class SOAPService:
def __init__(self, service):
self.classdef = StringIO()
self.initdef = StringIO()
self.location = ''
self.methods = []
def newMethod(self):
'''name -- operation name
'''
self.methods.append(StringIO())
return self.methods[-1]
class ServiceModuleWriter:
'''Creates a skeleton for a SOAP service instance.
'''
indent = ' '*4
server_module_suffix = '_server'
func_aname = TextProtectAttributeName
func_aname = staticmethod(func_aname)
separate_messages = False
def __init__(self, base=ServiceSOAPBinding, prefix='soap',
service_class=SOAPService):
'''
parameters:
base -- either a class definition, or a str representing a qualified
class name (eg. module.name.classname)
prefix -- method prefix.
'''
if inspect.isclass(base):
self.base_class_name = base.__name__
self.base_module_name = inspect.getmodule(base).__name__
else:
self.base_module_name, self.base_class_name = base.rsplit('.', 1)
self.wsdl = None
self.method_prefix = prefix
self._service_class = SOAPService
self.header = None
self.imports = None
self.messages = []
self._services = None
self.types_module_path = None
self.types_module_name = None
self.messages_module_name = None
def reset(self):
self.header = StringIO()
self.imports = StringIO()
self.message = []
self._services = {}
def getIndent(self, level=1):
'''return indent.
'''
assert 0 < level < 10, 'bad indent level %d' %level
return self.indent*level
def getMethodName(self, method):
'''return method name.
'''
return '%s_%s' %(self.method_prefix, TextProtect(method))
def getClassName(self, name):
'''return class name.
'''
return NCName_to_ClassName(name)
def setTypesModuleName(self, name):
self.types_module_name = name
# Backwards compatibility
setClientModuleName = setTypesModuleName
def getTypesModuleName(self):
'''return module name.
'''
assert self.wsdl is not None, 'initialize, call fromWSDL'
if self.types_module_name is not None:
return self.types_module_name
wsm = WriteServiceModule(self.wsdl)
return wsm.getTypesModuleName()
def getServiceModuleName(self):
'''return module name.
'''
name = GetModuleBaseNameFromWSDL(self.wsdl)
if not name:
raise WsdlGeneratorError, 'could not determine a service name'
if self.server_module_suffix is None:
return name
return '%s%s' %(name, self.server_module_suffix)
def getTypesModulePath(self):
return self.types_module_path
getClientModulePath = getTypesModulePath
def setTypesModulePath(self, path):
'''setup module path to where client module before calling fromWSDL.
'''
self.types_module_path = path
setClientModulePath = setTypesModulePath
def setUpClassDef(self, service):
'''set class definition and class variables.
service -- ServiceDescription instance
'''
assert isinstance(service, WSDLTools.Service) is True,\
'expecting WSDLTools.Service instance.'
s = self._services[service.name].classdef
print >>s, 'class %s(%s):' %(self.getClassName(service.name), self.base_class_name)
print >>s, '%ssoapAction = {}' % self.getIndent(level=1)
print >>s, '%sroot = {}' % self.getIndent(level=1)
def setUpImports(self):
'''set import statements
'''
i = self.imports
print >>i, 'from ZSI.schema import GED, GTD'
print >>i, 'from ZSI.TCcompound import ComplexType, Struct'
module = self.getTypesModuleName()
package = self.getTypesModulePath()
if package:
module = '%s.%s' %(package, module)
print >>i, 'from %s import *' %(module)
print >>i, 'from %s import %s' %(self.base_module_name, self.base_class_name)
def setUpInitDef(self, service):
'''set __init__ function
'''
assert isinstance(service, WSDLTools.Service), \
'expecting WSDLTools.Service instance.'
sd = self._services[service.name]
d = sd.initdef
if sd.location is not None:
scheme,netloc,path,params,query,fragment = urlparse.urlparse(sd.location)
print >>d, '%sdef __init__(self, post=\'%s\', **kw):' %(self.getIndent(level=1), path)
else:
print >>d, '%sdef __init__(self, post, **kw):' %self.getIndent(level=1)
# Require POST initialization value for test implementation
if self.base_module_name == inspect.getmodule(ServiceSOAPBinding).__name__:
print >>d, '%s%s.__init__(self, post)' %(self.getIndent(level=2), self.base_class_name)
return
# No POST initialization value, obtained from HTTP Request in twisted or wsgi
print >>d, '%s%s.__init__(self)' %(self.getIndent(level=2), self.base_class_name)
def mangle(self, name):
return TextProtect(name)
def getAttributeName(self, name):
return self.func_aname(name)
def setUpMethods(self, port):
'''set up all methods representing the port operations.
Parameters:
port -- Port that defines the operations.
'''
assert isinstance(port, WSDLTools.Port), \
'expecting WSDLTools.Port not: ' %type(port)
sd = self._services.get(port.getService().name)
assert sd is not None, 'failed to initialize.'
binding = port.getBinding()
portType = port.getPortType()
action_in = ''
for bop in binding.operations:
try:
op = portType.operations[bop.name]
except KeyError, ex:
raise WsdlGeneratorError,\
'Port(%s) PortType(%s) missing operation(%s) defined in Binding(%s)' \
%(port.name,portType.name,bop.name,binding.name)
for ext in bop.extensions:
if isinstance(ext, WSDLTools.SoapOperationBinding):
action_in = ext.soapAction
break
else:
warnings.warn('Port(%s) operation(%s) defined in Binding(%s) missing soapAction' \
%(port.name,op.name,binding.name)
)
msgin = op.getInputMessage()
msgin_name = TextProtect(msgin.name)
method_name = self.getMethodName(op.name)
m = sd.newMethod()
print >>m, '%sdef %s(self, ps, **kw):' %(self.getIndent(level=1), method_name)
if msgin is not None:
print >>m, '%srequest = ps.Parse(%s.typecode)' %(self.getIndent(level=2), msgin_name)
else:
print >>m, '%s# NO input' %self.getIndent(level=2)
msgout = op.getOutputMessage()
if msgout is not None:
msgout_name = TextProtect(msgout.name)
print >>m, '%sreturn request,%s()' %(self.getIndent(level=2), msgout_name)
else:
print >>m, '%s# NO output' % self.getIndent(level=2)
print >>m, '%sreturn request,None' % self.getIndent(level=2)
print >>m, ''
print >>m, '%ssoapAction[\'%s\'] = \'%s\'' %(self.getIndent(level=1), action_in, method_name)
print >>m, '%sroot[(%s.typecode.nspname,%s.typecode.pname)] = \'%s\'' \
%(self.getIndent(level=1), msgin_name, msgin_name, method_name)
return
def setUpHeader(self):
print >>self.header, '#'*50
print >>self.header, '# file: %s.py' %self.getServiceModuleName()
print >>self.header, '#'
print >>self.header, '# skeleton generated by "%s"' %self.__class__
print >>self.header, '# %s' %' '.join(sys.argv)
print >>self.header, '#'
print >>self.header, '#'*50
def write(self, fd=sys.stdout):
'''write out to file descriptor,
should not need to override.
'''
print >>fd, self.header.getvalue()
print >>fd, self.imports.getvalue()
print >>fd, '# Messages ',
for m in self.messages:
print >>fd, m
print >>fd, ''
print >>fd, ''
print >>fd, '# Service Skeletons'
for k,v in self._services.items():
print >>fd, v.classdef.getvalue()
print >>fd, v.initdef.getvalue()
for s in v.methods:
print >>fd, s.getvalue()
def fromWSDL(self, wsdl):
'''setup the service description from WSDL,
should not need to override.
'''
assert isinstance(wsdl, WSDLTools.WSDL), 'expecting WSDL instance'
if len(wsdl.services) == 0:
raise WsdlGeneratorError, 'No service defined'
self.reset()
self.wsdl = wsdl
self.setUpHeader()
self.setUpImports()
for service in wsdl.services:
sd = self._service_class(service.name)
self._services[service.name] = sd
for port in service.ports:
desc = BindingDescription(wsdl=wsdl)
try:
desc.setUp(port.getBinding())
except Wsdl2PythonError, ex:
continue
for soc in desc.operations:
if not soc.hasInput(): continue
self.messages.append(MessageWriter())
self.messages[-1].setUp(soc, port, input=True)
if soc.hasOutput():
self.messages.append(MessageWriter())
self.messages[-1].setUp(soc, port, input=False)
for e in port.extensions:
if isinstance(e, WSDLTools.SoapAddressBinding):
sd.location = e.location
self.setUpMethods(port)
self.setUpClassDef(service)
self.setUpInitDef(service)
class WSAServiceModuleWriter(ServiceModuleWriter):
'''Creates a skeleton for a WS-Address service instance.
'''
def __init__(self, base=WSAResource, prefix='wsa', service_class=SOAPService,
strict=True):
'''
Parameters:
strict -- check that soapAction and input ws-action do not collide.
'''
ServiceModuleWriter.__init__(self, base, prefix, service_class)
self.strict = strict
def createMethodBody(msgInName, msgOutName, **kw):
'''return a tuple of strings containing the body of a method.
msgInName -- None or a str
msgOutName -- None or a str
'''
body = []
if msgInName is not None:
body.append('request = ps.Parse(%s.typecode)' %msgInName)
if msgOutName is not None:
body.append('return request,%s()' %msgOutName)
else:
body.append('return request,None')
return tuple(body)
createMethodBody = staticmethod(createMethodBody)
def setUpClassDef(self, service):
'''use soapAction dict for WS-Action input, setup wsAction
dict for grabbing WS-Action output values.
'''
assert isinstance(service, WSDLTools.Service), \
'expecting WSDLTools.Service instance'
s = self._services[service.name].classdef
print >>s, 'class %s(%s):' %(self.getClassName(service.name), self.base_class_name)
print >>s, '%ssoapAction = {}' % self.getIndent(level=1)
print >>s, '%swsAction = {}' % self.getIndent(level=1)
print >>s, '%sroot = {}' % self.getIndent(level=1)
def setUpMethods(self, port):
'''set up all methods representing the port operations.
Parameters:
port -- Port that defines the operations.
'''
assert isinstance(port, WSDLTools.Port), \
'expecting WSDLTools.Port not: ' %type(port)
binding = port.getBinding()
portType = port.getPortType()
service = port.getService()
s = self._services[service.name]
for bop in binding.operations:
try:
op = portType.operations[bop.name]
except KeyError, ex:
raise WsdlGeneratorError,\
'Port(%s) PortType(%s) missing operation(%s) defined in Binding(%s)' \
%(port.name, portType.name, op.name, binding.name)
soap_action = wsaction_in = wsaction_out = None
if op.input is not None:
wsaction_in = op.getInputAction()
if op.output is not None:
wsaction_out = op.getOutputAction()
for ext in bop.extensions:
if isinstance(ext, WSDLTools.SoapOperationBinding) is False: continue
soap_action = ext.soapAction
if not soap_action: break
if wsaction_in is None: break
if wsaction_in == soap_action: break
if self.strict is False:
warnings.warn(\
'Port(%s) operation(%s) in Binding(%s) soapAction(%s) != WS-Action(%s)' \
%(port.name, op.name, binding.name, soap_action, wsaction_in),
)
break
raise WsdlGeneratorError,\
'Port(%s) operation(%s) in Binding(%s) soapAction(%s) MUST match WS-Action(%s)' \
%(port.name, op.name, binding.name, soap_action, wsaction_in)
method_name = self.getMethodName(op.name)
m = s.newMethod()
print >>m, '%sdef %s(self, ps, address):' %(self.getIndent(level=1), method_name)
msgin_name = msgout_name = None
msgin,msgout = op.getInputMessage(),op.getOutputMessage()
if msgin is not None:
msgin_name = TextProtect(msgin.name)
if msgout is not None:
msgout_name = TextProtect(msgout.name)
indent = self.getIndent(level=2)
for l in self.createMethodBody(msgin_name, msgout_name):
print >>m, indent + l
print >>m, ''
print >>m, '%ssoapAction[\'%s\'] = \'%s\'' %(self.getIndent(level=1), wsaction_in, method_name)
print >>m, '%swsAction[\'%s\'] = \'%s\'' %(self.getIndent(level=1), method_name, wsaction_out)
print >>m, '%sroot[(%s.typecode.nspname,%s.typecode.pname)] = \'%s\'' \
%(self.getIndent(level=1), msgin_name, msgin_name, method_name)
|