<s> import enum <EOL> import redis <EOL> from redis import exceptions as redis_exceptions <EOL> import six <EOL> def _raise_on_closed ( meth ) : <EOL> @ six . wraps ( meth ) <EOL> def wrapper ( self , * args , ** kwargs ) : <EOL> if self . closed : <EOL> raise redis_exceptions . ConnectionError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return meth ( self , * args , ** kwargs ) <EOL> return wrapper <EOL> class RedisClient ( redis . StrictRedis ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( RedisClient , self ) . __init__ ( * args , ** kwargs ) <EOL> self . closed = False <EOL> def close ( self ) : <EOL> self . closed = True <EOL> self . connection_pool . disconnect ( ) <EOL> execute_command = _raise_on_closed ( redis . StrictRedis . execute_command ) <EOL> transaction = _raise_on_closed ( redis . StrictRedis . transaction ) <EOL> pubsub = _raise_on_closed ( redis . StrictRedis . pubsub ) <EOL> class UnknownExpire ( enum . IntEnum ) : <EOL> """<STR_LIT>""" <EOL> DOES_NOT_EXPIRE = - <NUM_LIT:1> <EOL> """<STR_LIT>""" <EOL> KEY_NOT_FOUND = - <NUM_LIT:2> <EOL> DOES_NOT_EXPIRE = UnknownExpire . DOES_NOT_EXPIRE <EOL> KEY_NOT_FOUND = UnknownExpire . KEY_NOT_FOUND <EOL> _UNKNOWN_EXPIRE_MAPPING = dict ( ( e . value , e ) for e in list ( UnknownExpire ) ) <EOL> def get_expiry ( client , key , prior_version = None ) : <EOL> """<STR_LIT>""" <EOL> is_new_enough , _prior_version = is_server_new_enough ( <EOL> client , ( <NUM_LIT:2> , <NUM_LIT:6> ) , prior_version = prior_version ) <EOL> if is_new_enough : <EOL> result = client . pttl ( key ) <EOL> try : <EOL> return _UNKNOWN_EXPIRE_MAPPING [ result ] <EOL> except KeyError : <EOL> return result / <NUM_LIT> <EOL> else : <EOL> result = client . ttl ( key ) <EOL> try : <EOL> return _UNKNOWN_EXPIRE_MAPPING [ result ] <EOL> except KeyError : <EOL> return float ( result ) <EOL> def apply_expiry ( client , key , expiry , prior_version = None ) : <EOL> """<STR_LIT>""" <EOL> is_new_enough , _prior_version = is_server_new_enough ( <EOL> client , ( <NUM_LIT:2> , <NUM_LIT:6> ) , prior_version = prior_version ) <EOL> if is_new_enough : <EOL> ms_expiry = expiry * <NUM_LIT> <EOL> ms_expiry = max ( <NUM_LIT:0> , int ( ms_expiry ) ) <EOL> result = client . pexpire ( key , ms_expiry ) <EOL> else : <EOL> sec_expiry = int ( expiry ) <EOL> sec_expiry = max ( <NUM_LIT:0> , sec_expiry ) <EOL> result = client . expire ( key , sec_expiry ) <EOL> return bool ( result ) <EOL> def is_server_new_enough ( client , min_version , <EOL> default = False , prior_version = None ) : <EOL> """<STR_LIT>""" <EOL> if not prior_version : <EOL> try : <EOL> server_info = client . info ( ) <EOL> except redis_exceptions . ResponseError : <EOL> server_info = { } <EOL> version_text = server_info . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> version_text = prior_version <EOL> version_pieces = [ ] <EOL> for p in version_text . split ( "<STR_LIT:.>" ) : <EOL> try : <EOL> version_pieces . append ( int ( p ) ) <EOL> except ValueError : <EOL> break <EOL> if not version_pieces : <EOL> return ( default , version_text ) <EOL> else : <EOL> version_pieces = tuple ( version_pieces ) <EOL> return ( version_pieces >= min_version , version_text ) </s>
