Skip to content
Browse files

Merge branch 'from_url' of https://github.com/kennethreitz/redis-py i…

…nto kennethreitz-from_url

Conflicts:
	redis/__init__.py
  • Loading branch information...
2 parents 56625fe + 42ddf5b commit 833c7ce441735d6a0df70752b0a1556320529267 @andymccurdy committed May 18, 2012
Showing with 33 additions and 1 deletion.
  1. +2 −1 redis/__init__.py
  2. +31 −0 redis/utils.py
View
3 redis/__init__.py
@@ -4,6 +4,7 @@
Connection,
UnixDomainSocketConnection
)
+from redis.utils import from_url
from redis.exceptions import (
AuthenticationError,
ConnectionError,
@@ -23,5 +24,5 @@
'Redis', 'StrictRedis', 'ConnectionPool',
'Connection', 'UnixDomainSocketConnection',
'RedisError', 'ConnectionError', 'ResponseError', 'AuthenticationError',
- 'InvalidResponse', 'DataError', 'PubSubError', 'WatchError',
+ 'InvalidResponse', 'DataError', 'PubSubError', 'WatchError', 'from_url',
]
View
31 redis/utils.py
@@ -0,0 +1,31 @@
+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)

0 comments on commit 833c7ce

Please sign in to comment.
Something went wrong with that request. Please try again.