<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> __all__ = [ ] <EOL> import datetime <EOL> import logging <EOL> import os . path <EOL> import random <EOL> import string <EOL> import taskqueue_service_pb <EOL> from google . appengine . api import api_base_pb <EOL> from google . appengine . api import apiproxy_stub <EOL> from google . appengine . api import apiproxy_stub_map <EOL> from google . appengine . runtime import apiproxy_errors <EOL> from google . appengine . ext . remote_api import remote_api_pb <EOL> DEFAULT_RATE = '<STR_LIT>' <EOL> DEFAULT_RATE_FLOAT = <NUM_LIT> <EOL> DEFAULT_BUCKET_SIZE = <NUM_LIT:5> <EOL> MAX_ETA = datetime . timedelta ( days = <NUM_LIT:30> ) <EOL> MAX_PULL_TASK_SIZE_BYTES = <NUM_LIT:2> ** <NUM_LIT:20> <EOL> MAX_PUSH_TASK_SIZE_BYTES = <NUM_LIT:100> * ( <NUM_LIT:2> ** <NUM_LIT:10> ) <EOL> MAX_TASK_SIZE = MAX_PUSH_TASK_SIZE_BYTES <EOL> MAX_REQUEST_SIZE = <NUM_LIT:32> << <NUM_LIT:20> <EOL> BUILT_IN_HEADERS = set ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> DEFAULT_QUEUE_NAME = '<STR_LIT:default>' <EOL> INF = <NUM_LIT> <EOL> QUEUE_MODE = taskqueue_service_pb . TaskQueueMode <EOL> AUTOMATIC_QUEUES = { <EOL> DEFAULT_QUEUE_NAME : ( <NUM_LIT> , DEFAULT_BUCKET_SIZE , DEFAULT_RATE ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:1> , <NUM_LIT:1> , '<STR_LIT>' ) } <EOL> CERT_LOCATION = "<STR_LIT>" <EOL> KEY_LOCATION = "<STR_LIT>" <EOL> TASKQUEUE_LOCATION_FILE = "<STR_LIT>" <EOL> TASKQUEUE_SERVER_PORT = <NUM_LIT> <EOL> class TaskQueueServiceStub ( apiproxy_stub . APIProxyStub ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app_id , host , port , service_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> super ( TaskQueueServiceStub , self ) . __init__ ( <EOL> service_name , max_request_size = MAX_REQUEST_SIZE ) <EOL> self . __app_id = app_id <EOL> self . __nginx_host = host <EOL> self . __nginx_port = port <EOL> self . __tq_location = self . __GetTQLocation ( ) <EOL> def __GetTQLocation ( self ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( TASKQUEUE_LOCATION_FILE ) : <EOL> tq_file = open ( TASKQUEUE_LOCATION_FILE ) <EOL> location = tq_file . read ( ) <EOL> tq_file . close ( ) <EOL> else : <EOL> location = "<STR_LIT:localhost>" <EOL> location += "<STR_LIT::>" + str ( TASKQUEUE_SERVER_PORT ) <EOL> return location <EOL> def _ChooseTaskName ( self , app_name , queue_name , user_chosen = None ) : <EOL> """<STR_LIT>""" <EOL> RAND_LENGTH_SIZE = <NUM_LIT:32> <EOL> if not user_chosen : <EOL> user_chosen = '<STR_LIT>' . join ( random . choice ( string . ascii_uppercase + string . digits ) for x in range ( RAND_LENGTH_SIZE ) ) <EOL> return '<STR_LIT>' % ( app_name , queue_name , user_chosen ) <EOL> def _AddTransactionalBulkTask ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> for add_request in request . add_request_list ( ) : <EOL> task_result = response . add_taskresult ( ) <EOL> task_name = None <EOL> if add_request . has_task_name ( ) : <EOL> task_name = add_request . task_name ( ) <EOL> namespaced_name = self . _ChooseTaskName ( add_request . app_id ( ) , <EOL> add_request . queue_name ( ) , <EOL> user_chosen = task_name ) <EOL> add_request . set_task_name ( namespaced_name ) <EOL> task_result . set_chosen_task_name ( namespaced_name ) <EOL> for add_request , task_result in zip ( request . add_request_list ( ) , <EOL> response . taskresult_list ( ) ) : <EOL> task_result . set_result ( taskqueue_service_pb . TaskQueueServiceError . OK ) <EOL> try : <EOL> apiproxy_stub_map . MakeSyncCall ( <EOL> '<STR_LIT>' , '<STR_LIT>' , request , api_base_pb . VoidProto ( ) ) <EOL> except apiproxy_errors . ApplicationError , e : <EOL> raise apiproxy_errors . ApplicationError ( <EOL> e . application_error + <EOL> taskqueue_service_pb . TaskQueueServiceError . DATASTORE_ERROR , <EOL> e . error_detail ) <EOL> return response <EOL> def _Dynamic_Add ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> bulk_request = taskqueue_service_pb . TaskQueueBulkAddRequest ( ) <EOL> bulk_response = taskqueue_service_pb . TaskQueueBulkAddResponse ( ) <EOL> bulk_request . add_add_request ( ) . CopyFrom ( request ) <EOL> self . _Dynamic_BulkAdd ( bulk_request , bulk_response ) <EOL> assert bulk_response . taskresult_size ( ) == <NUM_LIT:1> <EOL> result = bulk_response . taskresult ( <NUM_LIT:0> ) . result ( ) <EOL> if result != taskqueue_service_pb . TaskQueueServiceError . OK : <EOL> raise apiproxy_errors . ApplicationError ( result ) <EOL> elif bulk_response . taskresult ( <NUM_LIT:0> ) . has_chosen_task_name ( ) : <EOL> response . set_chosen_task_name ( <EOL> bulk_response . taskresult ( <NUM_LIT:0> ) . chosen_task_name ( ) ) <EOL> def _Dynamic_BulkAdd ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> assert request . add_request_size ( ) , '<STR_LIT>' <EOL> if request . add_request ( <NUM_LIT:0> ) . has_transaction ( ) : <EOL> self . _AddTransactionalBulkTask ( request , response ) <EOL> return response <EOL> for add_request in request . add_request_list ( ) : <EOL> add_request . set_app_id ( self . __app_id ) <EOL> url = add_request . url ( ) <EOL> url = "<STR_LIT>" + self . __nginx_host + "<STR_LIT::>" + str ( self . __nginx_port ) + url <EOL> add_request . set_url ( url ) <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> return response <EOL> def _Dynamic_UpdateQueue ( self , request , unused_response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , unused_response , "<STR_LIT>" ) <EOL> return unused_response <EOL> def _Dynamic_FetchQueues ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> return response <EOL> def _Dynamic_FetchQueueStats ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> return response <EOL> def _Dynamic_QueryTasks ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> return response <EOL> def _Dynamic_FetchTask ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> return response <EOL> def _Dynamic_Delete ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> return response <EOL> def _Dynamic_ForceRun ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> return response <EOL> def _Dynamic_DeleteQueue ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> return response <EOL> def _Dynamic_PauseQueue ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> return response <EOL> def _Dynamic_PurgeQueue ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> return response <EOL> def _Dynamic_DeleteGroup ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> def _Dynamic_UpdateStorageLimit ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> def _Dynamic_QueryAndOwnTasks ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> def _Dynamic_ModifyTaskLease ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _RemoteSend ( request , response , "<STR_LIT>" ) <EOL> def _RemoteSend ( self , request , response , method ) : <EOL> """<STR_LIT>""" <EOL> tag = self . __app_id <EOL> api_request = remote_api_pb . Request ( ) <EOL> api_request . set_method ( method ) <EOL> api_request . set_service_name ( "<STR_LIT>" ) <EOL> api_request . set_request ( request . Encode ( ) ) <EOL> api_response = remote_api_pb . Response ( ) <EOL> api_response = api_request . sendCommand ( self . __tq_location , <EOL> tag , <EOL> api_response , <EOL> <NUM_LIT:1> , <EOL> False , <EOL> KEY_LOCATION , <EOL> CERT_LOCATION ) <EOL> if not api_response or not api_response . has_response ( ) : <EOL> raise apiproxy_errors . ApplicationError ( <EOL> taskqueue_service_pb . TaskQueueServiceError . INTERNAL_ERROR ) <EOL> if api_response . has_application_error ( ) : <EOL> error_pb = api_response . application_error ( ) <EOL> logging . error ( error_pb . detail ( ) ) <EOL> raise apiproxy_errors . ApplicationError ( error_pb . code ( ) , <EOL> error_pb . detail ( ) ) <EOL> if api_response . has_exception ( ) : <EOL> raise api_response . exception ( ) <EOL> response . ParseFromString ( api_response . response ( ) ) </s>
