<s> import weakref , re , os , sys <EOL> from ConfigParser import SafeConfigParser as ConfigParser <EOL> from pysphere . ZSI import TC <EOL> from pysphere . ZSI . client import _Binding <EOL> from pysphere . ZSI . generate import commands , containers <EOL> from pysphere . ZSI . schema import GED , GTD <EOL> import pysphere . ZSI . wstools as wstools <EOL> def _urn_to_module ( urn ) : return '<STR_LIT>' % re . sub ( _urn_to_module . regex , '<STR_LIT:_>' , urn ) <EOL> _urn_to_module . regex = re . compile ( r'<STR_LIT>' ) <EOL> class ServiceProxy : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , wsdl , url = None , service = None , port = None , <EOL> cachedir = os . path . join ( os . path . expanduser ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> asdict = True , lazy = False , pyclass = False , force = False , ** kw ) : <EOL> """<STR_LIT>""" <EOL> self . _asdict = asdict <EOL> self . _url = url <EOL> self . _kw = kw <EOL> self . _wsdl = wstools . WSDLTools . WSDLReader ( ) . loadFromURL ( wsdl ) <EOL> self . _service = self . _wsdl . services [ service or <NUM_LIT:0> ] <EOL> self . __doc__ = self . _service . documentation <EOL> self . _port = self . _service . ports [ port or <NUM_LIT:0> ] <EOL> self . _name = self . _service . name <EOL> self . _methods = { } <EOL> self . _cachedir = cachedir <EOL> self . _lazy = lazy <EOL> self . _pyclass = pyclass <EOL> self . _force = force <EOL> port = self . _port <EOL> for port in self . _service . ports : <EOL> for item in port . getPortType ( ) . operations : <EOL> try : <EOL> callinfo = wstools . WSDLTools . callInfoFromWSDL ( port , item . name ) <EOL> except : <EOL> continue <EOL> method = MethodProxy ( self , callinfo ) <EOL> setattr ( self , item . name , method ) <EOL> self . _methods . setdefault ( item . name , [ ] ) . append ( method ) <EOL> self . _mod = self . _load ( wsdl ) <EOL> def _load ( self , location ) : <EOL> """<STR_LIT>""" <EOL> cachedir = self . _cachedir <EOL> if not os . path . isdir ( cachedir ) : os . mkdir ( cachedir ) <EOL> _file = os . path . join ( cachedir , '<STR_LIT>' ) <EOL> section = '<STR_LIT>' <EOL> cp = ConfigParser ( ) <EOL> try : <EOL> cp . readfp ( open ( _file , '<STR_LIT:r>' ) ) <EOL> except IOError : <EOL> del cp ; cp = None <EOL> option = location . replace ( '<STR_LIT::>' , '<STR_LIT:->' ) <EOL> if ( not self . _force and cp is not None and cp . has_section ( section ) and <EOL> cp . has_option ( section , option ) ) : <EOL> types = cp . get ( section , option ) <EOL> else : <EOL> if not self . _pyclass : <EOL> containers . ContainerBase . func_aname = lambda instnc , n : str ( n ) <EOL> args = [ '<STR_LIT>' , cachedir , location ] <EOL> if self . _lazy : args . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> if self . _pyclass : args . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> files = commands . wsdl2py ( args ) <EOL> if cp is None : cp = ConfigParser ( ) <EOL> if not cp . has_section ( section ) : cp . add_section ( section ) <EOL> types = [ f for f in files if f . endswith ( '<STR_LIT>' ) ] [ <NUM_LIT:0> ] <EOL> cp . set ( section , option , types ) <EOL> cp . write ( open ( _file , '<STR_LIT:w>' ) ) <EOL> if os . path . abspath ( cachedir ) not in sys . path : <EOL> sys . path . append ( os . path . abspath ( cachedir ) ) <EOL> mod = os . path . split ( types ) [ - <NUM_LIT:1> ] . rstrip ( '<STR_LIT>' ) <EOL> return __import__ ( mod ) <EOL> def _load_schema ( self , location , xml = None ) : <EOL> """<STR_LIT>""" <EOL> cachedir = self . _cachedir <EOL> if not os . path . isdir ( cachedir ) : os . mkdir ( cachedir ) <EOL> _file = os . path . join ( cachedir , '<STR_LIT>' ) <EOL> section = '<STR_LIT>' <EOL> cp = ConfigParser ( ) <EOL> try : <EOL> cp . readfp ( open ( _file , '<STR_LIT:r>' ) ) <EOL> except IOError : <EOL> del cp ; cp = None <EOL> option = location . replace ( '<STR_LIT::>' , '<STR_LIT:->' ) <EOL> if ( cp is not None and cp . has_section ( section ) and <EOL> cp . has_option ( section , option ) ) : <EOL> types = cp . get ( section , option ) <EOL> else : <EOL> if not self . _pyclass : <EOL> containers . ContainerBase . func_aname = lambda instnc , n : str ( n ) <EOL> from pysphere . ZSI . wstools import XMLSchema <EOL> reader = XMLSchema . SchemaReader ( base_url = location ) <EOL> if xml is not None and isinstance ( xml , basestring ) : <EOL> schema = reader . loadFromString ( xml ) <EOL> elif xml is not None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> elif not os . path . isfile ( location ) : <EOL> schema = reader . loadFromURL ( location ) <EOL> else : <EOL> schema = reader . reader . loadFromFile ( location ) <EOL> class options : <EOL> output_dir = cachedir <EOL> schema = True <EOL> simple_naming = False <EOL> address = False <EOL> lazy = self . _lazy <EOL> complexType = self . _pyclass <EOL> schema . location = location <EOL> files = commands . _wsdl2py ( options , schema ) <EOL> if cp is None : cp = ConfigParser ( ) <EOL> if not cp . has_section ( section ) : cp . add_section ( section ) <EOL> types = [ f for f in files if f . endswith ( '<STR_LIT>' ) ] [ <NUM_LIT:0> ] <EOL> cp . set ( section , option , types ) <EOL> cp . write ( open ( _file , '<STR_LIT:w>' ) ) <EOL> if os . path . abspath ( cachedir ) not in sys . path : <EOL> sys . path . append ( os . path . abspath ( cachedir ) ) <EOL> mod = os . path . split ( types ) [ - <NUM_LIT:1> ] . rstrip ( '<STR_LIT>' ) <EOL> return __import__ ( mod ) <EOL> def _call ( self , name , soapheaders ) : <EOL> """<STR_LIT>""" <EOL> def call_closure ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) and len ( kwargs ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if len ( args ) > <NUM_LIT:0> : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if len ( kwargs ) : <EOL> args = kwargs <EOL> callinfo = getattr ( self , name ) . callinfo <EOL> for method in self . _methods [ name ] : <EOL> if len ( method . callinfo . inparams ) == len ( kwargs ) : <EOL> callinfo = method . callinfo <EOL> binding = _Binding ( url = self . _url or callinfo . location , <EOL> soapaction = callinfo . soapAction , <EOL> ** self . _kw ) <EOL> kw = dict ( unique = True ) <EOL> if callinfo . use == '<STR_LIT>' : <EOL> kw [ '<STR_LIT>' ] = False <EOL> if callinfo . style == '<STR_LIT>' : <EOL> request = TC . Struct ( None , ofwhat = [ ] , <EOL> pname = ( callinfo . namespace , name ) , ** kw ) <EOL> response = TC . Struct ( None , ofwhat = [ ] , <EOL> pname = ( callinfo . namespace , name + "<STR_LIT>" ) , ** kw ) <EOL> if len ( callinfo . getInParameters ( ) ) != len ( args ) : <EOL> raise RuntimeError ( '<STR_LIT>' % ( <EOL> str ( callinfo . getInParameters ( ) , str ( args ) ) ) ) <EOL> for msg , pms in ( ( request , callinfo . getInParameters ( ) ) , <EOL> ( response , callinfo . getOutParameters ( ) ) ) : <EOL> msg . ofwhat = [ ] <EOL> for part in pms : <EOL> klass = GTD ( * part . type ) <EOL> if klass is None : <EOL> if part . type : <EOL> klass = [ gt for gt in TC . TYPES if part . type == gt . type ] <EOL> if len ( klass ) == <NUM_LIT:0> : <EOL> klass = [ gt for gt in TC . TYPES if part . type [ <NUM_LIT:1> ] == gt . type [ <NUM_LIT:1> ] ] <EOL> if not len ( klass ) : klass = [ TC . Any ] <EOL> if len ( klass ) > <NUM_LIT:1> : <EOL> klass = [ i for i in klass if '<STR_LIT:type>' in i . __dict__ ] <EOL> klass = klass [ <NUM_LIT:0> ] <EOL> else : <EOL> klass = TC . Any <EOL> msg . ofwhat . append ( klass ( part . name ) ) <EOL> msg . ofwhat = tuple ( msg . ofwhat ) <EOL> if not args : args = { } <EOL> else : <EOL> ipart , opart = callinfo . getInParameters ( ) , callinfo . getOutParameters ( ) <EOL> if ( len ( ipart ) != <NUM_LIT:1> or not ipart [ <NUM_LIT:0> ] . element_type or <EOL> ipart [ <NUM_LIT:0> ] . type is None ) : <EOL> raise RuntimeError ( '<STR_LIT>' % callinfo . name ) <EOL> if ( len ( opart ) not in ( <NUM_LIT:0> , <NUM_LIT:1> ) or not opart [ <NUM_LIT:0> ] . element_type or <EOL> opart [ <NUM_LIT:0> ] . type is None ) : <EOL> raise RuntimeError ( '<STR_LIT>' % callinfo . name ) <EOL> ipart = ipart [ <NUM_LIT:0> ] <EOL> request , response = GED ( * ipart . type ) , None <EOL> if opart : response = GED ( * opart [ <NUM_LIT:0> ] . type ) <EOL> msg = args <EOL> if self . _asdict : <EOL> if not msg : msg = dict ( ) <EOL> self . _nullpyclass ( request ) <EOL> elif request . pyclass is not None : <EOL> if isinstance ( args , dict ) : <EOL> msg = request . pyclass ( ) <EOL> msg . __dict__ . update ( args ) <EOL> elif isinstance ( args , list ) and len ( args ) == <NUM_LIT:1> : <EOL> msg = request . pyclass ( args [ <NUM_LIT:0> ] ) <EOL> else : <EOL> msg = request . pyclass ( ) <EOL> binding . Send ( None , None , msg , <EOL> requesttypecode = request , <EOL> soapheaders = soapheaders , <EOL> encodingStyle = callinfo . encodingStyle ) <EOL> if response is None : <EOL> return None <EOL> if self . _asdict : self . _nullpyclass ( response ) <EOL> return binding . Receive ( replytype = response , <EOL> encodingStyle = callinfo . encodingStyle ) <EOL> return call_closure <EOL> def _nullpyclass ( cls , typecode ) : <EOL> typecode . pyclass = None <EOL> if not hasattr ( typecode , '<STR_LIT>' ) : return <EOL> if not isinstance ( typecode . ofwhat , ( list , tuple ) ) : <EOL> cls . _nullpyclass ( typecode . ofwhat ) <EOL> else : <EOL> for i in typecode . ofwhat : cls . _nullpyclass ( i ) <EOL> _nullpyclass = classmethod ( _nullpyclass ) <EOL> class MethodProxy : <EOL> """<STR_LIT:U+0020>""" <EOL> def __init__ ( self , parent , callinfo ) : <EOL> self . __name__ = callinfo . methodName <EOL> self . __doc__ = callinfo . documentation <EOL> self . callinfo = callinfo <EOL> self . parent = weakref . ref ( parent ) <EOL> self . soapheaders = [ ] <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> return self . parent ( ) . _call ( self . __name__ , self . soapheaders ) ( * args , ** kwargs ) <EOL> def add_headers ( self , ** headers ) : <EOL> """<STR_LIT>""" <EOL> class _holder : pass <EOL> def _remap ( pyobj , ** d ) : <EOL> pyobj . __dict__ = d <EOL> for k , v in pyobj . __dict__ . iteritems ( ) : <EOL> if not isinstance ( v , dict ) : continue <EOL> pyobj . __dict__ [ k ] = p = _holder ( ) <EOL> _remap ( p , ** v ) <EOL> for k , v in headers . iteritems ( ) : <EOL> h = [ i for i in self . callinfo . inheaders if k in i . type ] [ <NUM_LIT:0> ] <EOL> if h . element_type != <NUM_LIT:1> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> typecode = GED ( * h . type ) <EOL> if typecode is None : <EOL> raise RuntimeError ( '<STR_LIT>' % str ( h . type ) ) <EOL> pyclass = typecode . pyclass <EOL> if pyclass is None : <EOL> raise RuntimeError ( '<STR_LIT>' % str ( h . type ) ) <EOL> if not isinstance ( v , dict ) : <EOL> pyobj = pyclass ( v ) <EOL> else : <EOL> pyobj = pyclass ( ) <EOL> _remap ( pyobj , ** v ) <EOL> self . soapheaders . append ( pyobj ) </s>
