<s> """<STR_LIT>""" <EOL> import os <EOL> from gcloud import connection <EOL> from gcloud . environment_vars import GCD_HOST <EOL> from gcloud . exceptions import make_exception <EOL> from gcloud . datastore . _generated import datastore_pb2 as _datastore_pb2 <EOL> from google . rpc import status_pb2 <EOL> class Connection ( connection . Connection ) : <EOL> """<STR_LIT>""" <EOL> API_BASE_URL = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> API_VERSION = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> API_URL_TEMPLATE = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> SCOPE = ( '<STR_LIT>' , ) <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , credentials = None , http = None , api_base_url = None ) : <EOL> super ( Connection , self ) . __init__ ( credentials = credentials , http = http ) <EOL> if api_base_url is None : <EOL> try : <EOL> api_base_url = '<STR_LIT>' % ( os . environ [ GCD_HOST ] , ) <EOL> except KeyError : <EOL> api_base_url = self . __class__ . API_BASE_URL <EOL> self . api_base_url = api_base_url <EOL> def _request ( self , project , method , data ) : <EOL> """<STR_LIT>""" <EOL> headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : str ( len ( data ) ) , <EOL> '<STR_LIT>' : self . USER_AGENT , <EOL> } <EOL> headers , content = self . http . request ( <EOL> uri = self . build_api_url ( project = project , method = method ) , <EOL> method = '<STR_LIT:POST>' , headers = headers , body = data ) <EOL> status = headers [ '<STR_LIT:status>' ] <EOL> if status != '<STR_LIT>' : <EOL> error_status = status_pb2 . Status . FromString ( content ) <EOL> raise make_exception ( headers , error_status . message , use_json = False ) <EOL> return content <EOL> def _rpc ( self , project , method , request_pb , response_pb_cls ) : <EOL> """<STR_LIT>""" <EOL> response = self . _request ( project = project , method = method , <EOL> data = request_pb . SerializeToString ( ) ) <EOL> return response_pb_cls . FromString ( response ) <EOL> def build_api_url ( self , project , method , base_url = None , <EOL> api_version = None ) : <EOL> """<STR_LIT>""" <EOL> return self . API_URL_TEMPLATE . format ( <EOL> api_base = ( base_url or self . api_base_url ) , <EOL> api_version = ( api_version or self . API_VERSION ) , <EOL> project = project , method = method ) <EOL> def lookup ( self , project , key_pbs , <EOL> eventual = False , transaction_id = None ) : <EOL> """<STR_LIT>""" <EOL> lookup_request = _datastore_pb2 . LookupRequest ( ) <EOL> _set_read_options ( lookup_request , eventual , transaction_id ) <EOL> _add_keys_to_request ( lookup_request . keys , key_pbs ) <EOL> lookup_response = self . _rpc ( project , '<STR_LIT>' , lookup_request , <EOL> _datastore_pb2 . LookupResponse ) <EOL> results = [ result . entity for result in lookup_response . found ] <EOL> missing = [ result . entity for result in lookup_response . missing ] <EOL> return results , missing , list ( lookup_response . deferred ) <EOL> def run_query ( self , project , query_pb , namespace = None , <EOL> eventual = False , transaction_id = None ) : <EOL> """<STR_LIT>""" <EOL> request = _datastore_pb2 . RunQueryRequest ( ) <EOL> _set_read_options ( request , eventual , transaction_id ) <EOL> if namespace : <EOL> request . partition_id . namespace_id = namespace <EOL> request . query . CopyFrom ( query_pb ) <EOL> response = self . _rpc ( project , '<STR_LIT>' , request , <EOL> _datastore_pb2 . RunQueryResponse ) <EOL> return ( <EOL> [ e . entity for e in response . batch . entity_results ] , <EOL> response . batch . end_cursor , <EOL> response . batch . more_results , <EOL> response . batch . skipped_results , <EOL> ) <EOL> def begin_transaction ( self , project ) : <EOL> """<STR_LIT>""" <EOL> request = _datastore_pb2 . BeginTransactionRequest ( ) <EOL> response = self . _rpc ( project , '<STR_LIT>' , request , <EOL> _datastore_pb2 . BeginTransactionResponse ) <EOL> return response . transaction <EOL> def commit ( self , project , request , transaction_id ) : <EOL> """<STR_LIT>""" <EOL> if transaction_id : <EOL> request . mode = _datastore_pb2 . CommitRequest . TRANSACTIONAL <EOL> request . transaction = transaction_id <EOL> else : <EOL> request . mode = _datastore_pb2 . CommitRequest . NON_TRANSACTIONAL <EOL> response = self . _rpc ( project , '<STR_LIT>' , request , <EOL> _datastore_pb2 . CommitResponse ) <EOL> return _parse_commit_response ( response ) <EOL> def rollback ( self , project , transaction_id ) : <EOL> """<STR_LIT>""" <EOL> request = _datastore_pb2 . RollbackRequest ( ) <EOL> request . transaction = transaction_id <EOL> self . _rpc ( project , '<STR_LIT>' , request , <EOL> _datastore_pb2 . RollbackResponse ) <EOL> def allocate_ids ( self , project , key_pbs ) : <EOL> """<STR_LIT>""" <EOL> request = _datastore_pb2 . AllocateIdsRequest ( ) <EOL> _add_keys_to_request ( request . keys , key_pbs ) <EOL> response = self . _rpc ( project , '<STR_LIT>' , request , <EOL> _datastore_pb2 . AllocateIdsResponse ) <EOL> return list ( response . keys ) <EOL> def _set_read_options ( request , eventual , transaction_id ) : <EOL> """<STR_LIT>""" <EOL> if eventual and ( transaction_id is not None ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> opts = request . read_options <EOL> if eventual : <EOL> opts . read_consistency = _datastore_pb2 . ReadOptions . EVENTUAL <EOL> elif transaction_id : <EOL> opts . transaction = transaction_id <EOL> def _add_keys_to_request ( request_field_pb , key_pbs ) : <EOL> """<STR_LIT>""" <EOL> for key_pb in key_pbs : <EOL> request_field_pb . add ( ) . CopyFrom ( key_pb ) <EOL> def _parse_commit_response ( commit_response_pb ) : <EOL> """<STR_LIT>""" <EOL> mut_results = commit_response_pb . mutation_results <EOL> index_updates = commit_response_pb . index_updates <EOL> completed_keys = [ mut_result . key for mut_result in mut_results <EOL> if mut_result . HasField ( '<STR_LIT:key>' ) ] <EOL> return index_updates , completed_keys </s>
