<s> """<STR_LIT>""" <EOL> import sys , xmlrpclib , urlparse <EOL> from twisted . web import resource , server , http <EOL> from twisted . internet import defer , protocol , reactor <EOL> from twisted . python import log , reflect , failure <EOL> NOT_FOUND = <NUM_LIT> <EOL> FAILURE = <NUM_LIT> <EOL> Fault = xmlrpclib . Fault <EOL> Binary = xmlrpclib . Binary <EOL> Boolean = xmlrpclib . Boolean <EOL> DateTime = xmlrpclib . DateTime <EOL> if sys . version_info [ : <NUM_LIT:2> ] < ( <NUM_LIT:2> , <NUM_LIT:5> ) : <EOL> _decode = DateTime . decode <EOL> DateTime . decode = lambda self , value : _decode ( self , value . encode ( '<STR_LIT:ascii>' ) ) <EOL> def withRequest ( f ) : <EOL> """<STR_LIT>""" <EOL> f . withRequest = True <EOL> return f <EOL> class NoSuchFunction ( Fault ) : <EOL> """<STR_LIT>""" <EOL> class Handler : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resource , * args ) : <EOL> self . resource = resource <EOL> self . result = defer . Deferred ( ) <EOL> self . run ( * args ) <EOL> def run ( self , * args ) : <EOL> self . result . errback ( <EOL> NotImplementedError ( "<STR_LIT>" ) ) <EOL> class XMLRPC ( resource . Resource ) : <EOL> """<STR_LIT>""" <EOL> NOT_FOUND = <NUM_LIT> <EOL> FAILURE = <NUM_LIT> <EOL> isLeaf = <NUM_LIT:1> <EOL> separator = '<STR_LIT:.>' <EOL> allowedMethods = ( '<STR_LIT:POST>' , ) <EOL> def __init__ ( self , allowNone = False , useDateTime = False ) : <EOL> resource . Resource . __init__ ( self ) <EOL> self . subHandlers = { } <EOL> self . allowNone = allowNone <EOL> self . useDateTime = useDateTime <EOL> def __setattr__ ( self , name , value ) : <EOL> if name == "<STR_LIT>" and value and sys . version_info [ : <NUM_LIT:2> ] < ( <NUM_LIT:2> , <NUM_LIT:5> ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . __dict__ [ name ] = value <EOL> def putSubHandler ( self , prefix , handler ) : <EOL> self . subHandlers [ prefix ] = handler <EOL> def getSubHandler ( self , prefix ) : <EOL> return self . subHandlers . get ( prefix , None ) <EOL> def getSubHandlerPrefixes ( self ) : <EOL> return self . subHandlers . keys ( ) <EOL> def render_POST ( self , request ) : <EOL> request . content . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> request . setHeader ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> try : <EOL> if self . useDateTime : <EOL> args , functionPath = xmlrpclib . loads ( request . content . read ( ) , <EOL> use_datetime = True ) <EOL> else : <EOL> args , functionPath = xmlrpclib . loads ( request . content . read ( ) ) <EOL> except Exception , e : <EOL> f = Fault ( self . FAILURE , "<STR_LIT>" % ( e , ) ) <EOL> self . _cbRender ( f , request ) <EOL> else : <EOL> try : <EOL> function = self . lookupProcedure ( functionPath ) <EOL> except Fault , f : <EOL> self . _cbRender ( f , request ) <EOL> else : <EOL> responseFailed = [ ] <EOL> request . notifyFinish ( ) . addErrback ( responseFailed . append ) <EOL> if getattr ( function , '<STR_LIT>' , False ) : <EOL> d = defer . maybeDeferred ( function , request , * args ) <EOL> else : <EOL> d = defer . maybeDeferred ( function , * args ) <EOL> d . addErrback ( self . _ebRender ) <EOL> d . addCallback ( self . _cbRender , request , responseFailed ) <EOL> return server . NOT_DONE_YET <EOL> def _cbRender ( self , result , request , responseFailed = None ) : <EOL> if responseFailed : <EOL> return <EOL> if isinstance ( result , Handler ) : <EOL> result = result . result <EOL> if not isinstance ( result , Fault ) : <EOL> result = ( result , ) <EOL> try : <EOL> try : <EOL> content = xmlrpclib . dumps ( <EOL> result , methodresponse = True , <EOL> allow_none = self . allowNone ) <EOL> except Exception , e : <EOL> f = Fault ( self . FAILURE , "<STR_LIT>" % ( e , ) ) <EOL> content = xmlrpclib . dumps ( f , methodresponse = True , <EOL> allow_none = self . allowNone ) <EOL> request . setHeader ( "<STR_LIT>" , str ( len ( content ) ) ) <EOL> request . write ( content ) <EOL> except : <EOL> log . err ( ) <EOL> request . finish ( ) <EOL> def _ebRender ( self , failure ) : <EOL> if isinstance ( failure . value , Fault ) : <EOL> return failure . value <EOL> log . err ( failure ) <EOL> return Fault ( self . FAILURE , "<STR_LIT:error>" ) <EOL> def lookupProcedure ( self , procedurePath ) : <EOL> """<STR_LIT>""" <EOL> if procedurePath . find ( self . separator ) != - <NUM_LIT:1> : <EOL> prefix , procedurePath = procedurePath . split ( self . separator , <NUM_LIT:1> ) <EOL> handler = self . getSubHandler ( prefix ) <EOL> if handler is None : <EOL> raise NoSuchFunction ( self . NOT_FOUND , <EOL> "<STR_LIT>" % prefix ) <EOL> return handler . lookupProcedure ( procedurePath ) <EOL> f = getattr ( self , "<STR_LIT>" % procedurePath , None ) <EOL> if not f : <EOL> raise NoSuchFunction ( self . NOT_FOUND , <EOL> "<STR_LIT>" % procedurePath ) <EOL> elif not callable ( f ) : <EOL> raise NoSuchFunction ( self . NOT_FOUND , <EOL> "<STR_LIT>" % procedurePath ) <EOL> else : <EOL> return f <EOL> def listProcedures ( self ) : <EOL> """<STR_LIT>""" <EOL> return reflect . prefixedMethodNames ( self . __class__ , '<STR_LIT>' ) <EOL> class XMLRPCIntrospection ( XMLRPC ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent ) : <EOL> """<STR_LIT>""" <EOL> XMLRPC . __init__ ( self ) <EOL> self . _xmlrpc_parent = parent <EOL> def xmlrpc_listMethods ( self ) : <EOL> """<STR_LIT>""" <EOL> functions = [ ] <EOL> todo = [ ( self . _xmlrpc_parent , '<STR_LIT>' ) ] <EOL> while todo : <EOL> obj , prefix = todo . pop ( <NUM_LIT:0> ) <EOL> functions . extend ( [ prefix + name for name in obj . listProcedures ( ) ] ) <EOL> todo . extend ( [ ( obj . getSubHandler ( name ) , <EOL> prefix + name + obj . separator ) <EOL> for name in obj . getSubHandlerPrefixes ( ) ] ) <EOL> return functions <EOL> xmlrpc_listMethods . signature = [ [ '<STR_LIT>' ] ] <EOL> def xmlrpc_methodHelp ( self , method ) : <EOL> """<STR_LIT>""" <EOL> method = self . _xmlrpc_parent . lookupProcedure ( method ) <EOL> return ( getattr ( method , '<STR_LIT>' , None ) <EOL> or getattr ( method , '<STR_LIT>' , None ) or '<STR_LIT>' ) <EOL> xmlrpc_methodHelp . signature = [ [ '<STR_LIT:string>' , '<STR_LIT:string>' ] ] <EOL> def xmlrpc_methodSignature ( self , method ) : <EOL> """<STR_LIT>""" <EOL> method = self . _xmlrpc_parent . lookupProcedure ( method ) <EOL> return getattr ( method , '<STR_LIT>' , None ) or '<STR_LIT>' <EOL> xmlrpc_methodSignature . signature = [ [ '<STR_LIT>' , '<STR_LIT:string>' ] , <EOL> [ '<STR_LIT:string>' , '<STR_LIT:string>' ] ] <EOL> def addIntrospection ( xmlrpc ) : <EOL> """<STR_LIT>""" <EOL> xmlrpc . putSubHandler ( '<STR_LIT>' , XMLRPCIntrospection ( xmlrpc ) ) <EOL> class QueryProtocol ( http . HTTPClient ) : <EOL> def connectionMade ( self ) : <EOL> self . _response = None <EOL> self . sendCommand ( '<STR_LIT:POST>' , self . factory . path ) <EOL> self . sendHeader ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . sendHeader ( '<STR_LIT>' , self . factory . host ) <EOL> self . sendHeader ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . sendHeader ( '<STR_LIT>' , str ( len ( self . factory . payload ) ) ) <EOL> if self . factory . user : <EOL> auth = '<STR_LIT>' % ( self . factory . user , self . factory . password ) <EOL> auth = auth . encode ( '<STR_LIT>' ) . strip ( ) <EOL> self . sendHeader ( '<STR_LIT>' , '<STR_LIT>' % ( auth , ) ) <EOL> self . endHeaders ( ) <EOL> self . transport . write ( self . factory . payload ) <EOL> def handleStatus ( self , version , status , message ) : <EOL> if status != '<STR_LIT>' : <EOL> self . factory . badStatus ( status , message ) <EOL> def handleResponse ( self , contents ) : <EOL> """<STR_LIT>""" <EOL> self . transport . loseConnection ( ) <EOL> self . _response = contents <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> http . HTTPClient . connectionLost ( self , reason ) <EOL> if self . _response is not None : <EOL> response , self . _response = self . _response , None <EOL> self . factory . parseResponse ( response ) <EOL> payloadTemplate = """<STR_LIT>""" <EOL> class _QueryFactory ( protocol . ClientFactory ) : <EOL> """<STR_LIT>""" <EOL> deferred = None <EOL> protocol = QueryProtocol <EOL> def __init__ ( self , path , host , method , user = None , password = None , <EOL> allowNone = False , args = ( ) , canceller = None , useDateTime = False ) : <EOL> """<STR_LIT>""" <EOL> self . path , self . host = path , host <EOL> self . user , self . password = user , password <EOL> self . payload = payloadTemplate % ( method , <EOL> xmlrpclib . dumps ( args , allow_none = allowNone ) ) <EOL> self . deferred = defer . Deferred ( canceller ) <EOL> self . useDateTime = useDateTime <EOL> def parseResponse ( self , contents ) : <EOL> if not self . deferred : <EOL> return <EOL> try : <EOL> if self . useDateTime : <EOL> response = xmlrpclib . loads ( contents , <EOL> use_datetime = True ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> else : <EOL> response = xmlrpclib . loads ( contents ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> except : <EOL> deferred , self . deferred = self . deferred , None <EOL> deferred . errback ( failure . Failure ( ) ) <EOL> else : <EOL> deferred , self . deferred = self . deferred , None <EOL> deferred . callback ( response ) <EOL> def clientConnectionLost ( self , _ , reason ) : <EOL> if self . deferred is not None : <EOL> deferred , self . deferred = self . deferred , None <EOL> deferred . errback ( reason ) <EOL> clientConnectionFailed = clientConnectionLost <EOL> def badStatus ( self , status , message ) : <EOL> deferred , self . deferred = self . deferred , None <EOL> deferred . errback ( ValueError ( status , message ) ) <EOL> class Proxy : <EOL> """<STR_LIT>""" <EOL> queryFactory = _QueryFactory <EOL> def __init__ ( self , url , user = None , password = None , allowNone = False , <EOL> useDateTime = False , connectTimeout = <NUM_LIT> , reactor = reactor ) : <EOL> """<STR_LIT>""" <EOL> scheme , netloc , path , params , query , fragment = urlparse . urlparse ( url ) <EOL> netlocParts = netloc . split ( '<STR_LIT:@>' ) <EOL> if len ( netlocParts ) == <NUM_LIT:2> : <EOL> userpass = netlocParts . pop ( <NUM_LIT:0> ) . split ( '<STR_LIT::>' ) <EOL> self . user = userpass . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> self . password = userpass . pop ( <NUM_LIT:0> ) <EOL> except : <EOL> self . password = None <EOL> else : <EOL> self . user = self . password = None <EOL> hostport = netlocParts [ <NUM_LIT:0> ] . split ( '<STR_LIT::>' ) <EOL> self . host = hostport . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> self . port = int ( hostport . pop ( <NUM_LIT:0> ) ) <EOL> except : <EOL> self . port = None <EOL> self . path = path <EOL> if self . path in [ '<STR_LIT>' , None ] : <EOL> self . path = '<STR_LIT:/>' <EOL> self . secure = ( scheme == '<STR_LIT>' ) <EOL> if user is not None : <EOL> self . user = user <EOL> if password is not None : <EOL> self . password = password <EOL> self . allowNone = allowNone <EOL> self . useDateTime = useDateTime <EOL> self . connectTimeout = connectTimeout <EOL> self . _reactor = reactor <EOL> def __setattr__ ( self , name , value ) : <EOL> if name == "<STR_LIT>" and value and sys . version_info [ : <NUM_LIT:2> ] < ( <NUM_LIT:2> , <NUM_LIT:5> ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . __dict__ [ name ] = value <EOL> def callRemote ( self , method , * args ) : <EOL> """<STR_LIT>""" <EOL> def cancel ( d ) : <EOL> factory . deferred = None <EOL> connector . disconnect ( ) <EOL> factory = self . queryFactory ( <EOL> self . path , self . host , method , self . user , <EOL> self . password , self . allowNone , args , cancel , self . useDateTime ) <EOL> if self . secure : <EOL> from twisted . internet import ssl <EOL> connector = self . _reactor . connectSSL ( <EOL> self . host , self . port or <NUM_LIT> , <EOL> factory , ssl . ClientContextFactory ( ) , <EOL> timeout = self . connectTimeout ) <EOL> else : <EOL> connector = self . _reactor . connectTCP ( <EOL> self . host , self . port or <NUM_LIT> , factory , <EOL> timeout = self . connectTimeout ) <EOL> return factory . deferred <EOL> __all__ = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] </s>
