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.raw_wsdl = wsdl.document.toxml().replace("\"", "\\\"") 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: for e in port.extensions: if isinstance(e, WSDLTools.SoapAddressBinding): sd.location = e.location self.setUpMethods(port) self.setUpClassDef(service) self.setUpInitDef(service)
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: 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('%sdef %s(self, ps, **kw):' %(self.getIndent(level=1), method_name), file=m) if msgin is not None: print('%srequest = ps.Parse(%s.typecode)' %(self.getIndent(level=2), msgin_name), file=m) else: print('%s# NO input' %self.getIndent(level=2), file=m) msgout = op.getOutputMessage() if msgout is not None: msgout_name = TextProtect(msgout.name) print('%sreturn request,%s()' %(self.getIndent(level=2), msgout_name), file=m) else: print('%s# NO output' % self.getIndent(level=2), file=m) print('%sreturn request,None' % self.getIndent(level=2), file=m) print('', file=m) print('%ssoapAction[\'%s\'] = \'%s\'' %(self.getIndent(level=1), action_in, method_name), file=m) print('%sroot[(%s.typecode.nspname,%s.typecode.pname)] = \'%s\'' \ %(self.getIndent(level=1), msgin_name, msgin_name, method_name), file=m) return
def getAlias(cls, ns): if ns in cls.alias_dict: return cls.alias_dict[ns][1] msg = 'failed to find import for schema "%s"'%ns +\ 'possibly missing @schemaLocation attribute.' if ns in SCHEMA.XSD_LIST: msg = 'missing built-in typecode for schema "%s"' % ns raise WsdlGeneratorError(msg)
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 getClientModuleName(self): """client module name. """ name = GetModuleBaseNameFromWSDL(self._wsdl) if not name: raise WsdlGeneratorError('could not determine a service name') if self.client_module_suffix is None: return name return '%s%s' %(name, self.client_module_suffix)
def getMessagesModuleName(self): name = GetModuleBaseNameFromWSDL(self._wsdl) if not name: raise WsdlGeneratorError('could not determine a service name') if self.messages_module_suffix is None: return name if len(self.messages_module_suffix) == 0: return self.getClientModuleName() return '%s%s' %(name, self.messages_module_suffix)
def getTypesModuleName(self): """types module name. """ if self.types_module_name is not None: return self.types_module_name name = GetModuleBaseNameFromWSDL(self._wsdl) if not name: raise WsdlGeneratorError('could not determine a service name') if self.types_module_suffix is None: return name return '%s%s' %(name, self.types_module_suffix)
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: 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)
def setUp(self, soc, port, input=False): assert isinstance(soc, ServiceOperationContainer),\ 'expecting a ServiceOperationContainer instance' assert isinstance(port, WSDLTools.Port),\ 'expecting a WSDL.Port instance' rpc,literal = soc.isRPC(), soc.isLiteral(input) kw,klass = {}, None if rpc and literal: klass = ServiceRPCLiteralMessageContainer elif not rpc and literal: kw['do_extended'] = self.do_extended klass = ServiceDocumentLiteralMessageContainer elif rpc and not literal: klass = ServiceRPCEncodedMessageContainer else: raise WsdlGeneratorError('doc/enc not supported.') self.content = klass(**kw) self.content.setUp(port, soc, input)
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 as 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 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('%sdef %s(self, ps, address):' % (self.getIndent(level=1), method_name), file=m) 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(indent + l, file=m) print('', file=m) print('%ssoapAction[\'%s\'] = \'%s\'' % (self.getIndent(level=1), wsaction_in, method_name), file=m) print('%swsAction[\'%s\'] = \'%s\'' % (self.getIndent(level=1), method_name, wsaction_out), file=m) print('%sroot[(%s.typecode.nspname,%s.typecode.pname)] = \'%s\'' \ %(self.getIndent(level=1), msgin_name, msgin_name, method_name), file=m)
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 as 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('%sdef %s(self, ps):' % (self.getIndent(level=1), method_name), file=m) if msgin is not None: print('%sself.request = ps.Parse(%s.typecode)' % (self.getIndent(level=2), msgin_name), file=m) else: print('%s# NO input' % self.getIndent(level=2), file=m) msgout = op.getOutputMessage() if self.do_extended: input_args = list(msgin.parts.values()) iargs = ["%s" % x.name for x in input_args] if msgout is not None: output_args = list(msgout.parts.values()) else: output_args = [] oargs = ["%s" % x.name for x in output_args] if output_args: if len(output_args) > 1: print( "Message has more than one return value (Bad Design)." ) output_args = "(%s)" % output_args else: output_args = "" # Get arg list iSubNames = GetPartsSubNames(list(msgin.parts.values()), self.wsdl) for i in range( len(iargs) ): # should only be one part to messages here anyway argSubNames = iSubNames[i] if len(argSubNames) > 0: subNamesStr = "self.request." + ", self.request.".join( map(self.getAttributeName, argSubNames)) if len(argSubNames) > 1: subNamesStr = "(" + subNamesStr + ")" print("%s%s = %s" % (self.getIndent(level=2), iargs[i], subNamesStr), file=m) print("\n%s# If we have an implementation object use it" % (self.getIndent(level=2)), file=m) print("%sif hasattr(self,'impl'):" % (self.getIndent(level=2)), file=m) iargsStrList = [] for arg in iargs: argSubNames = iSubNames[i] if len(argSubNames) > 0: if len(argSubNames) > 1: for i in range(len(argSubNames)): iargsStrList.append(arg + "[%i]" % i) else: iargsStrList.append(arg) iargsStr = ",".join(iargsStrList) oargsStr = ", ".join(oargs) if len(oargsStr) > 0: oargsStr += " = " print("%s%sself.impl.%s(%s)" % (self.getIndent(level=3), oargsStr, op.name, iargsStr), file=m) if msgout is not None: msgout_name = TextProtect(msgout.name) if self.do_extended: print('\n%sresult = %s()' % (self.getIndent(level=2), msgout_name), file=m) oSubNames = GetPartsSubNames(list(msgout.parts.values()), self.wsdl) if (len(oSubNames) > 0) and (len(oSubNames[0]) > 0): print( "%s# If we have an implementation object, copy the result " % (self.getIndent(level=2)), file=m) print("%sif hasattr(self,'impl'):" % (self.getIndent(level=2)), file=m) # copy result's members for i in range( len(oargs) ): # should only be one part messages here anyway oargSubNames = oSubNames[i] if len(oargSubNames) > 1: print('%s# Should have a tuple of %i args' % (self.getIndent(level=3), len(oargSubNames)), file=m) for j in range(len(oargSubNames)): oargSubName = oargSubNames[j] print('%sresult.%s = %s[%i]' % (self.getIndent(level=3), self.getAttributeName(oargSubName), oargs[i], j), file=m) elif len(oargSubNames) == 1: oargSubName = oargSubNames[0] print('%sresult.%s = %s' % (self.getIndent(level=3), self.getAttributeName(oargSubName), oargs[i]), file=m) else: raise Exception( "The subnames within message " + msgout_name + "'s part were not found. Message is the output of operation " + op.name) print('%sreturn result' % (self.getIndent(level=2)), file=m) else: print('%sreturn %s()' % (self.getIndent(level=2), msgout_name), file=m) else: print('%s# NO output' % self.getIndent(level=2), file=m) print('%sreturn None' % self.getIndent(level=2), file=m) print('', file=m) print('%ssoapAction[\'%s\'] = \'%s\'' % (self.getIndent(level=1), action_in, method_name), file=m) print('%sroot[(%s.typecode.nspname,%s.typecode.pname)] = \'%s\'' \ %(self.getIndent(level=1), msgin_name, msgin_name, method_name), file=m) return