diff options
author | andy <andy@whiskeymedia.com> | 2012-08-14 14:34:23 -0700 |
---|---|---|
committer | andy <andy@whiskeymedia.com> | 2012-08-14 14:34:23 -0700 |
commit | cdebbd48771f49a7cdf68823dcfc487ea15141cf (patch) | |
tree | 9a1bf424b9fa662c922b1050d708a75dd0455337 /redis/client.py | |
parent | 937f8e4dfecc838afcc208a86c5d5139b33626fd (diff) | |
parent | a378cdb91fbf3909b67748bc18a12f0dbbcaacf5 (diff) | |
download | redis-py-cdebbd48771f49a7cdf68823dcfc487ea15141cf.tar.gz |
Merge remote-tracking branch 'jparise/cls-from_url'
Conflicts:
redis/client.py
redis/utils.py
Diffstat (limited to 'redis/client.py')
-rw-r--r-- | redis/client.py | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/redis/client.py b/redis/client.py index db8213f..83a8bd3 100644 --- a/redis/client.py +++ b/redis/client.py @@ -3,9 +3,8 @@ from itertools import starmap import datetime import time import warnings - from redis._compat import (b, izip, imap, iteritems, dictkeys, dictvalues, - basestring, long, nativestr) + basestring, long, nativestr, urlparse) from redis.connection import ConnectionPool, UnixDomainSocketConnection from redis.exceptions import ( ConnectionError, @@ -205,6 +204,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', |