Permalink
Browse files

[refactoring] extracted execution_context to new module brukva.utils

  • Loading branch information...
1 parent 84603bb commit 0883aac17d41296e19fc86dee226e77bbda031a9 @evilkost committed May 10, 2011
Showing with 80 additions and 68 deletions.
  1. +2 −66 brukva/client.py
  2. +5 −2 brukva/exceptions.py
  3. +73 −0 brukva/utils.py
View
@@ -14,76 +14,12 @@
from datetime import datetime
from brukva.exceptions import RedisError, RequestError, \
- ConnectionError, ResponseError, InvalidResponse
+ ConnectionError, ResponseError, InvalidResponse, InternalRedisError
+from brukva.utils import execution_context
log = logging.getLogger('brukva.client')
log_blob = logging.getLogger('brukva.client.blob')
-class ExecutionContext(object):
- def __init__(self, callbacks, error_wrapper=None):
- self.callbacks = callbacks
- self.error_wrapper = error_wrapper
- self.is_active = True
-
- def _call_callbacks(self, callbacks, value):
- if callbacks:
- if isinstance(callbacks, Iterable):
- for cb in callbacks:
- cb(value)
- else:
- callbacks(value)
-
- def __enter__(self):
- return self
-
- def __exit__(self, type_, value, tb):
- if type_ is None:
- return True
-
- if self.error_wrapper:
- value = self.error_wrapper(value)
- else:
- value = value or Exception(
- 'Strange exception with None value type: %s; tb: %s' %
- (type_, '\n'.join(traceback.format_tb(tb))
- ))
-
- if self.is_active:
- log.error(value, exc_info=(type_, value, tb))
- self.ret_call(value)
- return True
- else:
- return False
-
- def disable(self):
- self.is_active = False
-
- def enable(self):
- self.is_active = True
-
- def ret_call(self, value):
- self.is_active = False
- self._call_callbacks(self.callbacks, value)
- self.is_active = True
-
- def safe_call(self, callbacks, value):
- self.is_active = False
- self._call_callbacks(callbacks, value)
- self.is_active = True
-
-
-def execution_context(callbacks, error_wrapper=None):
- """
- Syntax sugar.
- If some error occurred inside with block,
- it will be suppressed and forwarded to callbacks.
-
- Use contex.ret_call(value) method to call callbacks.
-
- @type callbacks: callable or iterator over callables
- @rtype: context
- """
- return ExecutionContext(callbacks, error_wrapper)
class Message(object):
def __init__(self, kind, channel, body):
View
@@ -30,11 +30,14 @@ def __init__(self, message, cmd_line=None):
def __repr__(self):
if self.cmd_line:
- return 'ResponseError (on %s [%s, %s]): %s' % (self.cmd_line.cmd, self.cmd_line.args, self.cmd_line.kwargs, self.message)
- return 'ResponseError: %s' % self.message
+ return '%s (on %s [%s, %s]): %s' % (self.__class__, self.cmd_line.cmd, self.cmd_line.args, self.cmd_line.kwargs, self.message)
+ return '%s: %s' % (self.__class__, self.message)
__str__ = __repr__
+class InternalRedisError(ResponseError):
+ pass
+
class InvalidResponse(RedisError):
pass
View
@@ -0,0 +1,73 @@
+# -*- coding: utf-8 -*-
+
+import traceback
+from collections import Iterable
+import logging
+
+log = logging.getLogger('brukva.utils')
+
+class ExecutionContext(object):
+ def __init__(self, callbacks, error_wrapper=None):
+ self.callbacks = callbacks
+ self.error_wrapper = error_wrapper
+ self.is_active = True
+
+ def _call_callbacks(self, callbacks, value):
+ if callbacks:
+ if isinstance(callbacks, Iterable):
+ for cb in callbacks:
+ cb(value)
+ else:
+ callbacks(value)
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type_, value, tb):
+ if type_ is None:
+ return True
+
+ if self.error_wrapper:
+ value = self.error_wrapper(value)
+ else:
+ value = value or Exception(
+ 'Strange exception with None value type: %s; tb: %s' %
+ (type_, '\n'.join(traceback.format_tb(tb))
+ ))
+
+ if self.is_active:
+ log.error(value, exc_info=(type_, value, tb))
+ self.ret_call(value)
+ return True
+ else:
+ return False
+
+ def disable(self):
+ self.is_active = False
+
+ def enable(self):
+ self.is_active = True
+
+ def ret_call(self, value):
+ self.is_active = False
+ self._call_callbacks(self.callbacks, value)
+ self.is_active = True
+
+ def safe_call(self, callbacks, value):
+ self.is_active = False
+ self._call_callbacks(callbacks, value)
+ self.is_active = True
+
+
+def execution_context(callbacks, error_wrapper=None):
+ """
+ Syntax sugar.
+ If some error occurred inside with block,
+ it will be suppressed and forwarded to callbacks.
+
+ Use contex.ret_call(value) method to call callbacks.
+
+ @type callbacks: callable or iterator over callables
+ @rtype: context
+ """
+ return ExecutionContext(callbacks, error_wrapper)

0 comments on commit 0883aac

Please sign in to comment.