Created
July 28, 2010 23:06
-
-
Save williamsjj/496659 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Enhanced txRedis that handles setting up auth and DB | |
| class ERedis(Redis): | |
| def __init__(self, db=None, charset='utf8', errors='strict', passwd=None): | |
| self.charset = charset | |
| self.db = db | |
| self.errors = errors | |
| self._buffer = '' | |
| self._bulk_length = None | |
| self._disconnected = False | |
| self._multi_bulk_length = None | |
| self._multi_bulk_reply = [] | |
| self._request_queue = deque() | |
| self.passwd = passwd | |
| print self.db | |
| def connectionMade(self): | |
| Redis.connectionMade(self) | |
| # Authenticate if requested, and select DB | |
| if self.passwd: | |
| return self.init_auth().addCallback(self.select_db) | |
| else: | |
| return self.select_db() | |
| @defer.inlineCallbacks | |
| def init_auth(self, *args): | |
| while self.transport == None: | |
| yield task.deferLater(reactor, 1.0, defer.passthru, None) | |
| try: | |
| res = yield self.auth(self.passwd) | |
| except Exception, e: | |
| raise Exception("Unexpected error authenticating with Redis backend: %s" % str(e)) | |
| if res != "OK": | |
| raise Exception("Invalid Redis password supplied for authentication.") | |
| @defer.inlineCallbacks | |
| def select_db(self, *args): | |
| while self.transport == None: | |
| yield task.deferLater(reactor, 1.0, defer.passthru, None) | |
| try: | |
| res = yield self.select(self.db) | |
| res = str(res) | |
| if res == "operation not permitted": | |
| raise Exception("Error selecting DB %s. Redis password required." % self.redis_db) | |
| elif res == "invalid DB index": | |
| raise Exception("Invalid DB index (%s)." % self.redis_db) | |
| elif res != "OK": | |
| raise Exception("Unexpected error selecting DB %s." % self.redis_db) | |
| except error.ConnectionRefusedError, e: | |
| raise Exception("Unexpected connection error selecting Redis DB %s: %s" % (self.redis_db, | |
| str(e))) | |
| return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment