# HG changeset patch # User souliane # Date 1409684939 -7200 # Node ID 85699d18921f4d1c7ec9e005e13a42524316a53c # Parent 1af112b97e45a32ec2a5e1138f2b512d810c40bc browser_side: menu callback can be a method not belonging to a class diff -r 1af112b97e45 -r 85699d18921f src/browser/sat_browser/base_menu.py --- 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)