<s> """<STR_LIT>""" <EOL> import types <EOL> import sys <EOL> from xmlrpclib import Transport as XMLTransport <EOL> from xmlrpclib import SafeTransport as XMLSafeTransport <EOL> from xmlrpclib import ServerProxy as XMLServerProxy <EOL> from xmlrpclib import _Method as XML_Method <EOL> import time <EOL> import jsonrpclib <EOL> from jsonrpclib import config <EOL> from jsonrpclib import history <EOL> cjson = None <EOL> json = None <EOL> try : <EOL> import cjson <EOL> except ImportError : <EOL> pass <EOL> if not cjson : <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> pass <EOL> if not cjson and not json : <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> raise ImportError ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> def jdumps ( obj , encoding = '<STR_LIT:utf-8>' ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in obj and len ( obj [ '<STR_LIT>' ] ) == <NUM_LIT:1> : <EOL> obj [ '<STR_LIT>' ] = obj [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> global cjson <EOL> if cjson : <EOL> return cjson . encode ( obj ) <EOL> else : <EOL> return json . dumps ( obj , encoding = encoding ) <EOL> def jloads ( json_string ) : <EOL> global cjson <EOL> if cjson : <EOL> return cjson . decode ( json_string ) <EOL> else : <EOL> return json . loads ( json_string ) <EOL> class ProtocolError ( Exception ) : <EOL> pass <EOL> class TransportMixIn ( object ) : <EOL> """<STR_LIT>""" <EOL> user_agent = config . user_agent <EOL> _connection = None <EOL> def send_content ( self , connection , request_body ) : <EOL> connection . putheader ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> connection . putheader ( "<STR_LIT>" , str ( len ( request_body ) ) ) <EOL> connection . endheaders ( ) <EOL> if request_body : <EOL> connection . send ( request_body ) <EOL> def getparser ( self ) : <EOL> target = JSONTarget ( ) <EOL> return JSONParser ( target ) , target <EOL> class JSONParser ( object ) : <EOL> def __init__ ( self , target ) : <EOL> self . target = target <EOL> def feed ( self , data ) : <EOL> self . target . feed ( data ) <EOL> def close ( self ) : <EOL> pass <EOL> class JSONTarget ( object ) : <EOL> def __init__ ( self ) : <EOL> self . data = [ ] <EOL> def feed ( self , data ) : <EOL> self . data . append ( data ) <EOL> def close ( self ) : <EOL> return '<STR_LIT>' . join ( self . data ) <EOL> class Transport ( TransportMixIn , XMLTransport ) : <EOL> pass <EOL> class SafeTransport ( TransportMixIn , XMLSafeTransport ) : <EOL> pass <EOL> class ServerProxy ( XMLServerProxy ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , uri , transport = None , encoding = None , <EOL> verbose = <NUM_LIT:0> , version = None ) : <EOL> import urllib <EOL> if not version : <EOL> version = config . version <EOL> self . __version = version <EOL> schema , uri = urllib . splittype ( uri ) <EOL> if schema not in ( '<STR_LIT:http>' , '<STR_LIT>' ) : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> self . __host , self . __handler = urllib . splithost ( uri ) <EOL> if not self . __handler : <EOL> self . __handler == '<STR_LIT:/>' <EOL> if transport is None : <EOL> if schema == '<STR_LIT>' : <EOL> transport = SafeTransport ( ) <EOL> else : <EOL> transport = Transport ( ) <EOL> self . __transport = transport <EOL> self . __encoding = encoding <EOL> self . __verbose = verbose <EOL> def _request ( self , methodname , params , rpcid = None ) : <EOL> request = dumps ( params , methodname , encoding = self . __encoding , <EOL> rpcid = rpcid , version = self . __version ) <EOL> response = self . _run_request ( request ) <EOL> check_for_errors ( response ) <EOL> return response [ '<STR_LIT:result>' ] <EOL> def _request_notify ( self , methodname , params , rpcid = None ) : <EOL> request = dumps ( params , methodname , encoding = self . __encoding , <EOL> rpcid = rpcid , version = self . __version , notify = True ) <EOL> response = self . _run_request ( request , notify = True ) <EOL> check_for_errors ( response ) <EOL> return <EOL> def _run_request ( self , request , notify = None ) : <EOL> history . add_request ( request ) <EOL> response = self . __transport . request ( <EOL> self . __host , <EOL> self . __handler , <EOL> request , <EOL> verbose = self . __verbose <EOL> ) <EOL> history . add_response ( response ) <EOL> if not response : <EOL> return None <EOL> return_obj = loads ( response ) <EOL> return return_obj <EOL> def __getattr__ ( self , name ) : <EOL> return _Method ( self . _request , name ) <EOL> @ property <EOL> def _notify ( self ) : <EOL> return _Notify ( self . _request_notify ) <EOL> class _Method ( XML_Method ) : <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> if len ( args ) > <NUM_LIT:0> and len ( kwargs ) > <NUM_LIT:0> : <EOL> raise ProtocolError ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> if len ( args ) > <NUM_LIT:0> : <EOL> return self . __send ( self . __name , args ) <EOL> else : <EOL> return self . __send ( self . __name , kwargs ) <EOL> def __getattr__ ( self , name ) : <EOL> self . __name = '<STR_LIT>' % ( self . __name , name ) <EOL> return self <EOL> class _Notify ( object ) : <EOL> def __init__ ( self , request ) : <EOL> self . _request = request <EOL> def __getattr__ ( self , name ) : <EOL> return _Method ( self . _request , name ) <EOL> class MultiCallMethod ( object ) : <EOL> def __init__ ( self , method , notify = False ) : <EOL> self . method = method <EOL> self . params = [ ] <EOL> self . notify = notify <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> if len ( kwargs ) > <NUM_LIT:0> and len ( args ) > <NUM_LIT:0> : <EOL> raise ProtocolError ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> if len ( kwargs ) > <NUM_LIT:0> : <EOL> self . params = kwargs <EOL> else : <EOL> self . params = args <EOL> def request ( self , encoding = None , rpcid = None ) : <EOL> return dumps ( self . params , self . method , version = <NUM_LIT> , <EOL> encoding = encoding , rpcid = rpcid , notify = self . notify ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT:%s>' % self . request ( ) <EOL> def __getattr__ ( self , method ) : <EOL> new_method = '<STR_LIT>' % ( self . method , method ) <EOL> self . method = new_method <EOL> return self <EOL> class MultiCallNotify ( object ) : <EOL> def __init__ ( self , multicall ) : <EOL> self . multicall = multicall <EOL> def __getattr__ ( self , name ) : <EOL> new_job = MultiCallMethod ( name , notify = True ) <EOL> self . multicall . _job_list . append ( new_job ) <EOL> return new_job <EOL> class MultiCallIterator ( object ) : <EOL> def __init__ ( self , results ) : <EOL> self . results = results <EOL> def __iter__ ( self ) : <EOL> for i in range ( <NUM_LIT:0> , len ( self . results ) ) : <EOL> yield self [ i ] <EOL> raise StopIteration <EOL> def __getitem__ ( self , i ) : <EOL> item = self . results [ i ] <EOL> check_for_errors ( item ) <EOL> return item [ '<STR_LIT:result>' ] <EOL> def __len__ ( self ) : <EOL> return len ( self . results ) <EOL> class MultiCall ( object ) : <EOL> def __init__ ( self , server ) : <EOL> self . _server = server <EOL> self . _job_list = [ ] <EOL> def _request ( self ) : <EOL> if len ( self . _job_list ) < <NUM_LIT:1> : <EOL> return <EOL> request_body = '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( [ job . request ( ) for <EOL> job in self . _job_list ] ) <EOL> responses = self . _server . _run_request ( request_body ) <EOL> del self . _job_list [ : ] <EOL> if not responses : <EOL> responses = [ ] <EOL> return MultiCallIterator ( responses ) <EOL> @ property <EOL> def _notify ( self ) : <EOL> return MultiCallNotify ( self ) <EOL> def __getattr__ ( self , name ) : <EOL> new_job = MultiCallMethod ( name ) <EOL> self . _job_list . append ( new_job ) <EOL> return new_job <EOL> __call__ = _request <EOL> Server = ServerProxy <EOL> class Fault ( object ) : <EOL> def __init__ ( self , code = - <NUM_LIT> , message = '<STR_LIT>' , rpcid = None ) : <EOL> self . faultCode = code <EOL> self . faultString = message <EOL> self . rpcid = rpcid <EOL> def error ( self ) : <EOL> return { '<STR_LIT:code>' : self . faultCode , '<STR_LIT:message>' : self . faultString } <EOL> def response ( self , rpcid = None , version = None ) : <EOL> if not version : <EOL> version = config . version <EOL> if rpcid : <EOL> self . rpcid = rpcid <EOL> return dumps ( <EOL> self , methodresponse = True , rpcid = self . rpcid , version = version <EOL> ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . faultCode , self . faultString ) <EOL> def random_id ( length = <NUM_LIT:8> ) : <EOL> import string <EOL> import random <EOL> random . seed ( ) <EOL> choices = string . lowercase + string . digits <EOL> return_id = '<STR_LIT>' <EOL> for i in range ( length ) : <EOL> return_id += random . choice ( choices ) <EOL> return return_id <EOL> class Payload ( dict ) : <EOL> def __init__ ( self , rpcid = None , version = None ) : <EOL> if not version : <EOL> version = config . version <EOL> self . id = rpcid <EOL> self . version = float ( version ) <EOL> def request ( self , method , params = [ ] ) : <EOL> if type ( method ) not in types . StringTypes : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not self . id : <EOL> self . id = random_id ( ) <EOL> request = { '<STR_LIT:id>' : self . id , '<STR_LIT>' : method } <EOL> if params : <EOL> request [ '<STR_LIT>' ] = params <EOL> if self . version >= <NUM_LIT:2> : <EOL> request [ '<STR_LIT>' ] = str ( self . version ) <EOL> return request <EOL> def notify ( self , method , params = [ ] ) : <EOL> request = self . request ( method , params ) <EOL> if self . version >= <NUM_LIT:2> : <EOL> del request [ '<STR_LIT:id>' ] <EOL> else : <EOL> request [ '<STR_LIT:id>' ] = None <EOL> return request <EOL> def response ( self , result = None ) : <EOL> response = { '<STR_LIT:result>' : result , '<STR_LIT:id>' : self . id } <EOL> if self . version >= <NUM_LIT:2> : <EOL> response [ '<STR_LIT>' ] = str ( self . version ) <EOL> else : <EOL> response [ '<STR_LIT:error>' ] = None <EOL> return response <EOL> def error ( self , code = - <NUM_LIT> , message = '<STR_LIT>' ) : <EOL> error = self . response ( ) <EOL> if self . version >= <NUM_LIT:2> : <EOL> del error [ '<STR_LIT:result>' ] <EOL> else : <EOL> error [ '<STR_LIT:result>' ] = None <EOL> error [ '<STR_LIT:error>' ] = { '<STR_LIT:code>' : code , '<STR_LIT:message>' : message } <EOL> return error <EOL> def dumps ( params = [ ] , methodname = None , methodresponse = None , <EOL> encoding = None , rpcid = None , version = None , notify = None ) : <EOL> """<STR_LIT>""" <EOL> if not version : <EOL> version = config . version <EOL> valid_params = ( types . TupleType , types . ListType , types . DictType ) <EOL> if methodname in types . StringTypes and type ( params ) not in valid_params and not isinstance ( params , Fault ) : <EOL> """<STR_LIT>""" <EOL> raise TypeError ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> payload = Payload ( rpcid = rpcid , version = version ) <EOL> if not encoding : <EOL> encoding = '<STR_LIT:utf-8>' <EOL> if type ( params ) is Fault : <EOL> response = payload . error ( params . faultCode , params . faultString ) <EOL> return jdumps ( response , encoding = encoding ) <EOL> if type ( methodname ) not in types . StringTypes and methodresponse != True : <EOL> raise ValueError ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> if config . use_jsonclass == True : <EOL> from jsonrpclib import jsonclass <EOL> params = jsonclass . dump ( params ) <EOL> if methodresponse is True : <EOL> if rpcid is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> response = payload . response ( params ) <EOL> return jdumps ( response , encoding = encoding ) <EOL> request = None <EOL> if notify == True : <EOL> request = payload . notify ( methodname , params ) <EOL> else : <EOL> request = payload . request ( methodname , params ) <EOL> return jdumps ( request , encoding = encoding ) <EOL> def loads ( data ) : <EOL> """<STR_LIT>""" <EOL> if data == '<STR_LIT>' : <EOL> return None <EOL> result = jloads ( data ) <EOL> if config . use_jsonclass == True : <EOL> from jsonrpclib import jsonclass <EOL> result = jsonclass . load ( result ) <EOL> return result <EOL> def check_for_errors ( result ) : <EOL> if not result : <EOL> return result <EOL> if type ( result ) is not types . DictType : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in result . keys ( ) and float ( result [ '<STR_LIT>' ] ) > <NUM_LIT> : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> if '<STR_LIT:result>' not in result . keys ( ) and '<STR_LIT:error>' not in result . keys ( ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if '<STR_LIT:error>' in result . keys ( ) and result [ '<STR_LIT:error>' ] != None : <EOL> code = result [ '<STR_LIT:error>' ] [ '<STR_LIT:code>' ] <EOL> message = result [ '<STR_LIT:error>' ] [ '<STR_LIT:message>' ] <EOL> raise ProtocolError ( ( code , message ) ) <EOL> return result <EOL> def isbatch ( result ) : <EOL> if type ( result ) not in ( types . ListType , types . TupleType ) : <EOL> return False <EOL> if len ( result ) < <NUM_LIT:1> : <EOL> return False <EOL> if type ( result [ <NUM_LIT:0> ] ) is not types . DictType : <EOL> return False <EOL> if '<STR_LIT>' not in result [ <NUM_LIT:0> ] . keys ( ) : <EOL> return False <EOL> try : <EOL> version = float ( result [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> except ValueError : <EOL> raise ProtocolError ( '<STR_LIT>' ) <EOL> if version < <NUM_LIT:2> : <EOL> return False <EOL> return True <EOL> def isnotification ( request ) : <EOL> if '<STR_LIT:id>' not in request . keys ( ) : <EOL> return True <EOL> if request [ '<STR_LIT:id>' ] == None : <EOL> return True <EOL> return False </s>
