<s> """<STR_LIT>""" <EOL> import logging <EOL> from hashlib import sha1 <EOL> from functools import wraps <EOL> from redis . client import StrictRedis <EOL> from redis . exceptions import ConnectionError <EOL> class Node ( object ) : <EOL> """<STR_LIT>""" <EOL> redis_client_class = StrictRedis <EOL> def __init__ ( self , name , host , port ) : <EOL> self . name = name <EOL> self . host = host <EOL> self . port = port <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . info ( "<STR_LIT>" % ( name , host , port ) ) <EOL> self . connection = self . redis_client_class ( host , int ( port ) ) <EOL> def executeOnNode ( func ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( func ) <EOL> def wrapper ( self , key , * args , ** kwargs ) : <EOL> node = self . get_node_for_key ( key ) <EOL> nodeFunc = getattr ( node . connection , func . __name__ ) <EOL> try : <EOL> return nodeFunc ( key , * args , ** kwargs ) <EOL> except ConnectionError : <EOL> node = self . get_master ( node ) <EOL> nodeFunc = getattr ( node . connection , func . __name__ ) <EOL> return nodeFunc ( key , * args , ** kwargs ) <EOL> return wrapper <EOL> class DisredisClient ( object ) : <EOL> """<STR_LIT>""" <EOL> redis_client_class = StrictRedis <EOL> sentinel = None <EOL> nodes = None <EOL> def __init__ ( self , sentinel_addresses ) : <EOL> self . sentinel_addresses = sentinel_addresses <EOL> self . _get_nodes ( ) <EOL> def _connect ( self ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> try : <EOL> address = self . sentinel_addresses . pop ( <NUM_LIT:0> ) <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . info ( "<STR_LIT>" % address ) <EOL> host , port = address . split ( "<STR_LIT::>" ) <EOL> self . sentinel = self . redis_client_class ( host , int ( port ) ) <EOL> self . sentinel_addresses . append ( address ) <EOL> break <EOL> except ConnectionError : <EOL> if not self . sentinel_addresses : <EOL> raise <EOL> except IndexError : <EOL> raise ConnectionError ( "<STR_LIT>" ) <EOL> def _execute_sentinel_command ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> try : <EOL> if self . sentinel is None : <EOL> self . _connect ( ) <EOL> return self . sentinel . execute_command ( "<STR_LIT>" , * args , <EOL> ** kwargs ) <EOL> except ConnectionError : <EOL> self . sentinel = None <EOL> if self . sentinel_addresses : <EOL> self . sentinel_addresses . pop ( ) <EOL> else : <EOL> raise <EOL> def _get_nodes ( self ) : <EOL> """<STR_LIT>""" <EOL> masterList = self . _execute_sentinel_command ( "<STR_LIT>" ) <EOL> self . nodes = [ ] <EOL> for master in masterList : <EOL> info = dict ( zip ( master [ : : <NUM_LIT:2> ] , master [ <NUM_LIT:1> : : <NUM_LIT:2> ] ) ) <EOL> self . nodes . append ( Node ( info [ "<STR_LIT:name>" ] , info [ "<STR_LIT>" ] , info [ "<STR_LIT:port>" ] ) ) <EOL> def get_master ( self , node ) : <EOL> """<STR_LIT>""" <EOL> host , port = self . _execute_sentinel_command ( "<STR_LIT>" , <EOL> node . name ) <EOL> if host == node . host and port == node . port : <EOL> return node <EOL> newNode = Node ( node . name , host , port ) <EOL> self . nodes [ self . nodes . index ( node ) ] = newNode <EOL> return newNode <EOL> def get_node_for_key ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT:{>" in key and "<STR_LIT:}>" in key : <EOL> key = key [ key . index ( "<STR_LIT:{>" ) + <NUM_LIT:1> : key . index ( "<STR_LIT:}>" ) ] <EOL> return self . nodes [ int ( sha1 ( key ) . hexdigest ( ) , <NUM_LIT:16> ) % len ( self . nodes ) ] <EOL> def set_response_callback ( self , command , callback ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def pipeline ( self , transaction = True , shard_hint = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def transaction ( self , func , * watches , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ executeOnNode <EOL> def lock ( self , name , timeout = None , sleep = <NUM_LIT:0.1> ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def pubsub ( self , shard_hint = None ) : <EOL> """<STR_LIT>""" <EOL> def bgrewriteaof ( self ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def bgsave ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def client_kill ( self , address ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def client_list ( self ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def client_getname ( self ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def client_setname ( self , name ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def config_get ( self , pattern = "<STR_LIT:*>" ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def config_set ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def dbsize ( self ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def time ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ executeOnNode <EOL> def debug_object ( self , key ) : <EOL> "<STR_LIT>" <EOL> def delete ( self , * names ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , * names ) <EOL> __delitem__ = delete <EOL> def echo ( self , value ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def flushall ( self ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def flushdb ( self ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def info ( self , section = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def lastsave ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def object ( self , infotype , key ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def ping ( self ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def shutdown ( self ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def slaveof ( self , host = None , port = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ executeOnNode <EOL> def append ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def getrange ( self , key , start , end ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def bitcount ( self , key , start = None , end = None ) : <EOL> """<STR_LIT>""" <EOL> def bitop ( self , operation , dest , * keys ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ executeOnNode <EOL> def decr ( self , name , amount = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def exists ( self , name ) : <EOL> "<STR_LIT>" <EOL> __contains__ = exists <EOL> @ executeOnNode <EOL> def expire ( self , name , time ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def expireat ( self , name , when ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def get ( self , name ) : <EOL> """<STR_LIT>""" <EOL> def __getitem__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> value = self . get ( name ) <EOL> if value : <EOL> return value <EOL> raise KeyError ( name ) <EOL> @ executeOnNode <EOL> def getbit ( self , name , offset ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def getset ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def incr ( self , name , amount = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> def incrby ( self , name , amount = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . incr ( name , amount ) <EOL> @ executeOnNode <EOL> def incrbyfloat ( self , name , amount = <NUM_LIT:1.0> ) : <EOL> """<STR_LIT>""" <EOL> def keys ( self , pattern = '<STR_LIT:*>' ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def mget ( self , keys , * args ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def mset ( self , mapping ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def msetnx ( self , mapping ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ executeOnNode <EOL> def move ( self , name , db ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def persist ( self , name ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def pexpire ( self , name , time ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def pexpireat ( self , name , when ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def psetex ( self , name , time_ms , value ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def pttl ( self , name ) : <EOL> "<STR_LIT>" <EOL> def randomkey ( self ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def rename ( self , src , dst ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def renamenx ( self , src , dst ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ executeOnNode <EOL> def set ( self , name , value , ex = None , px = None , nx = False , xx = False ) : <EOL> """<STR_LIT>""" <EOL> __setitem__ = set <EOL> @ executeOnNode <EOL> def setbit ( self , name , offset , value ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def setex ( self , name , time , value ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def setnx ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def setrange ( self , name , offset , value ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def strlen ( self , name ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def substr ( self , name , start , end = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def ttl ( self , name ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def type ( self , name ) : <EOL> "<STR_LIT>" <EOL> def blpop ( self , keys , timeout = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def brpop ( self , keys , timeout = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def brpoplpush ( self , src , dst , timeout = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ executeOnNode <EOL> def lindex ( self , name , index ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def linsert ( self , name , where , refvalue , value ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def llen ( self , name ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def lpop ( self , name ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def lpush ( self , name , * values ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def lpushx ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def lrange ( self , name , start , end ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def lrem ( self , name , count , value ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def lset ( self , name , index , value ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def ltrim ( self , name , start , end ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def rpop ( self , name ) : <EOL> "<STR_LIT>" <EOL> def rpoplpush ( self , src , dst ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ executeOnNode <EOL> def rpush ( self , name , * values ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def rpushx ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def sort ( self , name , start = None , num = None , by = None , get = None , <EOL> desc = False , alpha = False , store = None , groups = False ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def sadd ( self , name , * values ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def scard ( self , name ) : <EOL> "<STR_LIT>" <EOL> def sdiff ( self , keys , * args ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def sdiffstore ( self , dest , keys , * args ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def sinter ( self , keys , * args ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def sinterstore ( self , dest , keys , * args ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ executeOnNode <EOL> def sismember ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def smembers ( self , name ) : <EOL> "<STR_LIT>" <EOL> def smove ( self , src , dst , value ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , src , dst , value ) <EOL> @ executeOnNode <EOL> def spop ( self , name ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def srandmember ( self , name , number = None ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def srem ( self , name , * values ) : <EOL> "<STR_LIT>" <EOL> def sunion ( self , keys , * args ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def sunionstore ( self , dest , keys , * args ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ executeOnNode <EOL> def zadd ( self , name , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def zcard ( self , name ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def zcount ( self , name , min , max ) : <EOL> """<STR_LIT:U+0020>""" <EOL> @ executeOnNode <EOL> def zincrby ( self , name , value , amount = <NUM_LIT:1> ) : <EOL> "<STR_LIT>" <EOL> def zinterstore ( self , dest , keys , aggregate = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ executeOnNode <EOL> def zrange ( self , name , start , end , desc = False , withscores = False , <EOL> score_cast_func = float ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def zrangebyscore ( self , name , min , max , start = None , num = None , <EOL> withscores = False , score_cast_func = float ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def zrank ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def zrem ( self , name , * values ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def zremrangebyrank ( self , name , min , max ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def zremrangebyscore ( self , name , min , max ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def zrevrange ( self , name , start , num , withscores = False , <EOL> score_cast_func = float ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def zrevrangebyscore ( self , name , max , min , start = None , num = None , <EOL> withscores = False , score_cast_func = float ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def zrevrank ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def zscore ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> def zunionstore ( self , dest , keys , aggregate = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ executeOnNode <EOL> def hdel ( self , name , * keys ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def hexists ( self , name , key ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def hget ( self , name , key ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def hgetall ( self , name ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def hincrby ( self , name , key , amount = <NUM_LIT:1> ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def hincrbyfloat ( self , name , key , amount = <NUM_LIT:1.0> ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def hkeys ( self , name ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def hlen ( self , name ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def hset ( self , name , key , value ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def hsetnx ( self , name , key , value ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def hmset ( self , name , mapping ) : <EOL> """<STR_LIT>""" <EOL> @ executeOnNode <EOL> def hmget ( self , name , keys , * args ) : <EOL> "<STR_LIT>" <EOL> @ executeOnNode <EOL> def hvals ( self , name ) : <EOL> "<STR_LIT>" <EOL> def publish ( self , channel , message ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def eval ( self , script , numkeys , * keys_and_args ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def evalsha ( self , sha , numkeys , * keys_and_args ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def script_exists ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def script_flush ( self ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def script_kill ( self ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def script_load ( self , script ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def register_script ( self , script ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) </s>
