blob: 466509b1b02b4e7c2c487b7b3702b9f7e3f086e8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
from urlparse import urlparse
from .client import Redis
DEFAULT_DATABASE_ID = 0
def from_url(url, db=None):
"""Returns an active Redis client generated from the given database URL.
Will attempt to extract the database id from the path url fragment, if
none is provided.
"""
url = urlparse(url)
# Make sure it's a redis database.
if url.scheme:
assert url.scheme == 'redis'
# Attempt to resolve database id.
if db is None:
try:
db = int(url.path.replace('/', ''))
except (AttributeError, ValueError):
db = DEFAULT_DATABASE_ID
return Redis(
host=url.hostname,
port=url.port,
db=db,
password=url.password)
|