<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> import random <EOL> import threading <EOL> from google . appengine . api import apiproxy_rpc <EOL> from google . appengine . api import request_info <EOL> from google . appengine . runtime import apiproxy_errors <EOL> MAX_REQUEST_SIZE = <NUM_LIT:1> << <NUM_LIT:20> <EOL> class APIProxyStub ( object ) : <EOL> """<STR_LIT>""" <EOL> _ACCEPTS_REQUEST_ID = False <EOL> def __init__ ( self , service_name , max_request_size = MAX_REQUEST_SIZE , <EOL> request_data = None ) : <EOL> """<STR_LIT>""" <EOL> self . __service_name = service_name <EOL> self . __max_request_size = max_request_size <EOL> self . request_data = request_data or request_info . _local_request_info <EOL> self . _mutex = threading . RLock ( ) <EOL> self . __error = None <EOL> self . __error_dict = { } <EOL> def CreateRPC ( self ) : <EOL> """<STR_LIT>""" <EOL> return apiproxy_rpc . RealRPC ( stub = self ) <EOL> def MakeSyncCall ( self , service , call , request , response , request_id = None ) : <EOL> """<STR_LIT>""" <EOL> assert service == self . __service_name , ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( self . __service_name , <EOL> service ) ) <EOL> if request . ByteSize ( ) > self . __max_request_size : <EOL> raise apiproxy_errors . RequestTooLargeError ( <EOL> '<STR_LIT>' % ( service , call ) ) <EOL> messages = [ ] <EOL> assert request . IsInitialized ( messages ) , messages <EOL> exception_type , frequency = self . __error_dict . get ( call , ( None , None ) ) <EOL> if exception_type and frequency : <EOL> if random . random ( ) <= frequency : <EOL> raise exception_type <EOL> if self . __error : <EOL> if random . random ( ) <= self . __error_rate : <EOL> raise self . __error <EOL> method = getattr ( self , '<STR_LIT>' + call ) <EOL> if self . _ACCEPTS_REQUEST_ID : <EOL> method ( request , response , request_id ) <EOL> else : <EOL> method ( request , response ) <EOL> def SetError ( self , error , method = None , error_rate = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> assert error is None or isinstance ( error , apiproxy_errors . Error ) <EOL> if method and error : <EOL> self . __error_dict [ method ] = error , error_rate <EOL> else : <EOL> self . __error_rate = error_rate <EOL> self . __error = error <EOL> def Synchronized ( method ) : <EOL> """<STR_LIT>""" <EOL> def WrappedMethod ( self , * args , ** kwargs ) : <EOL> with self . _mutex : <EOL> return method ( self , * args , ** kwargs ) <EOL> return WrappedMethod </s>
