comparison src/browser/sat_browser/base_menu.py @ 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 db3436c85fb1
children a5019e62c3e9
comparison
equal deleted inserted replaced
516:1af112b97e45 517:85699d18921f
26 26
27 import pyjd # this is dummy in pyjs 27 import pyjd # this is dummy in pyjs
28 from sat.core.log import getLogger 28 from sat.core.log import getLogger
29 log = getLogger(__name__) 29 log = getLogger(__name__)
30 30
31 from sat.core import exceptions
31 from pyjamas.ui.MenuBar import MenuBar 32 from pyjamas.ui.MenuBar import MenuBar
32 from pyjamas.ui.UIObject import UIObject 33 from pyjamas.ui.UIObject import UIObject
33 from pyjamas.ui.MenuItem import MenuItem 34 from pyjamas.ui.MenuItem import MenuItem
34 from pyjamas import Window 35 from pyjamas import Window
35 36
37 38
38 39
39 class MenuCmd: 40 class MenuCmd:
40 """Return an object with an "execute" method that can be set to a menu item callback""" 41 """Return an object with an "execute" method that can be set to a menu item callback"""
41 42
42 def __init__(self, object_, handler, data=None): 43 def __init__(self, object_, handler=None, data=None):
43 self._object = object_ 44 """
44 self._handler = handler 45 @param object_ (object): a callable or a class instance
45 self._data = data 46 @param handler (str): method name if object_ is a class instance
47 @param data (dict): data to pass as the callback argument
48 """
49 if handler is None:
50 assert(callable(object_))
51 self.callback = object_
52 else:
53 self.callback = getattr(object_, handler)
54 self.data = data
46 55
47 def execute(self): 56 def execute(self):
48 handler = getattr(self._object, self._handler) 57 self.callback(self.data) if self.data else self.callback()
49 handler(self._data) if self._data else handler()
50 58
51 59
52 class PluginMenuCmd: 60 class PluginMenuCmd:
53 """Like MenuCmd, but instead of executing a method, it will command the bridge to launch an action""" 61 """Like MenuCmd, but instead of executing a method, it will command the bridge to launch an action"""
54 62
202 menus = self.menu.host.menus.get(type_, []) 210 menus = self.menu.host.menus.get(type_, [])
203 for action_id, path, path_i18n in menus: 211 for action_id, path, path_i18n in menus:
204 if len(path) != len(path_i18n): 212 if len(path) != len(path_i18n):
205 log.error("inconsistency between menu paths") 213 log.error("inconsistency between menu paths")
206 continue 214 continue
207 callback = PluginMenuCmd(self.menu.host, action_id, menu_data) 215 if isinstance(action_id, str):
216 callback = PluginMenuCmd(self.menu.host, action_id, menu_data)
217 elif callable(action_id):
218 callback = MenuCmd(action_id, data=menu_data)
219 else:
220 raise exceptions.InternalError
208 self.addMenuItem(path, path_i18n, 'plugins', callback) 221 self.addMenuItem(path, path_i18n, 'plugins', callback)
209 222
210 223
211 class GenericMenuBar(MenuBar): 224 class GenericMenuBar(MenuBar):
212 """A menu bar with sub-categories and items""" 225 """A menu bar with sub-categories and items"""