Change in vdsm[master]: dispatcher: use a method wrapper instead of Protect

Federico Simoncelli fsimonce at redhat.com
Fri Apr 18 08:33:02 UTC 2014


Federico Simoncelli has uploaded a new change for review.

Change subject: dispatcher: use a method wrapper instead of Protect
......................................................................

dispatcher: use a method wrapper instead of Protect

This patch replaces the superfluous Protect class with a method wrapper.

Change-Id: Ia7a1a2f88238cb4b845e62de0c1f12e462a4ae72
Signed-off-by: Federico Simoncelli <fsimonce at redhat.com>
---
M vdsm/storage/dispatcher.py
1 file changed, 40 insertions(+), 56 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/74/26774/4

diff --git a/vdsm/storage/dispatcher.py b/vdsm/storage/dispatcher.py
index da838e8..1823a09 100644
--- a/vdsm/storage/dispatcher.py
+++ b/vdsm/storage/dispatcher.py
@@ -19,67 +19,13 @@
 #
 
 import logging
+from functools import wraps
 from vdsm.config import config
 
 import task
 import storage_exception as se
 
 _EXPORTED_ATTRIBUTE = "__dispatcher_exported__"
-
-
-class Protect:
-    STATUS_OK = {'status': {'code': 0, 'message': "OK"}}
-    STATUS_ERROR = {'status': {'code': 100, 'message': "ERROR"}}
-    log = logging.getLogger('Storage.Dispatcher.Protect')
-
-    def __init__(self, func, name):
-        self.name = name
-        self.func = func
-
-        self.help = None
-        try:
-            if hasattr(func.im_self, "help"):
-                self.help = getattr(func.im_self, "help")
-        except:
-            pass
-
-        if not self.help:
-            try:
-                self.help = getattr(func, "__doc__")
-            except:
-                pass
-
-        if not self.help:
-            self.help = "No help available for method %s" % name
-
-    def run(self, *args, **kwargs):
-        try:
-            ctask = task.Task(id=None, name=self.name)
-            try:
-                response = self.STATUS_OK.copy()
-                result = ctask.prepare(self.func, *args, **kwargs)
-                if type(result) == dict:
-                    response.update(result)
-                return response
-            except se.GeneralException as e:
-                self.log.error(e.response())
-                return e.response()
-            except BaseException as e:
-                self.log.error(e, exc_info=True)
-                defaultException = ctask.defaultException
-                if defaultException and hasattr(defaultException, "response"):
-                    resp = defaultException.response()
-                    defaultExceptionInfo = (resp['status']['code'],
-                                            resp['status']['message'])
-                    return se.generateResponse(e, defaultExceptionInfo)
-
-                return se.generateResponse(e)
-        except:
-            try:
-                self.log.error("Unhandled exception in run and protect: %s, "
-                               "args: %s ", self.name, args, exc_info=True)
-            finally:
-                return self.STATUS_ERROR.copy()
 
 
 def exported(f):
@@ -89,6 +35,9 @@
 
 class Dispatcher:
     log = logging.getLogger('Storage.Dispatcher')
+
+    STATUS_OK = {'status': {'code': 0, 'message': "OK"}}
+    STATUS_ERROR = {'status': {'code': 100, 'message': "ERROR"}}
 
     def __init__(self, obj):
         self._obj = obj
@@ -110,7 +59,42 @@
                     continue
                 # Create a new entry in instance's "dict" that will mask the
                 # original method
-                setattr(self, funcName, Protect(funcObj, funcName).run)
+                setattr(self, funcName, self.protect(funcObj, funcName))
+
+    def protect(self, func, name, *args, **kwargs):
+        @wraps(func)
+        def wrapper(*args, **kwargs):
+            try:
+                ctask = task.Task(id=None, name=name)
+                try:
+                    response = self.STATUS_OK.copy()
+                    result = ctask.prepare(func, *args, **kwargs)
+                    if type(result) == dict:
+                        response.update(result)
+                    return response
+                except se.GeneralException as e:
+                    self.log.error(e.response())
+                    return e.response()
+                except BaseException as e:
+                    self.log.error(e, exc_info=True)
+                    defaultException = ctask.defaultException
+                    if (defaultException and
+                            hasattr(defaultException, "response")):
+                        resp = defaultException.response()
+                        defaultExceptionInfo = (resp['status']['code'],
+                                                resp['status']['message'])
+                        return se.generateResponse(e, defaultExceptionInfo)
+
+                    return se.generateResponse(e)
+            except:
+                try:
+                    self.log.error(
+                        "Unhandled exception in run and protect: %s, "
+                        "args: %s ", self.name, args, exc_info=True)
+                finally:
+                    return self.STATUS_ERROR.copy()
+
+        return wrapper
 
     def _methodHelp(self, method):
         # this method must be present for system.methodHelp


-- 
To view, visit http://gerrit.ovirt.org/26774
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia7a1a2f88238cb4b845e62de0c1f12e462a4ae72
Gerrit-PatchSet: 4
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Federico Simoncelli <fsimonce at redhat.com>
Gerrit-Reviewer: automation at ovirt.org
Gerrit-Reviewer: oVirt Jenkins CI Server


More information about the vdsm-patches mailing list