<s> """<STR_LIT>""" <EOL> import sys <EOL> import threading <EOL> class RPC ( object ) : <EOL> """<STR_LIT>""" <EOL> IDLE = <NUM_LIT:0> <EOL> RUNNING = <NUM_LIT:1> <EOL> FINISHING = <NUM_LIT:2> <EOL> def __init__ ( self , package = None , call = None , request = None , response = None , <EOL> callback = None , deadline = None , stub = None ) : <EOL> """<STR_LIT>""" <EOL> self . _exception = None <EOL> self . _state = RPC . IDLE <EOL> self . _traceback = None <EOL> self . package = package <EOL> self . call = call <EOL> self . request = request <EOL> self . response = response <EOL> self . callback = callback <EOL> self . deadline = deadline <EOL> self . stub = stub <EOL> def Clone ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _state != RPC . IDLE : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> clone = self . __class__ ( ) <EOL> for k , v in self . __dict__ . iteritems ( ) : <EOL> setattr ( clone , k , v ) <EOL> return clone <EOL> def MakeCall ( self , package = None , call = None , request = None , response = None , <EOL> callback = None , deadline = None ) : <EOL> """<STR_LIT>""" <EOL> self . callback = callback or self . callback <EOL> self . package = package or self . package <EOL> self . call = call or self . call <EOL> self . request = request or self . request <EOL> self . response = response or self . response <EOL> self . deadline = deadline or self . deadline <EOL> assert self . _state is RPC . IDLE , ( '<STR_LIT>' % <EOL> ( self . package , self . call ) ) <EOL> assert self . callback is None or callable ( self . callback ) <EOL> self . _MakeCallImpl ( ) <EOL> def Wait ( self ) : <EOL> """<STR_LIT>""" <EOL> rpc_completed = self . _WaitImpl ( ) <EOL> assert rpc_completed , ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( self . package , self . call ) ) <EOL> def CheckSuccess ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _exception and self . _traceback : <EOL> raise self . _exception . __class__ , self . _exception , self . _traceback <EOL> elif self . _exception : <EOL> raise self . _exception <EOL> @ property <EOL> def exception ( self ) : <EOL> return self . _exception <EOL> @ property <EOL> def state ( self ) : <EOL> return self . _state <EOL> def _MakeCallImpl ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _state = RPC . RUNNING <EOL> def _WaitImpl ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> try : <EOL> self . stub . MakeSyncCall ( self . package , self . call , <EOL> self . request , self . response ) <EOL> except Exception : <EOL> _ , self . _exception , self . _traceback = sys . exc_info ( ) <EOL> finally : <EOL> self . _state = RPC . FINISHING <EOL> self . _Callback ( ) <EOL> return True <EOL> def _Callback ( self ) : <EOL> if self . callback : <EOL> try : <EOL> self . callback ( ) <EOL> except : <EOL> _ , self . _exception , self . _traceback = sys . exc_info ( ) <EOL> self . _exception . _appengine_apiproxy_rpc = self <EOL> raise <EOL> class RealRPC ( RPC ) : <EOL> """<STR_LIT>""" <EOL> def _MakeCallImpl ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _thread = threading . Thread ( target = self . stub . MakeSyncCall , <EOL> args = ( self . package , self . call , <EOL> self . request , self . response ) ) <EOL> self . _thread . start ( ) <EOL> self . _state = RPC . RUNNING <EOL> def _WaitImpl ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _thread . join ( ) <EOL> except Exception : <EOL> _ , self . _exception , self . _traceback = sys . exc_info ( ) <EOL> self . _Callback ( ) <EOL> self . _state = RPC . FINISHING <EOL> """<STR_LIT>""" <EOL> return True </s>
