<s> """<STR_LIT>""" <EOL> import urllib . request <EOL> import urllib . error <EOL> import urllib . parse <EOL> import uuid <EOL> import itertools <EOL> import logging <EOL> import json <EOL> from . exceptions import InvalidFunctionError <EOL> ERR_PARSE = - <NUM_LIT> <EOL> ERR_INVALID_REQ = - <NUM_LIT> <EOL> ERR_METHOD_NOT_FOUND = - <NUM_LIT> <EOL> ERR_INVALID_PARAMS = - <NUM_LIT> <EOL> ERR_INTERNAL = - <NUM_LIT> <EOL> ERR_UNKNOWN = - <NUM_LIT> <EOL> ERR_INVALID_RESP = - <NUM_LIT> <EOL> def contract_from_file ( fname ) : <EOL> """<STR_LIT>""" <EOL> f = open ( fname ) <EOL> j = f . read ( ) <EOL> f . close ( ) <EOL> return Contract ( json . loads ( j ) ) <EOL> def unpack_method ( method ) : <EOL> """<STR_LIT>""" <EOL> pos = method . find ( "<STR_LIT:.>" ) <EOL> if pos == - <NUM_LIT:1> : <EOL> raise RpcException ( ERR_METHOD_NOT_FOUND , "<STR_LIT>" % method ) <EOL> iface_name = method [ : pos ] <EOL> func_name = method [ pos + <NUM_LIT:1> : ] <EOL> return iface_name , func_name <EOL> def idgen_uuid ( ) : <EOL> """<STR_LIT>""" <EOL> return uuid . uuid4 ( ) . hex <EOL> idgen_seq_counter = itertools . count ( ) <EOL> def idgen_seq ( ) : <EOL> """<STR_LIT>""" <EOL> return str ( next ( idgen_seq_counter ) ) <EOL> def err_response ( reqid , code , msg , data = None ) : <EOL> """<STR_LIT>""" <EOL> err = { "<STR_LIT:code>" : code , "<STR_LIT:message>" : msg } <EOL> if data : <EOL> err [ "<STR_LIT:data>" ] = data <EOL> return { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:id>" : reqid , "<STR_LIT:error>" : err } <EOL> def safe_get ( d , key , def_val = None ) : <EOL> """<STR_LIT>""" <EOL> if key in d : <EOL> return d [ key ] <EOL> else : <EOL> return def_val <EOL> class RpcException ( Exception , json . JSONEncoder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , code , msg = "<STR_LIT>" , data = None ) : <EOL> """<STR_LIT>""" <EOL> self . code = code <EOL> self . msg = msg <EOL> self . data = data <EOL> def __str__ ( self ) : <EOL> s = "<STR_LIT>" % ( self . code , self . msg ) <EOL> if self . data : <EOL> s += "<STR_LIT>" % ( str ( self . data ) ) <EOL> return s <EOL> class RequestContext ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , props , req ) : <EOL> """<STR_LIT>""" <EOL> self . props = props <EOL> self . request = req <EOL> self . response = None <EOL> self . error = None <EOL> def func_name ( self ) : <EOL> return unpack_method ( self . request [ "<STR_LIT>" ] ) [ <NUM_LIT:1> ] <EOL> def get_prop ( self , key , default_val = None ) : <EOL> """<STR_LIT>""" <EOL> if key in self . props : <EOL> return self . props [ key ] <EOL> else : <EOL> return default_val <EOL> def set_error ( self , code , msg , data = None ) : <EOL> """<STR_LIT>""" <EOL> self . error = err_response ( self . request [ "<STR_LIT:id>" ] , code , msg , data ) <EOL> class Filter ( object ) : <EOL> """<STR_LIT>""" <EOL> def pre ( self , context ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def post ( self , context ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Server ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , contract , validate_request = True , validate_response = True ) : <EOL> """<STR_LIT>""" <EOL> logging . basicConfig ( ) <EOL> self . log = logging . getLogger ( "<STR_LIT>" ) <EOL> self . validate_req = validate_request <EOL> self . validate_resp = validate_response <EOL> self . contract = contract <EOL> self . handlers = { } <EOL> self . filters = None <EOL> def add_handler ( self , iface_name , handler ) : <EOL> """<STR_LIT>""" <EOL> if self . contract . has_interface ( iface_name ) : <EOL> self . handlers [ iface_name ] = handler <EOL> else : <EOL> raise RpcException ( ERR_INVALID_REQ , "<STR_LIT>" % iface_name ) <EOL> def set_filters ( self , filters ) : <EOL> """<STR_LIT>""" <EOL> if filters is None or isinstance ( filters , ( tuple , list ) ) : <EOL> self . filters = filters <EOL> else : <EOL> self . filters = [ filters ] <EOL> def call_json ( self , req_json , props = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> req = json . loads ( req_json ) <EOL> except Exception as e : <EOL> msg = "<STR_LIT>" % req_json <EOL> return json . dumps ( err_response ( None , - <NUM_LIT> , msg ) ) <EOL> return json . dumps ( self . call ( req , props ) ) <EOL> def call ( self , req , props = None ) : <EOL> """<STR_LIT>""" <EOL> resp = None <EOL> if self . log . isEnabledFor ( logging . DEBUG ) : <EOL> self . log . debug ( "<STR_LIT>" % str ( req ) ) <EOL> if isinstance ( req , list ) : <EOL> if len ( req ) < <NUM_LIT:1> : <EOL> resp = err_response ( None , ERR_INVALID_REQ , "<STR_LIT>" ) <EOL> else : <EOL> resp = [ self . _call_and_format ( r , props ) for r in req ] <EOL> else : <EOL> resp = self . _call_and_format ( req , props ) <EOL> if self . log . isEnabledFor ( logging . DEBUG ) : <EOL> self . log . debug ( "<STR_LIT>" % str ( resp ) ) <EOL> return resp <EOL> def _call_and_format ( self , req , props = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( req , dict ) : <EOL> return err_response ( None , ERR_INVALID_REQ , <EOL> "<STR_LIT>" % str ( req ) ) <EOL> reqid = None <EOL> if "<STR_LIT:id>" in req : <EOL> reqid = req [ "<STR_LIT:id>" ] <EOL> if props is None : <EOL> props = { } <EOL> context = RequestContext ( props , req ) <EOL> if self . filters : <EOL> for f in self . filters : <EOL> f . pre ( context ) <EOL> if context . error : <EOL> return context . error <EOL> resp = None <EOL> try : <EOL> result = self . _call ( context ) <EOL> resp = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:id>" : reqid , "<STR_LIT:result>" : result } <EOL> except RpcException as e : <EOL> resp = err_response ( reqid , e . code , e . msg , e . data ) <EOL> except Exception as e : <EOL> self . log . exception ( "<STR_LIT>" % str ( req ) ) <EOL> resp = err_response ( reqid , ERR_UNKNOWN , "<STR_LIT>" , <EOL> data = { <EOL> '<STR_LIT>' : str ( e ) <EOL> } ) <EOL> if self . filters : <EOL> context . response = resp <EOL> for f in self . filters : <EOL> f . post ( context ) <EOL> return resp <EOL> def _call ( self , context ) : <EOL> """<STR_LIT>""" <EOL> req = context . request <EOL> if "<STR_LIT>" not in req : <EOL> raise RpcException ( ERR_INVALID_REQ , "<STR_LIT>" ) <EOL> method = req [ "<STR_LIT>" ] <EOL> if method == "<STR_LIT>" or method == "<STR_LIT>" : <EOL> return self . contract . idl_parsed <EOL> iface_name , func_name = unpack_method ( method ) <EOL> if iface_name in self . handlers : <EOL> iface_impl = self . handlers [ iface_name ] <EOL> func = getattr ( iface_impl , func_name ) <EOL> if func : <EOL> if "<STR_LIT>" in req : <EOL> params = req [ "<STR_LIT>" ] <EOL> else : <EOL> params = [ ] <EOL> if self . validate_req : <EOL> self . contract . validate_request ( iface_name , func_name , params ) <EOL> if hasattr ( iface_impl , "<STR_LIT>" ) : <EOL> pre_hook = getattr ( iface_impl , "<STR_LIT>" ) <EOL> pre_hook ( context , params ) <EOL> if params : <EOL> result = func ( * params ) <EOL> else : <EOL> result = func ( ) <EOL> if self . validate_resp : <EOL> self . contract . validate_response ( iface_name , func_name , result ) <EOL> return result <EOL> else : <EOL> msg = "<STR_LIT>" % method <EOL> raise RpcException ( ERR_METHOD_NOT_FOUND , msg ) <EOL> else : <EOL> msg = "<STR_LIT>" % iface_name <EOL> raise RpcException ( ERR_METHOD_NOT_FOUND , msg ) <EOL> class HttpTransport ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , url , handlers = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> if not headers : <EOL> headers = { } <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> self . url = url <EOL> self . headers = headers <EOL> if handlers : <EOL> self . opener = urllib . request . build_opener ( * handlers ) <EOL> else : <EOL> self . opener = urllib . request . build_opener ( ) <EOL> def request ( self , req ) : <EOL> """<STR_LIT>""" <EOL> data = json . dumps ( req ) <EOL> req = urllib . request . Request ( self . url , data , self . headers ) <EOL> f = self . opener . open ( req ) <EOL> resp = f . read ( ) <EOL> f . close ( ) <EOL> return json . loads ( resp ) <EOL> class InProcTransport ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server ) : <EOL> """<STR_LIT>""" <EOL> self . server = server <EOL> def request ( self , req ) : <EOL> """<STR_LIT>""" <EOL> return self . server . call ( req ) <EOL> class Client ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , transport , validate_request = True , validate_response = True , <EOL> id_gen = idgen_uuid ) : <EOL> """<STR_LIT>""" <EOL> logging . basicConfig ( ) <EOL> self . log = logging . getLogger ( "<STR_LIT>" ) <EOL> self . transport = transport <EOL> self . validate_req = validate_request <EOL> self . validate_resp = validate_response <EOL> self . id_gen = id_gen <EOL> req = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:id>" : "<STR_LIT:1>" } <EOL> resp = transport . request ( req ) <EOL> self . contract = Contract ( resp [ "<STR_LIT:result>" ] ) <EOL> for k , v in list ( self . contract . interfaces . items ( ) ) : <EOL> setattr ( self , k , InterfaceClientProxy ( self , v ) ) <EOL> def get_meta ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . contract . meta <EOL> def call ( self , iface_name , func_name , params ) : <EOL> """<STR_LIT>""" <EOL> req = self . to_request ( iface_name , func_name , params ) <EOL> if self . log . isEnabledFor ( logging . DEBUG ) : <EOL> self . log . debug ( "<STR_LIT>" % str ( req ) ) <EOL> resp = self . transport . request ( req ) <EOL> if self . log . isEnabledFor ( logging . DEBUG ) : <EOL> self . log . debug ( "<STR_LIT>" % str ( resp ) ) <EOL> return self . to_result ( iface_name , func_name , resp ) <EOL> def to_request ( self , iface_name , func_name , params ) : <EOL> """<STR_LIT>""" <EOL> if self . validate_req : <EOL> self . contract . validate_request ( iface_name , func_name , params ) <EOL> method = "<STR_LIT>" % ( iface_name , func_name ) <EOL> reqid = self . id_gen ( ) <EOL> return { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:id>" : reqid , "<STR_LIT>" : method , "<STR_LIT>" : params } <EOL> def to_result ( self , iface_name , func_name , resp ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT:error>" in resp : <EOL> e = resp [ "<STR_LIT:error>" ] <EOL> data = None <EOL> if "<STR_LIT:data>" in e : <EOL> data = e [ "<STR_LIT:data>" ] <EOL> raise RpcException ( e [ "<STR_LIT:code>" ] , e [ "<STR_LIT:message>" ] , data ) <EOL> result = resp [ "<STR_LIT:result>" ] <EOL> if self . validate_resp : <EOL> self . contract . validate_response ( iface_name , func_name , result ) <EOL> return result <EOL> def start_batch ( self ) : <EOL> """<STR_LIT>""" <EOL> return Batch ( self ) <EOL> class InterfaceClientProxy ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , client , iface ) : <EOL> """<STR_LIT>""" <EOL> self . client = client <EOL> iface_name = iface . name <EOL> for func_name , func in list ( iface . functions . items ( ) ) : <EOL> setattr ( self , func_name , self . _caller ( iface_name , func_name ) ) <EOL> def _caller ( self , iface_name , func_name ) : <EOL> """<STR_LIT>""" <EOL> def caller ( * params ) : <EOL> return self . client . call ( iface_name , func_name , params ) <EOL> return caller <EOL> class Batch ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , client ) : <EOL> """<STR_LIT>""" <EOL> self . client = client <EOL> self . req_list = [ ] <EOL> self . sent = False <EOL> for k , v in list ( client . contract . interfaces . items ( ) ) : <EOL> setattr ( self , k , InterfaceClientProxy ( self , v ) ) <EOL> def call ( self , iface_name , func_name , params ) : <EOL> """<STR_LIT>""" <EOL> if self . sent : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> req = self . client . to_request ( iface_name , func_name , params ) <EOL> self . req_list . append ( req ) <EOL> def send ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . sent : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> self . sent = True <EOL> results = self . client . transport . request ( self . req_list ) <EOL> id_to_method = { } <EOL> by_id = { } <EOL> for res in results : <EOL> reqid = res [ "<STR_LIT:id>" ] <EOL> by_id [ reqid ] = res <EOL> in_req_order = [ ] <EOL> for req in self . req_list : <EOL> reqid = req [ "<STR_LIT:id>" ] <EOL> result = None <EOL> error = None <EOL> resp = safe_get ( by_id , reqid ) <EOL> if resp is None : <EOL> msg = "<STR_LIT>" % reqid <EOL> error = RpcException ( ERR_INVALID_RESP , msg ) <EOL> else : <EOL> r_err = safe_get ( resp , "<STR_LIT:error>" ) <EOL> if r_err is None : <EOL> result = resp [ "<STR_LIT:result>" ] <EOL> else : <EOL> error = RpcException ( r_err [ "<STR_LIT:code>" ] , r_err [ "<STR_LIT:message>" ] , safe_get ( r_err , "<STR_LIT:data>" ) ) <EOL> in_req_order . append ( RpcResponse ( req , result , error ) ) <EOL> return in_req_order <EOL> class RpcResponse ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request , result , error ) : <EOL> self . request = request <EOL> self . result = result <EOL> self . error = error <EOL> class Contract ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , idl_parsed ) : <EOL> """<STR_LIT>""" <EOL> self . idl_parsed = idl_parsed <EOL> self . interfaces = { } <EOL> self . structs = { } <EOL> self . enums = { } <EOL> self . meta = { } <EOL> for e in idl_parsed : <EOL> if e [ "<STR_LIT:type>" ] == "<STR_LIT>" : <EOL> self . structs [ e [ "<STR_LIT:name>" ] ] = Struct ( e , self ) <EOL> elif e [ "<STR_LIT:type>" ] == "<STR_LIT>" : <EOL> self . enums [ e [ "<STR_LIT:name>" ] ] = Enum ( e ) <EOL> elif e [ "<STR_LIT:type>" ] == "<STR_LIT>" : <EOL> self . interfaces [ e [ "<STR_LIT:name>" ] ] = Interface ( e , self ) <EOL> elif e [ "<STR_LIT:type>" ] == "<STR_LIT>" : <EOL> for k , v in list ( e . items ( ) ) : <EOL> if k != "<STR_LIT:type>" : <EOL> self . meta [ k ] = v <EOL> def validate_request ( self , iface_name , func_name , params ) : <EOL> """<STR_LIT>""" <EOL> self . interface ( iface_name ) . function ( func_name ) . validate_params ( params ) <EOL> def validate_response ( self , iface_name , func_name , resp ) : <EOL> """<STR_LIT>""" <EOL> self . interface ( iface_name ) . function ( func_name ) . validate_response ( resp ) <EOL> def get ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name in self . structs : <EOL> return self . structs [ name ] <EOL> elif name in self . enums : <EOL> return self . enums [ name ] <EOL> elif name in self . interfaces : <EOL> return self . interfaces [ name ] <EOL> else : <EOL> raise RpcException ( ERR_INVALID_PARAMS , "<STR_LIT>" % name ) <EOL> def struct ( self , struct_name ) : <EOL> """<STR_LIT>""" <EOL> if struct_name in self . structs : <EOL> return self . structs [ struct_name ] <EOL> else : <EOL> raise RpcException ( ERR_INVALID_PARAMS , "<STR_LIT>" , struct_name ) <EOL> def has_interface ( self , iface_name ) : <EOL> """<STR_LIT>""" <EOL> return iface_name in self . interfaces <EOL> def interface ( self , iface_name ) : <EOL> """<STR_LIT>""" <EOL> if self . has_interface ( iface_name ) : <EOL> return self . interfaces [ iface_name ] <EOL> else : <EOL> raise RpcException ( ERR_INVALID_PARAMS , "<STR_LIT>" % iface_name ) <EOL> def validate ( self , expected_type , is_array , val ) : <EOL> """<STR_LIT>""" <EOL> if val is None : <EOL> if expected_type . optional : <EOL> return True , None <EOL> else : <EOL> return False , "<STR_LIT>" <EOL> elif is_array : <EOL> if not isinstance ( val , list ) : <EOL> return self . _type_err ( val , "<STR_LIT:list>" ) <EOL> else : <EOL> for v in val : <EOL> ok , msg = self . validate ( expected_type , False , v ) <EOL> if not ok : <EOL> return ok , msg <EOL> elif expected_type . type == "<STR_LIT:int>" : <EOL> if not isinstance ( val , int ) : <EOL> return self . _type_err ( val , "<STR_LIT:int>" ) <EOL> elif expected_type . type == "<STR_LIT:float>" : <EOL> if not isinstance ( val , ( float , int ) ) : <EOL> return self . _type_err ( val , "<STR_LIT:float>" ) <EOL> elif expected_type . type == "<STR_LIT:bool>" : <EOL> if not isinstance ( val , bool ) : <EOL> return self . _type_err ( val , "<STR_LIT:bool>" ) <EOL> elif expected_type . type == "<STR_LIT:string>" : <EOL> if not isinstance ( val , str ) : <EOL> return self . _type_err ( val , "<STR_LIT:string>" ) <EOL> else : <EOL> return self . get ( expected_type . type ) . validate ( val ) <EOL> return True , None <EOL> def _type_err ( self , val , expected ) : <EOL> return False , "<STR_LIT>" % ( val , type ( val ) , expected ) <EOL> class Interface ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , iface , contract ) : <EOL> """<STR_LIT>""" <EOL> self . name = iface [ "<STR_LIT:name>" ] <EOL> self . functions = { } <EOL> for f in iface [ "<STR_LIT>" ] : <EOL> self . functions [ f [ "<STR_LIT:name>" ] ] = Function ( self . name , f , contract ) <EOL> def function ( self , func_name ) : <EOL> """<STR_LIT>""" <EOL> if func_name in self . functions : <EOL> return self . functions [ func_name ] <EOL> else : <EOL> raise RpcException ( ERR_METHOD_NOT_FOUND , <EOL> "<STR_LIT>" % ( self . name , func_name ) ) <EOL> class Enum ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , enum ) : <EOL> """<STR_LIT>""" <EOL> self . name = enum [ "<STR_LIT:name>" ] <EOL> self . values = [ ] <EOL> for v in enum [ "<STR_LIT>" ] : <EOL> self . values . append ( v [ "<STR_LIT:value>" ] ) <EOL> def validate ( self , val ) : <EOL> """<STR_LIT>""" <EOL> if val in self . values : <EOL> return True , None <EOL> else : <EOL> return False , "<STR_LIT>" % ( val , str ( self . values ) ) <EOL> class Struct ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , s , contract ) : <EOL> """<STR_LIT>""" <EOL> self . contract = contract <EOL> self . name = s [ "<STR_LIT:name>" ] <EOL> self . extends = s [ "<STR_LIT>" ] <EOL> self . parent = None <EOL> self . fields = { } <EOL> for f in s [ "<STR_LIT>" ] : <EOL> self . fields [ f [ "<STR_LIT:name>" ] ] = Type ( f ) <EOL> def field ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name in self . fields : <EOL> return self . fields [ name ] <EOL> elif self . extends : <EOL> if not self . parent : <EOL> self . parent = self . contract . struct ( self . extends ) <EOL> return self . parent . field ( name ) <EOL> else : <EOL> return None <EOL> def validate ( self , val ) : <EOL> """<STR_LIT>""" <EOL> if type ( val ) is not dict : <EOL> return False , "<STR_LIT>" % ( str ( val ) ) <EOL> for k , v in list ( val . items ( ) ) : <EOL> field = self . field ( k ) <EOL> if field : <EOL> ok , msg = self . contract . validate ( field , field . is_array , v ) <EOL> if not ok : <EOL> return False , "<STR_LIT>" % ( field . name , msg ) <EOL> else : <EOL> return False , "<STR_LIT>" % ( k , self . name ) <EOL> all_fields = self . get_all_fields ( [ ] ) <EOL> for field in all_fields : <EOL> if field . name not in val and not field . optional : <EOL> return False , "<STR_LIT>" % ( field . name , str ( val ) ) <EOL> return True , None <EOL> def get_all_fields ( self , arr ) : <EOL> """<STR_LIT>""" <EOL> for k , v in list ( self . fields . items ( ) ) : <EOL> arr . append ( v ) <EOL> if self . extends : <EOL> parent = self . contract . get ( self . extends ) <EOL> if parent : <EOL> return parent . get_all_fields ( arr ) <EOL> return arr <EOL> class Function ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , iface_name , f , contract ) : <EOL> """<STR_LIT>""" <EOL> self . contract = contract <EOL> self . name = f [ "<STR_LIT:name>" ] <EOL> self . params = [ ] <EOL> for p in f [ "<STR_LIT>" ] : <EOL> self . params . append ( Type ( p ) ) <EOL> self . returns = Type ( f [ "<STR_LIT>" ] ) if "<STR_LIT>" in f else None <EOL> self . full_name = "<STR_LIT>" % ( iface_name , self . name ) <EOL> self . validate_structure ( ) <EOL> def validate_structure ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . name in [ None , '<STR_LIT>' ] : <EOL> raise InvalidFunctionError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> if self . returns is None : <EOL> raise InvalidFunctionError ( <EOL> '<STR_LIT>' % ( <EOL> self . full_name <EOL> ) <EOL> ) <EOL> def validate_params ( self , params ) : <EOL> """<STR_LIT>""" <EOL> if params is not None : <EOL> if len ( self . params ) != len ( params ) : <EOL> vals = ( self . full_name , len ( self . params ) , len ( params ) ) <EOL> msg = "<STR_LIT>" % vals <EOL> raise RpcException ( ERR_INVALID_PARAMS , msg ) <EOL> [ self . _validate_param ( x , y ) for ( x , y ) in zip ( self . params , params ) ] <EOL> def validate_response ( self , resp ) : <EOL> """<STR_LIT>""" <EOL> ok , msg = self . contract . validate ( self . returns , <EOL> self . returns . is_array , resp ) <EOL> if not ok : <EOL> vals = ( self . full_name , str ( resp ) , msg ) <EOL> msg = "<STR_LIT>" % vals <EOL> raise RpcException ( ERR_INVALID_RESP , msg ) <EOL> def _validate_param ( self , expected , param ) : <EOL> """<STR_LIT>""" <EOL> ok , msg = self . contract . validate ( expected , expected . is_array , param ) <EOL> if not ok : <EOL> vals = ( self . full_name , expected . name , msg ) <EOL> msg = "<STR_LIT>" % vals <EOL> raise RpcException ( ERR_INVALID_PARAMS , msg ) <EOL> class Type ( object ) : <EOL> def __init__ ( self , type_dict ) : <EOL> self . name = "<STR_LIT>" <EOL> self . optional = False <EOL> if "<STR_LIT:name>" in type_dict : <EOL> self . name = type_dict [ "<STR_LIT:name>" ] <EOL> self . type = type_dict [ "<STR_LIT:type>" ] <EOL> self . is_array = type_dict [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in type_dict : <EOL> self . optional = type_dict [ "<STR_LIT>" ] </s>
