[PATCH] Add a way to override previously registered callback

Vratislav Podzimek vpodzime at redhat.com
Mon Jan 14 12:06:08 UTC 2013


It may be needed to force the name for a callback even when there
already is a callback with the same name registered.

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 meh/__init__.py    | 10 +++++++---
 tests/callbacks.py |  7 +++++--
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/meh/__init__.py b/meh/__init__.py
index 17a7864..329969f 100644
--- a/meh/__init__.py
+++ b/meh/__init__.py
@@ -96,7 +96,8 @@ class Config(object):
         if not self.programVersion:
             raise ValueError("programVersion must be set.")
 
-    def register_callback(self, item_name, callback, attchmnt_only=False):
+    def register_callback(self, item_name, callback, attchmnt_only=False,
+                          override=False):
         """
         Register new callback that will be called when data about the
         crash is collected. The returned value will be included as the
@@ -111,14 +112,17 @@ class Config(object):
                              as an attachment or also in the main '*-tb' file
                              (defaults to False)
         @type attchmnt_only: bool
+        @param override: whether to override the previously registered callback
+                         with the same item name or not (may raise exception)
+        @type override: bool
 
         @raise ConfigError: if callback with the 'item_name' has already been
-                            registered
+                            registered and 'override' is not set to True
         @return: None
 
         """
 
-        if item_name not in self.callbackDict:
+        if item_name not in self.callbackDict or override:
             self.callbackDict[item_name] = (callback, attchmnt_only)
         else:
             msg = "Callback with name '%s' already registered" % item_name
diff --git a/tests/callbacks.py b/tests/callbacks.py
index 5a420be..a73b0a8 100644
--- a/tests/callbacks.py
+++ b/tests/callbacks.py
@@ -23,11 +23,14 @@ class Callbacks_TestCase(BaseTestCase):
                                     "callback2": (callback2, False)})
 
         # another way to register callback
-        conf.register_callback("callback3", callback3)
+        conf.register_callback("callback3", callback2)
 
         # callback with given item name already registered
         with self.assertRaises(ConfigError):
-            conf.register_callback("callback3", callback2)
+            conf.register_callback("callback3", callback3)
+
+        # should not raise exception
+        conf.register_callback("callback3", callback3, override=True)
 
         conf.register_callback("callback4", callback4, attchmnt_only=True)
 
-- 
1.7.11.7



More information about the anaconda-patches mailing list