<s> from django . conf import settings <EOL> from django . core . cache . backends . base import InvalidCacheBackendError <EOL> from django . utils . six . moves . urllib . parse import parse_qsl <EOL> from redis import Redis , ConnectionError <EOL> class RedisError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def redis_client ( name ) : <EOL> """<STR_LIT>""" <EOL> if name not in settings . REDIS_BACKENDS : <EOL> raise RedisError ( <EOL> '<STR_LIT>' . format ( k = name ) ) <EOL> uri = settings . REDIS_BACKENDS [ name ] <EOL> _ , server , params = parse_backend_uri ( uri ) <EOL> db = params . pop ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> try : <EOL> db = int ( db ) <EOL> except ( ValueError , TypeError ) : <EOL> db = <NUM_LIT:1> <EOL> try : <EOL> socket_timeout = float ( params . pop ( '<STR_LIT>' ) ) <EOL> except ( KeyError , ValueError ) : <EOL> socket_timeout = None <EOL> password = params . pop ( '<STR_LIT:password>' , None ) <EOL> if '<STR_LIT::>' in server : <EOL> host , port = server . split ( '<STR_LIT::>' ) <EOL> try : <EOL> port = int ( port ) <EOL> except ( ValueError , TypeError ) : <EOL> port = <NUM_LIT> <EOL> else : <EOL> host = server <EOL> port = <NUM_LIT> <EOL> redis = Redis ( host = host , port = port , db = db , password = password , <EOL> socket_timeout = socket_timeout ) <EOL> try : <EOL> redis . exists ( '<STR_LIT>' ) <EOL> except ConnectionError : <EOL> raise RedisError ( <EOL> '<STR_LIT>' . format ( k = name ) ) <EOL> return redis <EOL> def parse_backend_uri ( backend_uri ) : <EOL> """<STR_LIT>""" <EOL> if backend_uri . find ( '<STR_LIT::>' ) == - <NUM_LIT:1> : <EOL> raise InvalidCacheBackendError ( "<STR_LIT>" ) <EOL> scheme , rest = backend_uri . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if not rest . startswith ( '<STR_LIT>' ) : <EOL> raise InvalidCacheBackendError ( "<STR_LIT>" ) <EOL> host = rest [ <NUM_LIT:2> : ] <EOL> qpos = rest . find ( '<STR_LIT:?>' ) <EOL> if qpos != - <NUM_LIT:1> : <EOL> params = dict ( parse_qsl ( rest [ qpos + <NUM_LIT:1> : ] ) ) <EOL> host = rest [ <NUM_LIT:2> : qpos ] <EOL> else : <EOL> params = { } <EOL> if host . endswith ( '<STR_LIT:/>' ) : <EOL> host = host [ : - <NUM_LIT:1> ] <EOL> return scheme , host , params </s>
