summaryrefslogtreecommitdiff
path: root/redis
diff options
context:
space:
mode:
authorJon Parise <jon@indelible.org>2012-07-06 17:44:26 -0700
committerJon Parise <jon@indelible.org>2012-07-06 17:49:37 -0700
commita378cdb91fbf3909b67748bc18a12f0dbbcaacf5 (patch)
tree43ed3deedc83fc3746d41b00515b200295550d7d /redis
parent1f6c4a5ab1fefb125fb8af9b3974c3f25fd44f8f (diff)
downloadredis-py-a378cdb91fbf3909b67748bc18a12f0dbbcaacf5.tar.gz
Improved `from_url()` support.
`from_url()` is now implemented as a classmethod. The previous implementation always returned instances of the `Redis` class. This new approach lets us construct StrictRedis instances (or any other class derived from StrictRedis, including Redis). `utils.from_url()` has been preserved for backwards compatibility. Also, both versions of `from_url()` now pass along any additional keyword arguments to the class's initializer.
Diffstat (limited to 'redis')
-rw-r--r--redis/client.py31
-rw-r--r--redis/utils.py26
2 files changed, 33 insertions, 24 deletions
diff --git a/redis/client.py b/redis/client.py
index d866928..3cca591 100644
--- a/redis/client.py
+++ b/redis/client.py
@@ -3,6 +3,7 @@ import datetime
import time
import warnings
from itertools import imap, izip, starmap
+from urlparse import urlparse
from redis.connection import ConnectionPool, UnixDomainSocketConnection
from redis.exceptions import (
ConnectionError,
@@ -184,6 +185,36 @@ class StrictRedis(object):
}
)
+ @classmethod
+ def from_url(cls, url, db=None, **kwargs):
+ """
+ Return a Redis client object configured from the given URL.
+
+ For example::
+
+ redis://username:password@localhost:6379/0
+
+ If ``db`` is None, this method will attempt to extract the database ID
+ from the URL path component.
+
+ Any additional keyword arguments will be passed along to the Redis
+ class's initializer.
+ """
+ url = urlparse(url)
+
+ # We only support redis:// schemes.
+ assert url.scheme == 'redis' or not url.scheme
+
+ # Extract the database ID from the path component if hasn't been given.
+ if db is None:
+ try:
+ db = int(url.path.replace('/', ''))
+ except (AttributeError, ValueError):
+ db = 0
+
+ return cls(host=url.hostname, port=url.port, db=db,
+ password=url.password, **kwargs)
+
def __init__(self, host='localhost', port=6379,
db=0, password=None, socket_timeout=None,
connection_pool=None, charset='utf-8',
diff --git a/redis/utils.py b/redis/utils.py
index 863541d..8e3b124 100644
--- a/redis/utils.py
+++ b/redis/utils.py
@@ -1,31 +1,9 @@
-from urlparse import urlparse
-
from .client import Redis
-DEFAULT_DATABASE_ID = 0
-
-def from_url(url, db=None):
+def from_url(url, db=None, **kwargs):
"""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) \ No newline at end of file
+ return Redis.from_url(url, db, **kwargs)