Permalink
Browse files

imporoved forward_error context manager

  • Loading branch information...
evilkost committed Apr 15, 2011
1 parent f9b252f commit 3d15814e8d0030f1a7520a1ee106c70b7a70e6cd
Showing with 29 additions and 13 deletions.
  1. +29 −13 brukva/client.py
View
@@ -16,20 +16,36 @@
log = logging.getLogger('brukva.client')
-@contextlib.contextmanager
-def forward_error(callbacks, cleanup=None):
- try:
- yield callbacks
- except Exception, e:
- log.error(e)
- if isinstance(callbacks, Iterable):
- for cb in callbacks:
- cb(e)
+class ForwardErrorManager(object):
+ def __init__(self, callbacks):
+ self.callbacks = callbacks
+ self.is_active = True
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, tb):
+ if type is None:
+ return True
+
+ if self.is_active:
+ if isinstance(self.callbacks, Iterable):
+ for cb in self.callbacks:
+ cb(value)
+ else:
+ self.callbacks(value)
+ return True
else:
- callbacks(e)
- finally:
- if cleanup:
- cleanup()
+ return False
+
+ def disable(self):
+ self.is_active = False
+
+ def enable(self):
+ self.is_active = True
+
+def forward_error(callbacks):
+ return ForwardErrorManager(callbacks)
class Message(object):
def __init__(self, kind, channel, body):

0 comments on commit 3d15814

Please sign in to comment.