<s> """<STR_LIT>""" <EOL> import google <EOL> import os <EOL> import pickle <EOL> import random <EOL> import sys <EOL> import thread <EOL> import threading <EOL> import yaml <EOL> import hashlib <EOL> if os . environ . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> from google . appengine . api import apiproxy_rpc <EOL> from google . appengine . api import apiproxy_stub_map <EOL> from google . appengine . datastore import datastore_pb <EOL> from google . appengine . ext . remote_api import remote_api_pb <EOL> from google . appengine . ext . remote_api import remote_api_services <EOL> from google . appengine . runtime import apiproxy_errors <EOL> else : <EOL> from google . appengine . api import apiproxy_rpc <EOL> from google . appengine . api import apiproxy_stub_map <EOL> from google . appengine . datastore import datastore_pb <EOL> from google . appengine . ext . remote_api import remote_api_pb <EOL> from google . appengine . ext . remote_api import remote_api_services <EOL> from google . appengine . runtime import apiproxy_errors <EOL> from google . appengine . tools import appengine_rpc <EOL> _REQUEST_ID_HEADER = '<STR_LIT>' <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class ConfigurationError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class UnknownJavaServerError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def GetUserAgent ( ) : <EOL> """<STR_LIT>""" <EOL> product_tokens = [ ] <EOL> product_tokens . append ( "<STR_LIT>" ) <EOL> product_tokens . append ( appengine_rpc . GetPlatformToken ( ) ) <EOL> python_version = "<STR_LIT:.>" . join ( str ( i ) for i in sys . version_info ) <EOL> product_tokens . append ( "<STR_LIT>" % python_version ) <EOL> return "<STR_LIT:U+0020>" . join ( product_tokens ) <EOL> def GetSourceName ( ) : <EOL> return "<STR_LIT>" <EOL> def HashEntity ( entity ) : <EOL> """<STR_LIT>""" <EOL> return hashlib . sha1 ( entity . Encode ( ) ) . digest ( ) <EOL> class TransactionData ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , thread_id , is_xg ) : <EOL> self . thread_id = thread_id <EOL> self . preconditions = { } <EOL> self . entities = { } <EOL> self . is_xg = is_xg <EOL> class RemoteStub ( object ) : <EOL> """<STR_LIT>""" <EOL> _local = threading . local ( ) <EOL> def __init__ ( self , server , path , _test_stub_map = None ) : <EOL> """<STR_LIT>""" <EOL> self . _server = server <EOL> self . _path = path <EOL> self . _test_stub_map = _test_stub_map <EOL> def _PreHookHandler ( self , service , call , request , response ) : <EOL> pass <EOL> def _PostHookHandler ( self , service , call , request , response ) : <EOL> pass <EOL> def MakeSyncCall ( self , service , call , request , response ) : <EOL> self . _PreHookHandler ( service , call , request , response ) <EOL> try : <EOL> test_stub = self . _test_stub_map and self . _test_stub_map . GetStub ( service ) <EOL> if test_stub : <EOL> test_stub . MakeSyncCall ( service , call , request , response ) <EOL> else : <EOL> self . _MakeRealSyncCall ( service , call , request , response ) <EOL> finally : <EOL> self . _PostHookHandler ( service , call , request , response ) <EOL> @ classmethod <EOL> def _GetRequestId ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . _local . request_id <EOL> @ classmethod <EOL> def _SetRequestId ( cls , request_id ) : <EOL> """<STR_LIT>""" <EOL> cls . _local . request_id = request_id <EOL> def _MakeRealSyncCall ( self , service , call , request , response ) : <EOL> request_pb = remote_api_pb . Request ( ) <EOL> request_pb . set_service_name ( service ) <EOL> request_pb . set_method ( call ) <EOL> request_pb . set_request ( request . Encode ( ) ) <EOL> if hasattr ( self . _local , '<STR_LIT>' ) : <EOL> request_pb . set_request_id ( self . _local . request_id ) <EOL> response_pb = remote_api_pb . Response ( ) <EOL> encoded_request = request_pb . Encode ( ) <EOL> encoded_response = self . _server . Send ( self . _path , encoded_request ) <EOL> response_pb . ParseFromString ( encoded_response ) <EOL> if response_pb . has_application_error ( ) : <EOL> error_pb = response_pb . application_error ( ) <EOL> raise apiproxy_errors . ApplicationError ( error_pb . code ( ) , <EOL> error_pb . detail ( ) ) <EOL> elif response_pb . has_exception ( ) : <EOL> raise pickle . loads ( response_pb . exception ( ) ) <EOL> elif response_pb . has_java_exception ( ) : <EOL> raise UnknownJavaServerError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> response . ParseFromString ( response_pb . response ( ) ) <EOL> def CreateRPC ( self ) : <EOL> return apiproxy_rpc . RPC ( stub = self ) <EOL> class RemoteDatastoreStub ( RemoteStub ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server , path , default_result_count = <NUM_LIT:20> , <EOL> _test_stub_map = None ) : <EOL> """<STR_LIT>""" <EOL> super ( RemoteDatastoreStub , self ) . __init__ ( server , path , _test_stub_map ) <EOL> self . default_result_count = default_result_count <EOL> self . __queries = { } <EOL> self . __transactions = { } <EOL> self . __next_local_cursor = <NUM_LIT:1> <EOL> self . __local_cursor_lock = threading . Lock ( ) <EOL> self . __next_local_tx = <NUM_LIT:1> <EOL> self . __local_tx_lock = threading . Lock ( ) <EOL> def MakeSyncCall ( self , service , call , request , response ) : <EOL> assert service == '<STR_LIT>' <EOL> explanation = [ ] <EOL> assert request . IsInitialized ( explanation ) , explanation <EOL> handler = getattr ( self , '<STR_LIT>' + call , None ) <EOL> if handler : <EOL> handler ( request , response ) <EOL> else : <EOL> super ( RemoteDatastoreStub , self ) . MakeSyncCall ( service , call , request , <EOL> response ) <EOL> assert response . IsInitialized ( explanation ) , explanation <EOL> def _Dynamic_RunQuery ( self , query , query_result , cursor_id = None ) : <EOL> super ( RemoteDatastoreStub , self ) . MakeSyncCall ( <EOL> '<STR_LIT>' , '<STR_LIT>' , query , query_result ) <EOL> if cursor_id is None : <EOL> self . __local_cursor_lock . acquire ( ) <EOL> try : <EOL> cursor_id = self . __next_local_cursor <EOL> self . __next_local_cursor += <NUM_LIT:1> <EOL> finally : <EOL> self . __local_cursor_lock . release ( ) <EOL> if query_result . more_results ( ) : <EOL> query . set_offset ( query . offset ( ) + query_result . result_size ( ) ) <EOL> if query . has_limit ( ) : <EOL> query . set_limit ( query . limit ( ) - query_result . result_size ( ) ) <EOL> self . __queries [ cursor_id ] = query <EOL> else : <EOL> self . __queries [ cursor_id ] = None <EOL> query_result . mutable_cursor ( ) . set_cursor ( cursor_id ) <EOL> def _Dynamic_Next ( self , next_request , query_result ) : <EOL> assert next_request . offset ( ) == <NUM_LIT:0> <EOL> cursor_id = next_request . cursor ( ) . cursor ( ) <EOL> if cursor_id not in self . __queries : <EOL> raise apiproxy_errors . ApplicationError ( datastore_pb . Error . BAD_REQUEST , <EOL> '<STR_LIT>' % cursor_id ) <EOL> query = self . __queries [ cursor_id ] <EOL> if query is None : <EOL> query_result . set_more_results ( False ) <EOL> return <EOL> else : <EOL> if next_request . has_count ( ) : <EOL> query . set_count ( next_request . count ( ) ) <EOL> else : <EOL> query . clear_count ( ) <EOL> self . _Dynamic_RunQuery ( query , query_result , cursor_id ) <EOL> query_result . set_skipped_results ( <NUM_LIT:0> ) <EOL> def _Dynamic_Get ( self , get_request , get_response ) : <EOL> txid = None <EOL> if get_request . has_transaction ( ) : <EOL> txid = get_request . transaction ( ) . handle ( ) <EOL> txdata = self . __transactions [ txid ] <EOL> assert ( txdata . thread_id == <EOL> thread . get_ident ( ) ) , "<STR_LIT>" <EOL> keys = [ ( k , k . Encode ( ) ) for k in get_request . key_list ( ) ] <EOL> new_request = datastore_pb . GetRequest ( ) <EOL> for key , enckey in keys : <EOL> if enckey not in txdata . entities : <EOL> new_request . add_key ( ) . CopyFrom ( key ) <EOL> else : <EOL> new_request = get_request <EOL> if new_request . key_size ( ) > <NUM_LIT:0> : <EOL> super ( RemoteDatastoreStub , self ) . MakeSyncCall ( <EOL> '<STR_LIT>' , '<STR_LIT>' , new_request , get_response ) <EOL> if txid is not None : <EOL> newkeys = new_request . key_list ( ) <EOL> entities = get_response . entity_list ( ) <EOL> for key , entity in zip ( newkeys , entities ) : <EOL> entity_hash = None <EOL> if entity . has_entity ( ) : <EOL> entity_hash = HashEntity ( entity . entity ( ) ) <EOL> txdata . preconditions [ key . Encode ( ) ] = ( key , entity_hash ) <EOL> new_response = datastore_pb . GetResponse ( ) <EOL> it = iter ( get_response . entity_list ( ) ) <EOL> for key , enckey in keys : <EOL> if enckey in txdata . entities : <EOL> cached_entity = txdata . entities [ enckey ] [ <NUM_LIT:1> ] <EOL> if cached_entity : <EOL> new_response . add_entity ( ) . mutable_entity ( ) . CopyFrom ( cached_entity ) <EOL> else : <EOL> new_response . add_entity ( ) <EOL> else : <EOL> new_entity = it . next ( ) <EOL> if new_entity . has_entity ( ) : <EOL> assert new_entity . entity ( ) . key ( ) == key <EOL> new_response . add_entity ( ) . CopyFrom ( new_entity ) <EOL> else : <EOL> new_response . add_entity ( ) <EOL> get_response . CopyFrom ( new_response ) <EOL> def _Dynamic_Put ( self , put_request , put_response ) : <EOL> if put_request . has_transaction ( ) : <EOL> entities = put_request . entity_list ( ) <EOL> requires_id = lambda x : x . id ( ) == <NUM_LIT:0> and not x . has_name ( ) <EOL> new_ents = [ e for e in entities <EOL> if requires_id ( e . key ( ) . path ( ) . element_list ( ) [ - <NUM_LIT:1> ] ) ] <EOL> id_request = datastore_pb . PutRequest ( ) <EOL> txid = put_request . transaction ( ) . handle ( ) <EOL> txdata = self . __transactions [ txid ] <EOL> assert ( txdata . thread_id == <EOL> thread . get_ident ( ) ) , "<STR_LIT>" <EOL> if new_ents : <EOL> for ent in new_ents : <EOL> e = id_request . add_entity ( ) <EOL> e . mutable_key ( ) . CopyFrom ( ent . key ( ) ) <EOL> e . mutable_entity_group ( ) <EOL> id_response = datastore_pb . PutResponse ( ) <EOL> if txdata . is_xg : <EOL> rpc_name = '<STR_LIT>' <EOL> else : <EOL> rpc_name = '<STR_LIT>' <EOL> super ( RemoteDatastoreStub , self ) . MakeSyncCall ( <EOL> '<STR_LIT>' , rpc_name , id_request , id_response ) <EOL> assert id_request . entity_size ( ) == id_response . key_size ( ) <EOL> for key , ent in zip ( id_response . key_list ( ) , new_ents ) : <EOL> ent . mutable_key ( ) . CopyFrom ( key ) <EOL> ent . mutable_entity_group ( ) . add_element ( ) . CopyFrom ( <EOL> key . path ( ) . element ( <NUM_LIT:0> ) ) <EOL> for entity in entities : <EOL> txdata . entities [ entity . key ( ) . Encode ( ) ] = ( entity . key ( ) , entity ) <EOL> put_response . add_key ( ) . CopyFrom ( entity . key ( ) ) <EOL> else : <EOL> super ( RemoteDatastoreStub , self ) . MakeSyncCall ( <EOL> '<STR_LIT>' , '<STR_LIT>' , put_request , put_response ) <EOL> def _Dynamic_Delete ( self , delete_request , response ) : <EOL> if delete_request . has_transaction ( ) : <EOL> txid = delete_request . transaction ( ) . handle ( ) <EOL> txdata = self . __transactions [ txid ] <EOL> assert ( txdata . thread_id == <EOL> thread . get_ident ( ) ) , "<STR_LIT>" <EOL> for key in delete_request . key_list ( ) : <EOL> txdata . entities [ key . Encode ( ) ] = ( key , None ) <EOL> else : <EOL> super ( RemoteDatastoreStub , self ) . MakeSyncCall ( <EOL> '<STR_LIT>' , '<STR_LIT>' , delete_request , response ) <EOL> def _Dynamic_BeginTransaction ( self , request , transaction ) : <EOL> self . __local_tx_lock . acquire ( ) <EOL> try : <EOL> txid = self . __next_local_tx <EOL> self . __transactions [ txid ] = TransactionData ( thread . get_ident ( ) , <EOL> request . allow_multiple_eg ( ) ) <EOL> self . __next_local_tx += <NUM_LIT:1> <EOL> finally : <EOL> self . __local_tx_lock . release ( ) <EOL> transaction . set_handle ( txid ) <EOL> transaction . set_app ( request . app ( ) ) <EOL> def _Dynamic_Commit ( self , transaction , transaction_response ) : <EOL> txid = transaction . handle ( ) <EOL> if txid not in self . __transactions : <EOL> raise apiproxy_errors . ApplicationError ( <EOL> datastore_pb . Error . BAD_REQUEST , <EOL> '<STR_LIT>' % ( txid , ) ) <EOL> txdata = self . __transactions [ txid ] <EOL> assert ( txdata . thread_id == <EOL> thread . get_ident ( ) ) , "<STR_LIT>" <EOL> del self . __transactions [ txid ] <EOL> tx = remote_api_pb . TransactionRequest ( ) <EOL> tx . set_allow_multiple_eg ( txdata . is_xg ) <EOL> for key , hash in txdata . preconditions . values ( ) : <EOL> precond = tx . add_precondition ( ) <EOL> precond . mutable_key ( ) . CopyFrom ( key ) <EOL> if hash : <EOL> precond . set_hash ( hash ) <EOL> puts = tx . mutable_puts ( ) <EOL> deletes = tx . mutable_deletes ( ) <EOL> for key , entity in txdata . entities . values ( ) : <EOL> if entity : <EOL> puts . add_entity ( ) . CopyFrom ( entity ) <EOL> else : <EOL> deletes . add_key ( ) . CopyFrom ( key ) <EOL> super ( RemoteDatastoreStub , self ) . MakeSyncCall ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> tx , datastore_pb . PutResponse ( ) ) <EOL> def _Dynamic_Rollback ( self , transaction , transaction_response ) : <EOL> txid = transaction . handle ( ) <EOL> self . __local_tx_lock . acquire ( ) <EOL> try : <EOL> if txid not in self . __transactions : <EOL> raise apiproxy_errors . ApplicationError ( <EOL> datastore_pb . Error . BAD_REQUEST , <EOL> '<STR_LIT>' % ( txid , ) ) <EOL> txdata = self . __transactions [ txid ] <EOL> assert ( txdata . thread_id == <EOL> thread . get_ident ( ) ) , "<STR_LIT>" <EOL> del self . __transactions [ txid ] <EOL> finally : <EOL> self . __local_tx_lock . release ( ) <EOL> def _Dynamic_CreateIndex ( self , index , id_response ) : <EOL> raise apiproxy_errors . CapabilityDisabledError ( <EOL> '<STR_LIT>' ) <EOL> def _Dynamic_UpdateIndex ( self , index , void ) : <EOL> raise apiproxy_errors . CapabilityDisabledError ( <EOL> '<STR_LIT>' ) <EOL> def _Dynamic_DeleteIndex ( self , index , void ) : <EOL> raise apiproxy_errors . CapabilityDisabledError ( <EOL> '<STR_LIT>' ) <EOL> ALL_SERVICES = set ( remote_api_services . SERVICE_PB_MAP ) <EOL> def GetRemoteAppIdFromServer ( server , path , remote_token = None ) : <EOL> """<STR_LIT>""" <EOL> if not remote_token : <EOL> random . seed ( ) <EOL> remote_token = str ( random . random ( ) ) [ <NUM_LIT:2> : ] <EOL> remote_token = str ( remote_token ) <EOL> urlargs = { '<STR_LIT>' : remote_token } <EOL> response = server . Send ( path , payload = None , ** urlargs ) <EOL> if not response . startswith ( '<STR_LIT:{>' ) : <EOL> raise ConfigurationError ( <EOL> '<STR_LIT>' % response ) <EOL> app_info = yaml . load ( response ) <EOL> if not app_info or '<STR_LIT>' not in app_info or '<STR_LIT>' not in app_info : <EOL> raise ConfigurationError ( '<STR_LIT>' ) <EOL> if str ( app_info [ '<STR_LIT>' ] ) != remote_token : <EOL> raise ConfigurationError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( repr ( remote_token ) , <EOL> repr ( app_info [ '<STR_LIT>' ] ) ) ) <EOL> return app_info [ '<STR_LIT>' ] <EOL> def ConfigureRemoteApiFromServer ( server , path , app_id , services = None , <EOL> default_auth_domain = None , <EOL> use_remote_datastore = True ) : <EOL> """<STR_LIT>""" <EOL> if services is None : <EOL> services = set ( ALL_SERVICES ) <EOL> else : <EOL> services = set ( services ) <EOL> unsupported = services . difference ( ALL_SERVICES ) <EOL> if unsupported : <EOL> raise ConfigurationError ( '<STR_LIT>' <EOL> % ( '<STR_LIT:U+002CU+0020>' . join ( unsupported ) , ) ) <EOL> os . environ [ '<STR_LIT>' ] = app_id <EOL> os . environ . setdefault ( '<STR_LIT>' , default_auth_domain or '<STR_LIT>' ) <EOL> apiproxy_stub_map . apiproxy = apiproxy_stub_map . APIProxyStubMap ( ) <EOL> if '<STR_LIT>' in services and use_remote_datastore : <EOL> services . remove ( '<STR_LIT>' ) <EOL> datastore_stub = RemoteDatastoreStub ( server , path ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( '<STR_LIT>' , datastore_stub ) <EOL> stub = RemoteStub ( server , path ) <EOL> for service in services : <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( service , stub ) <EOL> def GetRemoteAppId ( servername , <EOL> path , <EOL> auth_func , <EOL> rpc_server_factory = appengine_rpc . HttpRpcServer , <EOL> rtok = None , <EOL> secure = False , <EOL> save_cookies = False ) : <EOL> """<STR_LIT>""" <EOL> server = rpc_server_factory ( servername , auth_func , GetUserAgent ( ) , <EOL> GetSourceName ( ) , save_cookies = save_cookies , <EOL> debug_data = False , secure = secure ) <EOL> app_id = GetRemoteAppIdFromServer ( server , path , rtok ) <EOL> return app_id , server <EOL> def ConfigureRemoteApi ( app_id , <EOL> path , <EOL> auth_func , <EOL> servername = None , <EOL> rpc_server_factory = appengine_rpc . HttpRpcServer , <EOL> rtok = None , <EOL> secure = False , <EOL> services = None , <EOL> default_auth_domain = None , <EOL> save_cookies = False , <EOL> use_remote_datastore = True ) : <EOL> """<STR_LIT>""" <EOL> if not servername and not app_id : <EOL> raise ConfigurationError ( '<STR_LIT>' ) <EOL> if not servername : <EOL> servername = '<STR_LIT>' % ( app_id , ) <EOL> server = rpc_server_factory ( servername , auth_func , GetUserAgent ( ) , <EOL> GetSourceName ( ) , save_cookies = save_cookies , <EOL> debug_data = False , secure = secure ) <EOL> if not app_id : <EOL> app_id = GetRemoteAppIdFromServer ( server , path , rtok ) <EOL> ConfigureRemoteApiFromServer ( server , path , app_id , services , <EOL> default_auth_domain , use_remote_datastore ) <EOL> return server <EOL> def MaybeInvokeAuthentication ( ) : <EOL> """<STR_LIT>""" <EOL> datastore_stub = apiproxy_stub_map . apiproxy . GetStub ( '<STR_LIT>' ) <EOL> if isinstance ( datastore_stub , RemoteStub ) : <EOL> datastore_stub . _server . Send ( datastore_stub . _path , payload = None ) <EOL> else : <EOL> raise ConfigurationError ( '<STR_LIT>' ) <EOL> ConfigureRemoteDatastore = ConfigureRemoteApi </s>
