changeset 517:85699d18921f

browser_side: menu callback can be a method not belonging to a class
author souliane <souliane@mailoo.org>
date Tue, 02 Sep 2014 21:08:59 +0200
parents 1af112b97e45
children 3d8e8f693576
files src/browser/sat_browser/base_menu.py
diffstat 1 files changed, 20 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/browser/sat_browser/base_menu.py	Tue Sep 02 21:05:32 2014 +0200
+++ b/src/browser/sat_browser/base_menu.py	Tue Sep 02 21:08:59 2014 +0200
@@ -28,6 +28,7 @@
 from sat.core.log import getLogger
 log = getLogger(__name__)
 
+from sat.core import exceptions
 from pyjamas.ui.MenuBar import MenuBar
 from pyjamas.ui.UIObject import UIObject
 from pyjamas.ui.MenuItem import MenuItem
@@ -39,14 +40,21 @@
 class MenuCmd:
     """Return an object with an "execute" method that can be set to a menu item callback"""
 
-    def __init__(self, object_, handler, data=None):
-        self._object = object_
-        self._handler = handler
-        self._data = data
+    def __init__(self, object_, handler=None, data=None):
+        """
+        @param object_ (object): a callable or a class instance
+        @param handler (str): method name if object_ is a class instance
+        @param data (dict): data to pass as the callback argument
+        """
+        if handler is None:
+            assert(callable(object_))
+            self.callback = object_
+        else:
+            self.callback = getattr(object_, handler)
+        self.data = data
 
     def execute(self):
-        handler = getattr(self._object, self._handler)
-        handler(self._data) if self._data else handler()
+        self.callback(self.data) if self.data else self.callback()
 
 
 class PluginMenuCmd:
@@ -204,7 +212,12 @@
             if len(path) != len(path_i18n):
                 log.error("inconsistency between menu paths")
                 continue
-            callback = PluginMenuCmd(self.menu.host, action_id, menu_data)
+            if isinstance(action_id, str):
+                callback = PluginMenuCmd(self.menu.host, action_id, menu_data)
+            elif callable(action_id):
+                callback = MenuCmd(action_id, data=menu_data)
+            else:
+                raise exceptions.InternalError
             self.addMenuItem(path, path_i18n, 'plugins', callback)