<s> """<STR_LIT>""" <EOL> import cPickle <EOL> import cStringIO <EOL> import hashlib <EOL> import math <EOL> import types <EOL> from google . appengine . api import api_base_pb <EOL> from google . appengine . api import apiproxy_stub_map <EOL> from google . appengine . api import capabilities <EOL> from google . appengine . api import namespace_manager <EOL> from google . appengine . api . memcache import memcache_service_pb <EOL> from google . appengine . runtime import apiproxy_errors <EOL> MemcacheSetResponse = memcache_service_pb . MemcacheSetResponse <EOL> MemcacheSetRequest = memcache_service_pb . MemcacheSetRequest <EOL> MemcacheGetResponse = memcache_service_pb . MemcacheGetResponse <EOL> MemcacheGetRequest = memcache_service_pb . MemcacheGetRequest <EOL> MemcacheDeleteResponse = memcache_service_pb . MemcacheDeleteResponse <EOL> MemcacheDeleteRequest = memcache_service_pb . MemcacheDeleteRequest <EOL> MemcacheIncrementResponse = memcache_service_pb . MemcacheIncrementResponse <EOL> MemcacheIncrementRequest = memcache_service_pb . MemcacheIncrementRequest <EOL> MemcacheBatchIncrementResponse = memcache_service_pb . MemcacheBatchIncrementResponse <EOL> MemcacheBatchIncrementRequest = memcache_service_pb . MemcacheBatchIncrementRequest <EOL> MemcacheFlushResponse = memcache_service_pb . MemcacheFlushResponse <EOL> MemcacheFlushRequest = memcache_service_pb . MemcacheFlushRequest <EOL> MemcacheStatsRequest = memcache_service_pb . MemcacheStatsRequest <EOL> MemcacheStatsResponse = memcache_service_pb . MemcacheStatsResponse <EOL> DELETE_NETWORK_FAILURE = <NUM_LIT:0> <EOL> DELETE_ITEM_MISSING = <NUM_LIT:1> <EOL> DELETE_SUCCESSFUL = <NUM_LIT:2> <EOL> STORED = MemcacheSetResponse . STORED <EOL> NOT_STORED = MemcacheSetResponse . NOT_STORED <EOL> ERROR = MemcacheSetResponse . ERROR <EOL> EXISTS = MemcacheSetResponse . EXISTS <EOL> MAX_KEY_SIZE = <NUM_LIT> <EOL> MAX_VALUE_SIZE = <NUM_LIT:10> ** <NUM_LIT:6> <EOL> STAT_HITS = '<STR_LIT>' <EOL> STAT_MISSES = '<STR_LIT>' <EOL> STAT_BYTE_HITS = '<STR_LIT>' <EOL> STAT_ITEMS = '<STR_LIT>' <EOL> STAT_BYTES = '<STR_LIT>' <EOL> STAT_OLDEST_ITEM_AGES = '<STR_LIT>' <EOL> FLAG_TYPE_MASK = <NUM_LIT:7> <EOL> FLAG_COMPRESSED = <NUM_LIT:1> << <NUM_LIT:3> <EOL> TYPE_STR = <NUM_LIT:0> <EOL> TYPE_UNICODE = <NUM_LIT:1> <EOL> TYPE_PICKLED = <NUM_LIT:2> <EOL> TYPE_INT = <NUM_LIT:3> <EOL> TYPE_LONG = <NUM_LIT:4> <EOL> TYPE_BOOL = <NUM_LIT:5> <EOL> CAPABILITY = capabilities . CapabilitySet ( '<STR_LIT>' ) <EOL> def _is_pair ( obj ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( obj , tuple ) and len ( obj ) == <NUM_LIT:2> <EOL> def _add_name_space ( message , namespace = None ) : <EOL> """<STR_LIT>""" <EOL> if namespace is None : <EOL> namespace = namespace_manager . get_namespace ( ) <EOL> if not namespace : <EOL> message . clear_name_space ( ) <EOL> else : <EOL> message . set_name_space ( namespace ) <EOL> def _key_string ( key , key_prefix = '<STR_LIT>' , server_to_user_dict = None ) : <EOL> """<STR_LIT>""" <EOL> if _is_pair ( key ) : <EOL> key = key [ <NUM_LIT:1> ] <EOL> if not isinstance ( key , basestring ) : <EOL> raise TypeError ( '<STR_LIT>' % key ) <EOL> if not isinstance ( key_prefix , basestring ) : <EOL> raise TypeError ( '<STR_LIT>' % <EOL> key_prefix ) <EOL> server_key = key_prefix + key <EOL> if isinstance ( server_key , unicode ) : <EOL> server_key = server_key . encode ( '<STR_LIT:utf-8>' ) <EOL> if len ( server_key ) > MAX_KEY_SIZE : <EOL> server_key = hashlib . sha1 ( server_key ) . hexdigest ( ) <EOL> if server_to_user_dict is not None : <EOL> assert isinstance ( server_to_user_dict , dict ) <EOL> server_to_user_dict [ server_key ] = key <EOL> return server_key <EOL> def _validate_encode_value ( value , do_pickle ) : <EOL> """<STR_LIT>""" <EOL> flags = <NUM_LIT:0> <EOL> stored_value = value <EOL> if isinstance ( value , str ) : <EOL> pass <EOL> elif isinstance ( value , unicode ) : <EOL> stored_value = value . encode ( '<STR_LIT:utf-8>' ) <EOL> flags |= TYPE_UNICODE <EOL> elif isinstance ( value , bool ) : <EOL> stored_value = str ( int ( value ) ) <EOL> flags |= TYPE_BOOL <EOL> elif isinstance ( value , int ) : <EOL> stored_value = str ( value ) <EOL> flags |= TYPE_INT <EOL> elif isinstance ( value , long ) : <EOL> stored_value = str ( value ) <EOL> flags |= TYPE_LONG <EOL> else : <EOL> stored_value = do_pickle ( value ) <EOL> flags |= TYPE_PICKLED <EOL> if len ( stored_value ) > MAX_VALUE_SIZE : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( MAX_VALUE_SIZE , len ( stored_value ) ) ) <EOL> return ( stored_value , flags ) <EOL> def _decode_value ( stored_value , flags , do_unpickle ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( stored_value , str ) <EOL> assert isinstance ( flags , ( int , long ) ) <EOL> type_number = flags & FLAG_TYPE_MASK <EOL> value = stored_value <EOL> if type_number == TYPE_STR : <EOL> return value <EOL> elif type_number == TYPE_UNICODE : <EOL> return unicode ( value , '<STR_LIT:utf-8>' ) <EOL> elif type_number == TYPE_PICKLED : <EOL> return do_unpickle ( value ) <EOL> elif type_number == TYPE_BOOL : <EOL> return bool ( int ( value ) ) <EOL> elif type_number == TYPE_INT : <EOL> return int ( value ) <EOL> elif type_number == TYPE_LONG : <EOL> return long ( value ) <EOL> else : <EOL> assert False , "<STR_LIT>" <EOL> assert False , "<STR_LIT>" <EOL> def create_rpc ( deadline = None , callback = None ) : <EOL> """<STR_LIT>""" <EOL> return apiproxy_stub_map . UserRPC ( '<STR_LIT>' , deadline , callback ) <EOL> class Client ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , servers = None , debug = <NUM_LIT:0> , <EOL> pickleProtocol = cPickle . HIGHEST_PROTOCOL , <EOL> pickler = cPickle . Pickler , <EOL> unpickler = cPickle . Unpickler , <EOL> pload = None , <EOL> pid = None , <EOL> make_sync_call = None , <EOL> _app_id = None ) : <EOL> """<STR_LIT>""" <EOL> self . _pickler_factory = pickler <EOL> self . _unpickler_factory = unpickler <EOL> self . _pickle_protocol = pickleProtocol <EOL> self . _persistent_id = pid <EOL> self . _persistent_load = pload <EOL> self . _app_id = _app_id <EOL> self . _cas_ids = { } <EOL> def cas_reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _cas_ids . clear ( ) <EOL> def _make_async_call ( self , rpc , method , request , response , <EOL> get_result_hook , user_data ) : <EOL> """<STR_LIT>""" <EOL> if rpc is None : <EOL> rpc = create_rpc ( ) <EOL> assert rpc . service == '<STR_LIT>' , repr ( rpc . service ) <EOL> rpc . make_call ( method , request , response , get_result_hook , user_data ) <EOL> return rpc <EOL> def _do_pickle ( self , value ) : <EOL> """<STR_LIT>""" <EOL> pickle_data = cStringIO . StringIO ( ) <EOL> pickler = self . _pickler_factory ( pickle_data , <EOL> protocol = self . _pickle_protocol ) <EOL> if self . _persistent_id is not None : <EOL> pickler . persistent_id = self . _persistent_id <EOL> pickler . dump ( value ) <EOL> return pickle_data . getvalue ( ) <EOL> def _do_unpickle ( self , value ) : <EOL> """<STR_LIT>""" <EOL> pickle_data = cStringIO . StringIO ( value ) <EOL> unpickler = self . _unpickler_factory ( pickle_data ) <EOL> if self . _persistent_load is not None : <EOL> unpickler . persistent_load = self . _persistent_load <EOL> return unpickler . load ( ) <EOL> def _add_app_id ( self , message ) : <EOL> """<STR_LIT>""" <EOL> if self . _app_id : <EOL> app_override = message . mutable_override ( ) <EOL> app_override . set_app_id ( self . _app_id ) <EOL> def set_servers ( self , servers ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def disconnect_all ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def forget_dead_hosts ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def debuglog ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_stats ( self ) : <EOL> """<STR_LIT>""" <EOL> rpc = self . get_stats_async ( ) <EOL> return rpc . get_result ( ) <EOL> def get_stats_async ( self , rpc = None ) : <EOL> """<STR_LIT>""" <EOL> request = MemcacheStatsRequest ( ) <EOL> self . _add_app_id ( request ) <EOL> response = MemcacheStatsResponse ( ) <EOL> return self . _make_async_call ( rpc , '<STR_LIT>' , request , response , <EOL> self . __get_stats_hook , None ) <EOL> def __get_stats_hook ( self , rpc ) : <EOL> try : <EOL> rpc . check_success ( ) <EOL> except apiproxy_errors . Error : <EOL> return None <EOL> response = rpc . response <EOL> if not response . has_stats ( ) : <EOL> return { <EOL> STAT_HITS : <NUM_LIT:0> , <EOL> STAT_MISSES : <NUM_LIT:0> , <EOL> STAT_BYTE_HITS : <NUM_LIT:0> , <EOL> STAT_ITEMS : <NUM_LIT:0> , <EOL> STAT_BYTES : <NUM_LIT:0> , <EOL> STAT_OLDEST_ITEM_AGES : <NUM_LIT:0> , <EOL> } <EOL> stats = response . stats ( ) <EOL> return { <EOL> STAT_HITS : stats . hits ( ) , <EOL> STAT_MISSES : stats . misses ( ) , <EOL> STAT_BYTE_HITS : stats . byte_hits ( ) , <EOL> STAT_ITEMS : stats . items ( ) , <EOL> STAT_BYTES : stats . bytes ( ) , <EOL> STAT_OLDEST_ITEM_AGES : stats . oldest_item_age ( ) , <EOL> } <EOL> def flush_all ( self ) : <EOL> """<STR_LIT>""" <EOL> rpc = self . flush_all_async ( ) <EOL> return rpc . get_result ( ) <EOL> def flush_all_async ( self , rpc = None ) : <EOL> """<STR_LIT>""" <EOL> request = MemcacheFlushRequest ( ) <EOL> self . _add_app_id ( request ) <EOL> response = MemcacheFlushResponse ( ) <EOL> return self . _make_async_call ( rpc , '<STR_LIT>' , request , response , <EOL> self . __flush_all_hook , None ) <EOL> def __flush_all_hook ( self , rpc ) : <EOL> try : <EOL> rpc . check_success ( ) <EOL> except apiproxy_errors . Error : <EOL> return False <EOL> return True <EOL> def get ( self , key , namespace = None , for_cas = False ) : <EOL> """<STR_LIT>""" <EOL> if _is_pair ( key ) : <EOL> key = key [ <NUM_LIT:1> ] <EOL> rpc = self . get_multi_async ( [ key ] , namespace = namespace , for_cas = for_cas ) <EOL> results = rpc . get_result ( ) <EOL> return results . get ( key ) <EOL> def gets ( self , key , namespace = None ) : <EOL> """<STR_LIT>""" <EOL> return self . get ( key , namespace = namespace , for_cas = True ) <EOL> def get_multi ( self , keys , key_prefix = '<STR_LIT>' , namespace = None , for_cas = False ) : <EOL> """<STR_LIT>""" <EOL> rpc = self . get_multi_async ( keys , key_prefix , namespace , for_cas ) <EOL> return rpc . get_result ( ) <EOL> def get_multi_async ( self , keys , key_prefix = '<STR_LIT>' , namespace = None , <EOL> for_cas = False , rpc = None ) : <EOL> """<STR_LIT>""" <EOL> request = MemcacheGetRequest ( ) <EOL> self . _add_app_id ( request ) <EOL> _add_name_space ( request , namespace ) <EOL> if for_cas : <EOL> request . set_for_cas ( True ) <EOL> response = MemcacheGetResponse ( ) <EOL> user_key = { } <EOL> for key in keys : <EOL> request . add_key ( _key_string ( key , key_prefix , user_key ) ) <EOL> return self . _make_async_call ( rpc , '<STR_LIT>' , request , response , <EOL> self . __get_hook , user_key ) <EOL> def __get_hook ( self , rpc ) : <EOL> try : <EOL> rpc . check_success ( ) <EOL> except apiproxy_errors . Error : <EOL> return { } <EOL> for_cas = rpc . request . for_cas ( ) <EOL> response = rpc . response <EOL> user_key = rpc . user_data <EOL> return_value = { } <EOL> for returned_item in response . item_list ( ) : <EOL> value = _decode_value ( returned_item . value ( ) , returned_item . flags ( ) , <EOL> self . _do_unpickle ) <EOL> raw_key = returned_item . key ( ) <EOL> if for_cas : <EOL> self . _cas_ids [ raw_key ] = returned_item . cas_id ( ) <EOL> return_value [ user_key [ raw_key ] ] = value <EOL> return return_value <EOL> def delete ( self , key , seconds = <NUM_LIT:0> , namespace = None ) : <EOL> """<STR_LIT>""" <EOL> rpc = self . delete_multi_async ( [ key ] , seconds , namespace = namespace ) <EOL> results = rpc . get_result ( ) <EOL> if not results : <EOL> return DELETE_NETWORK_FAILURE <EOL> return results [ <NUM_LIT:0> ] <EOL> def delete_multi ( self , keys , seconds = <NUM_LIT:0> , key_prefix = '<STR_LIT>' , namespace = None ) : <EOL> """<STR_LIT>""" <EOL> rpc = self . delete_multi_async ( keys , seconds , key_prefix , namespace ) <EOL> results = rpc . get_result ( ) <EOL> return bool ( results ) <EOL> def delete_multi_async ( self , keys , seconds = <NUM_LIT:0> , key_prefix = '<STR_LIT>' , <EOL> namespace = None , rpc = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( seconds , ( int , long , float ) ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if seconds < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> request = MemcacheDeleteRequest ( ) <EOL> self . _add_app_id ( request ) <EOL> _add_name_space ( request , namespace ) <EOL> response = MemcacheDeleteResponse ( ) <EOL> for key in keys : <EOL> delete_item = request . add_item ( ) <EOL> delete_item . set_key ( _key_string ( key , key_prefix = key_prefix ) ) <EOL> delete_item . set_delete_time ( int ( math . ceil ( seconds ) ) ) <EOL> return self . _make_async_call ( rpc , '<STR_LIT>' , request , response , <EOL> self . __delete_hook , None ) <EOL> def __delete_hook ( self , rpc ) : <EOL> try : <EOL> rpc . check_success ( ) <EOL> except apiproxy_errors . Error : <EOL> return None <EOL> result = [ ] <EOL> for status in rpc . response . delete_status_list ( ) : <EOL> if status == MemcacheDeleteResponse . DELETED : <EOL> result . append ( DELETE_SUCCESSFUL ) <EOL> elif status == MemcacheDeleteResponse . NOT_FOUND : <EOL> result . append ( DELETE_ITEM_MISSING ) <EOL> else : <EOL> result . append ( DELETE_NETWORK_FAILURE ) <EOL> return result <EOL> def set ( self , key , value , time = <NUM_LIT:0> , min_compress_len = <NUM_LIT:0> , namespace = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_with_policy ( MemcacheSetRequest . SET , key , value , time = time , <EOL> namespace = namespace ) <EOL> def add ( self , key , value , time = <NUM_LIT:0> , min_compress_len = <NUM_LIT:0> , namespace = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_with_policy ( MemcacheSetRequest . ADD , key , value , time = time , <EOL> namespace = namespace ) <EOL> def replace ( self , key , value , time = <NUM_LIT:0> , min_compress_len = <NUM_LIT:0> , namespace = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_with_policy ( MemcacheSetRequest . REPLACE , <EOL> key , value , time = time , namespace = namespace ) <EOL> def cas ( self , key , value , time = <NUM_LIT:0> , min_compress_len = <NUM_LIT:0> , namespace = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_with_policy ( MemcacheSetRequest . CAS , key , value , <EOL> time , namespace ) <EOL> def _set_with_policy ( self , policy , key , value , time = <NUM_LIT:0> , namespace = None ) : <EOL> """<STR_LIT>""" <EOL> rpc = self . _set_multi_async_with_policy ( policy , { key : value } , <EOL> time , '<STR_LIT>' , namespace ) <EOL> status_dict = rpc . get_result ( ) <EOL> if not status_dict : <EOL> return False <EOL> return status_dict . get ( key ) == MemcacheSetResponse . STORED <EOL> def _set_multi_with_policy ( self , policy , mapping , time = <NUM_LIT:0> , key_prefix = '<STR_LIT>' , <EOL> namespace = None ) : <EOL> """<STR_LIT>""" <EOL> rpc = self . _set_multi_async_with_policy ( policy , mapping , time , <EOL> key_prefix , namespace ) <EOL> status_dict = rpc . get_result ( ) <EOL> server_keys , user_key = rpc . user_data <EOL> if not status_dict : <EOL> return user_key . values ( ) <EOL> unset_list = [ ] <EOL> for server_key in server_keys : <EOL> key = user_key [ server_key ] <EOL> set_status = status_dict [ key ] <EOL> if set_status != MemcacheSetResponse . STORED : <EOL> unset_list . append ( key ) <EOL> return unset_list <EOL> def _set_multi_async_with_policy ( self , policy , mapping , time = <NUM_LIT:0> , <EOL> key_prefix = '<STR_LIT>' , namespace = None , rpc = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( time , ( int , long , float ) ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if time < <NUM_LIT:0.0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> request = MemcacheSetRequest ( ) <EOL> self . _add_app_id ( request ) <EOL> _add_name_space ( request , namespace ) <EOL> user_key = { } <EOL> server_keys = [ ] <EOL> set_cas_id = ( policy == MemcacheSetRequest . CAS ) <EOL> for key , value in mapping . iteritems ( ) : <EOL> server_key = _key_string ( key , key_prefix , user_key ) <EOL> stored_value , flags = _validate_encode_value ( value , self . _do_pickle ) <EOL> server_keys . append ( server_key ) <EOL> item = request . add_item ( ) <EOL> item . set_key ( server_key ) <EOL> item . set_value ( stored_value ) <EOL> item . set_flags ( flags ) <EOL> item . set_set_policy ( policy ) <EOL> item . set_expiration_time ( int ( math . ceil ( time ) ) ) <EOL> if set_cas_id : <EOL> cas_id = self . _cas_ids . get ( server_key ) <EOL> if cas_id is not None : <EOL> item . set_cas_id ( cas_id ) <EOL> item . set_for_cas ( True ) <EOL> response = MemcacheSetResponse ( ) <EOL> return self . _make_async_call ( rpc , '<STR_LIT>' , request , response , <EOL> self . __set_with_policy_hook , <EOL> ( server_keys , user_key ) ) <EOL> def __set_with_policy_hook ( self , rpc ) : <EOL> try : <EOL> rpc . check_success ( ) <EOL> except apiproxy_errors . Error : <EOL> return None <EOL> response = rpc . response <EOL> server_keys , user_key = rpc . user_data <EOL> assert response . set_status_size ( ) == len ( server_keys ) <EOL> status_dict = { } <EOL> for server_key , status in zip ( server_keys , response . set_status_list ( ) ) : <EOL> status_dict [ user_key [ server_key ] ] = status <EOL> return status_dict <EOL> def set_multi ( self , mapping , time = <NUM_LIT:0> , key_prefix = '<STR_LIT>' , min_compress_len = <NUM_LIT:0> , <EOL> namespace = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_multi_with_policy ( MemcacheSetRequest . SET , mapping , <EOL> time = time , key_prefix = key_prefix , <EOL> namespace = namespace ) <EOL> def set_multi_async ( self , mapping , time = <NUM_LIT:0> , key_prefix = '<STR_LIT>' , <EOL> min_compress_len = <NUM_LIT:0> , namespace = None , rpc = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_multi_async_with_policy ( MemcacheSetRequest . SET , mapping , <EOL> time = time , key_prefix = key_prefix , <EOL> namespace = namespace , rpc = rpc ) <EOL> def add_multi ( self , mapping , time = <NUM_LIT:0> , key_prefix = '<STR_LIT>' , min_compress_len = <NUM_LIT:0> , <EOL> namespace = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_multi_with_policy ( MemcacheSetRequest . ADD , mapping , <EOL> time = time , key_prefix = key_prefix , <EOL> namespace = namespace ) <EOL> def add_multi_async ( self , mapping , time = <NUM_LIT:0> , key_prefix = '<STR_LIT>' , <EOL> min_compress_len = <NUM_LIT:0> , namespace = None , rpc = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_multi_async_with_policy ( MemcacheSetRequest . ADD , mapping , <EOL> time = time , key_prefix = key_prefix , <EOL> namespace = namespace , rpc = rpc ) <EOL> def replace_multi ( self , mapping , time = <NUM_LIT:0> , key_prefix = '<STR_LIT>' , min_compress_len = <NUM_LIT:0> , <EOL> namespace = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_multi_with_policy ( MemcacheSetRequest . REPLACE , mapping , <EOL> time = time , key_prefix = key_prefix , <EOL> namespace = namespace ) <EOL> def replace_multi_async ( self , mapping , time = <NUM_LIT:0> , key_prefix = '<STR_LIT>' , <EOL> min_compress_len = <NUM_LIT:0> , namespace = None , rpc = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_multi_async_with_policy ( MemcacheSetRequest . REPLACE , <EOL> mapping , <EOL> time = time , key_prefix = key_prefix , <EOL> namespace = namespace , rpc = rpc ) <EOL> def cas_multi ( self , mapping , time = <NUM_LIT:0> , key_prefix = '<STR_LIT>' , min_compress_len = <NUM_LIT:0> , <EOL> namespace = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_multi_with_policy ( MemcacheSetRequest . CAS , mapping , <EOL> time = time , key_prefix = key_prefix , <EOL> namespace = namespace ) <EOL> def cas_multi_async ( self , mapping , time = <NUM_LIT:0> , key_prefix = '<STR_LIT>' , <EOL> min_compress_len = <NUM_LIT:0> , namespace = None , rpc = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_multi_async_with_policy ( MemcacheSetRequest . CAS , mapping , <EOL> time = time , key_prefix = key_prefix , <EOL> namespace = namespace , rpc = rpc ) <EOL> def incr ( self , key , delta = <NUM_LIT:1> , namespace = None , initial_value = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _incrdecr ( key , False , delta , namespace = namespace , <EOL> initial_value = initial_value ) <EOL> def incr_async ( self , key , delta = <NUM_LIT:1> , namespace = None , initial_value = None , <EOL> rpc = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _incrdecr_async ( key , False , delta , namespace = namespace , <EOL> initial_value = initial_value , rpc = rpc ) <EOL> def decr ( self , key , delta = <NUM_LIT:1> , namespace = None , initial_value = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _incrdecr ( key , True , delta , namespace = namespace , <EOL> initial_value = initial_value ) <EOL> def decr_async ( self , key , delta = <NUM_LIT:1> , namespace = None , initial_value = None , <EOL> rpc = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _incrdecr_async ( key , True , delta , namespace = namespace , <EOL> initial_value = initial_value , rpc = rpc ) <EOL> def _incrdecr ( self , key , is_negative , delta , namespace = None , <EOL> initial_value = None ) : <EOL> """<STR_LIT>""" <EOL> rpc = self . _incrdecr_async ( key , is_negative , delta , namespace , <EOL> initial_value ) <EOL> return rpc . get_result ( ) <EOL> def _incrdecr_async ( self , key , is_negative , delta , namespace = None , <EOL> initial_value = None , rpc = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( delta , ( int , long ) ) : <EOL> raise TypeError ( '<STR_LIT>' % delta ) <EOL> if delta < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not isinstance ( key , basestring ) : <EOL> try : <EOL> it = iter ( key ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> if is_negative : <EOL> delta = - delta <EOL> return self . offset_multi_async ( dict ( ( k , delta ) for k in it ) , <EOL> namespace = namespace , <EOL> initial_value = initial_value , <EOL> rpc = rpc ) <EOL> request = MemcacheIncrementRequest ( ) <EOL> self . _add_app_id ( request ) <EOL> _add_name_space ( request , namespace ) <EOL> response = MemcacheIncrementResponse ( ) <EOL> request . set_key ( _key_string ( key ) ) <EOL> request . set_delta ( delta ) <EOL> if is_negative : <EOL> request . set_direction ( MemcacheIncrementRequest . DECREMENT ) <EOL> else : <EOL> request . set_direction ( MemcacheIncrementRequest . INCREMENT ) <EOL> if initial_value is not None : <EOL> request . set_initial_value ( long ( initial_value ) ) <EOL> initial_flags = None <EOL> if isinstance ( initial_value , int ) : <EOL> initial_flags = TYPE_INT <EOL> elif isinstance ( initial_value , long ) : <EOL> initial_flags = TYPE_LONG <EOL> if initial_flags is not None : <EOL> request . set_initial_flags ( initial_flags ) <EOL> return self . _make_async_call ( rpc , '<STR_LIT>' , request , response , <EOL> self . __incrdecr_hook , None ) <EOL> def __incrdecr_hook ( self , rpc ) : <EOL> try : <EOL> rpc . check_success ( ) <EOL> except apiproxy_errors . Error : <EOL> return None <EOL> response = rpc . response <EOL> if response . has_new_value ( ) : <EOL> return response . new_value ( ) <EOL> return None <EOL> def offset_multi ( self , mapping , key_prefix = '<STR_LIT>' , <EOL> namespace = None , initial_value = None ) : <EOL> """<STR_LIT>""" <EOL> rpc = self . offset_multi_async ( mapping , key_prefix , <EOL> namespace , initial_value ) <EOL> return rpc . get_result ( ) <EOL> def offset_multi_async ( self , mapping , key_prefix = '<STR_LIT>' , <EOL> namespace = None , initial_value = None , rpc = None ) : <EOL> """<STR_LIT>""" <EOL> initial_flags = None <EOL> if initial_value is not None : <EOL> if not isinstance ( initial_value , ( int , long ) ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if initial_value < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if isinstance ( initial_value , int ) : <EOL> initial_flags = TYPE_INT <EOL> else : <EOL> initial_flags = TYPE_LONG <EOL> request = MemcacheBatchIncrementRequest ( ) <EOL> self . _add_app_id ( request ) <EOL> response = MemcacheBatchIncrementResponse ( ) <EOL> _add_name_space ( request , namespace ) <EOL> for key , delta in mapping . iteritems ( ) : <EOL> if not isinstance ( delta , ( int , long ) ) : <EOL> raise TypeError ( '<STR_LIT>' % delta ) <EOL> if delta >= <NUM_LIT:0> : <EOL> direction = MemcacheIncrementRequest . INCREMENT <EOL> else : <EOL> delta = - delta <EOL> direction = MemcacheIncrementRequest . DECREMENT <EOL> server_key = _key_string ( key , key_prefix ) <EOL> item = request . add_item ( ) <EOL> item . set_key ( server_key ) <EOL> item . set_delta ( delta ) <EOL> item . set_direction ( direction ) <EOL> if initial_value is not None : <EOL> item . set_initial_value ( initial_value ) <EOL> item . set_initial_flags ( initial_flags ) <EOL> return self . _make_async_call ( rpc , '<STR_LIT>' , request , response , <EOL> self . __offset_hook , mapping ) <EOL> def __offset_hook ( self , rpc ) : <EOL> mapping = rpc . user_data <EOL> try : <EOL> rpc . check_success ( ) <EOL> except apiproxy_errors . Error : <EOL> return dict ( ( k , None ) for k in mapping . iterkeys ( ) ) <EOL> response = rpc . response <EOL> assert response . item_size ( ) == len ( mapping ) <EOL> result_dict = { } <EOL> for key , resp_item in zip ( mapping . iterkeys ( ) , response . item_list ( ) ) : <EOL> if ( resp_item . increment_status ( ) == MemcacheIncrementResponse . OK and <EOL> resp_item . has_new_value ( ) ) : <EOL> result_dict [ key ] = resp_item . new_value ( ) <EOL> else : <EOL> result_dict [ key ] = None <EOL> return result_dict <EOL> _CLIENT = None <EOL> def setup_client ( client_obj ) : <EOL> """<STR_LIT>""" <EOL> global _CLIENT <EOL> var_dict = globals ( ) <EOL> _CLIENT = client_obj <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . set_servers <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . disconnect_all <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . forget_dead_hosts <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . debuglog <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . get <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . get_multi <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . set <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . set_multi <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . add <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . add_multi <EOL> var_dict [ '<STR_LIT:replace>' ] = _CLIENT . replace <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . replace_multi <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . delete <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . delete_multi <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . incr <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . decr <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . flush_all <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . get_stats <EOL> var_dict [ '<STR_LIT>' ] = _CLIENT . offset_multi <EOL> setup_client ( Client ( ) ) </s>
