<s> import inspect <EOL> from cStringIO import StringIO <EOL> import sys , urlparse , warnings <EOL> from pysphere . ZSI . wstools import WSDLTools <EOL> from pysphere . ZSI . ServiceContainer import ServiceSOAPBinding , WSAResource <EOL> from pysphere . ZSI . generate import WsdlGeneratorError , Wsdl2PythonError <EOL> from pysphere . ZSI . generate . utility import TextProtect , GetModuleBaseNameFromWSDL , NCName_to_ClassName , TextProtectAttributeName <EOL> from pysphere . ZSI . generate . containers import BindingDescription <EOL> from pysphere . ZSI . generate . wsdl2python import MessageWriter , WriteServiceModule <EOL> rsplit = lambda x , sep , : ( x [ : x . rfind ( sep ) ] , x [ x . rfind ( sep ) + <NUM_LIT:1> : ] , ) <EOL> if sys . version_info [ <NUM_LIT:0> : <NUM_LIT:2> ] == ( <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:0> , '<STR_LIT>' , <NUM_LIT:0> ) [ <NUM_LIT:0> : <NUM_LIT:2> ] : <EOL> rsplit = lambda x , sep , : x . rsplit ( sep , <NUM_LIT:1> ) <EOL> class SOAPService : <EOL> def __init__ ( self , service ) : <EOL> self . classdef = StringIO ( ) <EOL> self . initdef = StringIO ( ) <EOL> self . location = '<STR_LIT>' <EOL> self . methods = [ ] <EOL> def newMethod ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . methods . append ( StringIO ( ) ) <EOL> return self . methods [ - <NUM_LIT:1> ] <EOL> class ServiceModuleWriter : <EOL> '''<STR_LIT>''' <EOL> indent = '<STR_LIT:U+0020>' * <NUM_LIT:4> <EOL> server_module_suffix = '<STR_LIT>' <EOL> func_aname = TextProtectAttributeName <EOL> func_aname = staticmethod ( func_aname ) <EOL> separate_messages = False <EOL> def __init__ ( self , base = ServiceSOAPBinding , prefix = '<STR_LIT>' , <EOL> service_class = SOAPService ) : <EOL> '''<STR_LIT>''' <EOL> if inspect . isclass ( base ) : <EOL> self . base_class_name = base . __name__ <EOL> self . base_module_name = inspect . getmodule ( base ) . __name__ <EOL> else : <EOL> self . base_module_name , self . base_class_name = base . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> self . wsdl = None <EOL> self . method_prefix = prefix <EOL> self . _service_class = SOAPService <EOL> self . header = None <EOL> self . imports = None <EOL> self . messages = [ ] <EOL> self . _services = None <EOL> self . types_module_path = None <EOL> self . types_module_name = None <EOL> self . messages_module_name = None <EOL> def reset ( self ) : <EOL> self . header = StringIO ( ) <EOL> self . imports = StringIO ( ) <EOL> self . message = [ ] <EOL> self . _services = { } <EOL> def getIndent ( self , level = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> assert <NUM_LIT:0> < level < <NUM_LIT:10> , '<STR_LIT>' % level <EOL> return self . indent * level <EOL> def getMethodName ( self , method ) : <EOL> '''<STR_LIT>''' <EOL> return '<STR_LIT>' % ( self . method_prefix , TextProtect ( method ) ) <EOL> def getClassName ( self , name ) : <EOL> '''<STR_LIT>''' <EOL> return NCName_to_ClassName ( name ) <EOL> def setTypesModuleName ( self , name ) : <EOL> self . types_module_name = name <EOL> setClientModuleName = setTypesModuleName <EOL> def getTypesModuleName ( self ) : <EOL> '''<STR_LIT>''' <EOL> assert self . wsdl is not None , '<STR_LIT>' <EOL> if self . types_module_name is not None : <EOL> return self . types_module_name <EOL> wsm = WriteServiceModule ( self . wsdl ) <EOL> return wsm . getTypesModuleName ( ) <EOL> def getServiceModuleName ( self ) : <EOL> '''<STR_LIT>''' <EOL> name = GetModuleBaseNameFromWSDL ( self . wsdl ) <EOL> if not name : <EOL> raise WsdlGeneratorError ( '<STR_LIT>' ) <EOL> if self . server_module_suffix is None : <EOL> return name <EOL> return '<STR_LIT>' % ( name , self . server_module_suffix ) <EOL> def getTypesModulePath ( self ) : <EOL> return self . types_module_path <EOL> getClientModulePath = getTypesModulePath <EOL> def setTypesModulePath ( self , path ) : <EOL> '''<STR_LIT>''' <EOL> self . types_module_path = path <EOL> setClientModulePath = setTypesModulePath <EOL> def setUpClassDef ( self , service ) : <EOL> '''<STR_LIT>''' <EOL> assert isinstance ( service , WSDLTools . Service ) is True , '<STR_LIT>' <EOL> s = self . _services [ service . name ] . classdef <EOL> print >> s , '<STR_LIT>' % ( self . getClassName ( service . name ) , self . base_class_name ) <EOL> print >> s , '<STR_LIT>' % self . getIndent ( level = <NUM_LIT:1> ) <EOL> print >> s , '<STR_LIT>' % self . getIndent ( level = <NUM_LIT:1> ) <EOL> def setUpImports ( self ) : <EOL> '''<STR_LIT>''' <EOL> i = self . imports <EOL> print >> i , '<STR_LIT>' <EOL> print >> i , '<STR_LIT>' <EOL> module = self . getTypesModuleName ( ) <EOL> package = self . getTypesModulePath ( ) <EOL> if package : <EOL> module = '<STR_LIT>' % ( package , module ) <EOL> print >> i , '<STR_LIT>' % ( module ) <EOL> print >> i , '<STR_LIT>' % ( self . base_module_name , self . base_class_name ) <EOL> def setUpInitDef ( self , service ) : <EOL> '''<STR_LIT>''' <EOL> assert isinstance ( service , WSDLTools . Service ) , '<STR_LIT>' <EOL> sd = self . _services [ service . name ] <EOL> d = sd . initdef <EOL> if sd . location is not None : <EOL> _ , _ , path , _ , _ , _ = urlparse . urlparse ( sd . location ) <EOL> print >> d , '<STR_LIT>' % ( self . getIndent ( level = <NUM_LIT:1> ) , path ) <EOL> else : <EOL> print >> d , '<STR_LIT>' % self . getIndent ( level = <NUM_LIT:1> ) <EOL> if self . base_module_name == inspect . getmodule ( ServiceSOAPBinding ) . __name__ : <EOL> print >> d , '<STR_LIT>' % ( self . getIndent ( level = <NUM_LIT:2> ) , self . base_class_name ) <EOL> return <EOL> print >> d , '<STR_LIT>' % ( self . getIndent ( level = <NUM_LIT:2> ) , self . base_class_name ) <EOL> def mangle ( self , name ) : <EOL> return TextProtect ( name ) <EOL> def getAttributeName ( self , name ) : <EOL> return self . func_aname ( name ) <EOL> def setUpMethods ( self , port ) : <EOL> '''<STR_LIT>''' <EOL> assert isinstance ( port , WSDLTools . Port ) , '<STR_LIT>' % type ( port ) <EOL> sd = self . _services . get ( port . getService ( ) . name ) <EOL> assert sd is not None , '<STR_LIT>' <EOL> binding = port . getBinding ( ) <EOL> portType = port . getPortType ( ) <EOL> action_in = '<STR_LIT>' <EOL> for bop in binding . operations : <EOL> try : <EOL> op = portType . operations [ bop . name ] <EOL> except KeyError : <EOL> raise WsdlGeneratorError ( <EOL> '<STR_LIT>' <EOL> % ( port . name , portType . name , bop . name , binding . name ) ) <EOL> for ext in bop . extensions : <EOL> if isinstance ( ext , WSDLTools . SoapOperationBinding ) : <EOL> action_in = ext . soapAction <EOL> break <EOL> else : <EOL> warnings . warn ( '<STR_LIT>' % ( port . name , op . name , binding . name ) <EOL> ) <EOL> msgin = op . getInputMessage ( ) <EOL> msgin_name = TextProtect ( msgin . name ) <EOL> method_name = self . getMethodName ( op . name ) <EOL> m = sd . newMethod ( ) <EOL> print >> m , '<STR_LIT>' % ( self . getIndent ( level = <NUM_LIT:1> ) , method_name ) <EOL> if msgin is not None : <EOL> print >> m , '<STR_LIT>' % ( self . getIndent ( level = <NUM_LIT:2> ) , msgin_name ) <EOL> else : <EOL> print >> m , '<STR_LIT>' % self . getIndent ( level = <NUM_LIT:2> ) <EOL> msgout = op . getOutputMessage ( ) <EOL> if msgout is not None : <EOL> msgout_name = TextProtect ( msgout . name ) <EOL> print >> m , '<STR_LIT>' % ( self . getIndent ( level = <NUM_LIT:2> ) , msgout_name ) <EOL> else : <EOL> print >> m , '<STR_LIT>' % self . getIndent ( level = <NUM_LIT:2> ) <EOL> print >> m , '<STR_LIT>' % self . getIndent ( level = <NUM_LIT:2> ) <EOL> print >> m , '<STR_LIT>' <EOL> print >> m , '<STR_LIT>' % ( self . getIndent ( level = <NUM_LIT:1> ) , action_in , method_name ) <EOL> print >> m , '<STR_LIT>' % ( self . getIndent ( level = <NUM_LIT:1> ) , msgin_name , msgin_name , method_name ) <EOL> return <EOL> def setUpHeader ( self ) : <EOL> print >> self . header , '<STR_LIT:#>' * <NUM_LIT:50> <EOL> print >> self . header , '<STR_LIT>' % self . getServiceModuleName ( ) <EOL> print >> self . header , '<STR_LIT:#>' <EOL> print >> self . header , '<STR_LIT>' % self . __class__ <EOL> print >> self . header , '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( sys . argv ) <EOL> print >> self . header , '<STR_LIT:#>' <EOL> print >> self . header , '<STR_LIT:#>' * <NUM_LIT:50> <EOL> def write ( self , fd = sys . stdout ) : <EOL> '''<STR_LIT>''' <EOL> print >> fd , self . header . getvalue ( ) <EOL> print >> fd , self . imports . getvalue ( ) <EOL> print >> fd , '<STR_LIT>' , <EOL> for m in self . messages : <EOL> print >> fd , m <EOL> print >> fd , '<STR_LIT>' <EOL> print >> fd , '<STR_LIT>' <EOL> print >> fd , '<STR_LIT>' <EOL> for v in self . _services . values ( ) : <EOL> print >> fd , v . classdef . getvalue ( ) <EOL> print >> fd , v . initdef . getvalue ( ) <EOL> for s in v . methods : <EOL> print >> fd , s . getvalue ( ) <EOL> def fromWSDL ( self , wsdl ) : <EOL> '''<STR_LIT>''' <EOL> assert isinstance ( wsdl , WSDLTools . WSDL ) , '<STR_LIT>' <EOL> if len ( wsdl . services ) == <NUM_LIT:0> : <EOL> raise WsdlGeneratorError ( '<STR_LIT>' ) <EOL> self . reset ( ) <EOL> self . wsdl = wsdl <EOL> self . setUpHeader ( ) <EOL> self . setUpImports ( ) <EOL> for service in wsdl . services : <EOL> sd = self . _service_class ( service . name ) <EOL> self . _services [ service . name ] = sd <EOL> for port in service . ports : <EOL> desc = BindingDescription ( wsdl = wsdl ) <EOL> try : <EOL> desc . setUp ( port . getBinding ( ) ) <EOL> except Wsdl2PythonError : <EOL> continue <EOL> for soc in desc . operations : <EOL> if not soc . hasInput ( ) : continue <EOL> self . messages . append ( MessageWriter ( ) ) <EOL> self . messages [ - <NUM_LIT:1> ] . setUp ( soc , port , input = True ) <EOL> if soc . hasOutput ( ) : <EOL> self . messages . append ( MessageWriter ( ) ) <EOL> self . messages [ - <NUM_LIT:1> ] . setUp ( soc , port , input = False ) <EOL> for e in port . extensions : <EOL> if isinstance ( e , WSDLTools . SoapAddressBinding ) : <EOL> sd . location = e . location <EOL> self . setUpMethods ( port ) <EOL> self . setUpClassDef ( service ) <EOL> self . setUpInitDef ( service ) <EOL> class WSAServiceModuleWriter ( ServiceModuleWriter ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , base = WSAResource , prefix = '<STR_LIT>' , service_class = SOAPService , <EOL> strict = True ) : <EOL> '''<STR_LIT>''' <EOL> ServiceModuleWriter . __init__ ( self , base , prefix , service_class ) <EOL> self . strict = strict <EOL> def createMethodBody ( msgInName , msgOutName , ** kw ) : <EOL> '''<STR_LIT>''' <EOL> body = [ ] <EOL> if msgInName is not None : <EOL> body . append ( '<STR_LIT>' % msgInName ) <EOL> if msgOutName is not None : <EOL> body . append ( '<STR_LIT>' % msgOutName ) <EOL> else : <EOL> body . append ( '<STR_LIT>' ) <EOL> return tuple ( body ) <EOL> createMethodBody = staticmethod ( createMethodBody ) <EOL> def setUpClassDef ( self , service ) : <EOL> '''<STR_LIT>''' <EOL> assert isinstance ( service , WSDLTools . Service ) , '<STR_LIT>' <EOL> s = self . _services [ service . name ] . classdef <EOL> print >> s , '<STR_LIT>' % ( self . getClassName ( service . name ) , self . base_class_name ) <EOL> print >> s , '<STR_LIT>' % self . getIndent ( level = <NUM_LIT:1> ) <EOL> print >> s , '<STR_LIT>' % self . getIndent ( level = <NUM_LIT:1> ) <EOL> print >> s , '<STR_LIT>' % self . getIndent ( level = <NUM_LIT:1> ) <EOL> def setUpMethods ( self , port ) : <EOL> '''<STR_LIT>''' <EOL> assert isinstance ( port , WSDLTools . Port ) , '<STR_LIT>' % type ( port ) <EOL> binding = port . getBinding ( ) <EOL> portType = port . getPortType ( ) <EOL> service = port . getService ( ) <EOL> s = self . _services [ service . name ] <EOL> for bop in binding . operations : <EOL> try : <EOL> op = portType . operations [ bop . name ] <EOL> except KeyError : <EOL> raise WsdlGeneratorError ( <EOL> '<STR_LIT>' <EOL> % ( port . name , portType . name , op . name , binding . name ) ) <EOL> soap_action = wsaction_in = wsaction_out = None <EOL> if op . input is not None : <EOL> wsaction_in = op . getInputAction ( ) <EOL> if op . output is not None : <EOL> wsaction_out = op . getOutputAction ( ) <EOL> for ext in bop . extensions : <EOL> if isinstance ( ext , WSDLTools . SoapOperationBinding ) is False : continue <EOL> soap_action = ext . soapAction <EOL> if not soap_action : break <EOL> if wsaction_in is None : break <EOL> if wsaction_in == soap_action : break <EOL> if self . strict is False : <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> % ( port . name , op . name , binding . name , soap_action , wsaction_in ) , <EOL> ) <EOL> break <EOL> raise WsdlGeneratorError ( <EOL> '<STR_LIT>' <EOL> % ( port . name , op . name , binding . name , soap_action , wsaction_in ) ) <EOL> method_name = self . getMethodName ( op . name ) <EOL> m = s . newMethod ( ) <EOL> print >> m , '<STR_LIT>' % ( self . getIndent ( level = <NUM_LIT:1> ) , method_name ) <EOL> msgin_name = msgout_name = None <EOL> msgin , msgout = op . getInputMessage ( ) , op . getOutputMessage ( ) <EOL> if msgin is not None : <EOL> msgin_name = TextProtect ( msgin . name ) <EOL> if msgout is not None : <EOL> msgout_name = TextProtect ( msgout . name ) <EOL> indent = self . getIndent ( level = <NUM_LIT:2> ) <EOL> for l in self . createMethodBody ( msgin_name , msgout_name ) : <EOL> print >> m , indent + l <EOL> print >> m , '<STR_LIT>' <EOL> print >> m , '<STR_LIT>' % ( self . getIndent ( level = <NUM_LIT:1> ) , wsaction_in , method_name ) <EOL> print >> m , '<STR_LIT>' % ( self . getIndent ( level = <NUM_LIT:1> ) , method_name , wsaction_out ) <EOL> print >> m , '<STR_LIT>' % ( self . getIndent ( level = <NUM_LIT:1> ) , msgin_name , msgin_name , method_name ) </s>
