Skip to content
This repository has been archived by the owner on Jan 15, 2019. It is now read-only.

Commit

Permalink
'revoke' now throws a KeyError on invalid revokation keys; otherwise …
Browse files Browse the repository at this point in the history
…True
  • Loading branch information
Charlie Liban committed Jun 21, 2012
1 parent 1ad0998 commit 3ebbe83
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion shorten/__init__.py
@@ -1,7 +1,7 @@
from . import keygens
from . import keystores

__version__ = '0.2.2'
__version__ = '0.2.3'
__all__ = ['keygens', 'shortener', 'keystores', '__version__', 'shorteners']

shorteners = ('memcache', 'memory', 'mongo', 'redis', 'redis-bucket', 'sqlalchemy')
Expand Down
26 changes: 21 additions & 5 deletions shorten/keystores.py
Expand Up @@ -77,7 +77,7 @@ def insert_with_revoke(self, val, revoke_key):
self._acquire()

if revoke_key in self._revokation:
raise KeyError('Revokation key already exists.')
raise KeyError('Revokation key exists.')

short_key = self.insert(val)
self._revokation[revoke_key] = short_key
Expand All @@ -87,9 +87,19 @@ def insert_with_revoke(self, val, revoke_key):
return short_key

def revoke(self, revoke_key):
short_key = self._revokation[revoke_key]
"""\
Revokes (deletes) the short key associated with the given revokation key `revoke_key`.
If `revoke_key` does not exist, throws a KeyError. Otherwise returns True.
"""

try:
short_key = self._revokation[revoke_key]
except KeyError:
raise KeyError('Revokation key does not exist.')

del self._data[short_key]
del self._revokation[revoke_key]
return True

def __setitem__(self, key, item):
self._data[key] = item
Expand Down Expand Up @@ -173,15 +183,18 @@ def insert_with_revoke(self, val, revoke_key, pipe=None):

if pipe is None:
if p.execute()[0] == 0:
raise KeyError('Revokation key already exists.')
raise KeyError('Revokation key exists.')
return short_key
finally:
if pipe is None:
p.reset()

def revoke(self, revoke_key, pipe=None):
"""\
Revokes a short key by deleting it and associated metadata from Redis.
Revokes (deletes) the short key associated with the given revokation key `revoke_key`.
If `revoke_key` does not exist, throws a KeyError. Otherwise returns True.
If a Redis pipeline is given, no value is returned.
"""

p = self._redis.pipeline() if pipe is None else pipe
Expand All @@ -193,7 +206,10 @@ def revoke(self, revoke_key, pipe=None):
p.delete(short_key, revoke_key)

if pipe is None:
p.execute()
if p.execute()[-1] == 0:
raise KeyError('Revokation key does not exist.')
else:
return True
except redis.WatchError:
raise
finally:
Expand Down

0 comments on commit 3ebbe83

Please sign in to comment.