Mercurial > libervia-backend
comparison frontends/src/quick_frontend/quick_menus.py @ 1364:28f0b33ca17c frontends_multi_profiles
quick_frontend (menus): added a quick_menus module to manage easily menus logic:
- there are 2 mains types of menu: MenuItem which can be launched, and MenuCategory which contains other menus
- local menus (from frontend only) and distant menus (from backend and its plugins) are managed
- there is a menu manager which, in a similar ways as for widgets, manages the menus logic and should be instantiated by host
- when a context is necessary (this is the case for most of menus), a data collector is used: it collects data from the caller (the instance linked to the menu) and construct data which is then sent throught bridge
- to have implementation specific to a frontend which override the backend one, there is a MenuHook class
- it is possible to place an expected menu in the desired position with MenuPlaceHolder class
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 17 Mar 2015 19:33:05 +0100 |
parents | |
children | 584d45bb36d9 |
comparison
equal
deleted
inserted
replaced
1363:fa77e40eb17b | 1364:28f0b33ca17c |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # helper class for making a SAT frontend | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 try: | |
21 # FIXME: to be removed when an acceptable solution is here | |
22 unicode('') # XXX: unicode doesn't exist in pyjamas | |
23 except (TypeError, AttributeError): # Error raised is not the same depending on pyjsbuild options | |
24 unicode = str | |
25 | |
26 from sat.core.log import getLogger | |
27 from sat.core.i18n import _, languageSwitch | |
28 log = getLogger(__name__) | |
29 from sat_frontends.quick_frontend.constants import Const as C | |
30 from collections import OrderedDict | |
31 | |
32 | |
33 ## items ## | |
34 | |
35 class MenuBase(object): | |
36 ACTIVE=True | |
37 | |
38 def __init__(self, name, extra=None): | |
39 """ | |
40 @param name(unicode): canonical name of the item | |
41 @param extra(dict[unicode, unicode], None): same as in [addMenus] | |
42 """ | |
43 self._name = name | |
44 self.setExtra(extra) | |
45 | |
46 @property | |
47 def canonical(self): | |
48 """Return the canonical name of the container, used to identify it""" | |
49 return self._name | |
50 | |
51 @property | |
52 def name(self): | |
53 """Return the name of the container, can be translated""" | |
54 return self._name | |
55 | |
56 def setExtra(self, extra): | |
57 if extra is None: | |
58 extra = {} | |
59 self.icon = extra.get("icon") | |
60 | |
61 | |
62 class MenuItem(MenuBase): | |
63 """A callable item in the menu""" | |
64 CALLABLE=False | |
65 | |
66 def __init__(self, name, name_i18n, extra=None, type_=None): | |
67 """ | |
68 @param name(unicode): canonical name of the item | |
69 @param name_i18n(unicode): translated name of the item | |
70 @param extra(dict[unicode, unicode], None): same as in [addMenus] | |
71 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
72 """ | |
73 MenuBase.__init__(self, name, extra) | |
74 self._name_i18n = name_i18n if name_i18n else name | |
75 self.type = type_ | |
76 | |
77 @property | |
78 def name(self): | |
79 return self._name_i18n | |
80 | |
81 def collectData(self, caller): | |
82 """Get data according to data_collector | |
83 | |
84 @param caller: Menu caller | |
85 """ | |
86 assert self.type is not None # if data collector are used, type must be set | |
87 data_collector = QuickMenusManager.getDataCollector(self.type) | |
88 | |
89 if data_collector is None: | |
90 return {} | |
91 | |
92 elif callable(data_collector): | |
93 return data_collector(caller, self.name) | |
94 | |
95 else: | |
96 if caller is None: | |
97 log.error(u"Caller can't be None with a dictionary as data_collector") | |
98 return {} | |
99 data = {} | |
100 for data_key, caller_attr in data_collector.iteritems(): | |
101 data[data_key] = unicode(getattr(caller, caller_attr)) | |
102 return data | |
103 | |
104 | |
105 def call(self, caller, profile=C.PROF_KEY_NONE): | |
106 """Execute the menu item | |
107 | |
108 @param caller: instance linked to the menu | |
109 @param profile: %(doc_profile)s | |
110 """ | |
111 raise NotImplementedError | |
112 | |
113 | |
114 class MenuItemDistant(MenuItem): | |
115 """A MenuItem with a distant callback""" | |
116 CALLABLE=True | |
117 | |
118 def __init__(self, host, type_, name, name_i18n, id_, extra=None): | |
119 """ | |
120 @param host: %(doc_host)s | |
121 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
122 @param name(unicode): canonical name of the item | |
123 @param name_i18n(unicode): translated name of the item | |
124 @param id_(unicode): id of the distant callback | |
125 @param extra(dict[unicode, unicode], None): same as in [addMenus] | |
126 """ | |
127 MenuItem.__init__(self, name, name_i18n, extra, type_) | |
128 self.host = host | |
129 self.id = id_ | |
130 | |
131 def call(self, caller, profile=C.PROF_KEY_NONE): | |
132 data = self.collectData(caller) | |
133 log.debug("data collected: %s" % data) | |
134 self.host.launchAction(self.id, data, profile=profile) | |
135 | |
136 | |
137 class MenuItemLocal(MenuItem): | |
138 """A MenuItem with a distant callback""" | |
139 CALLABLE=True | |
140 | |
141 def __init__(self, type_, name, name_i18n, callback, extra=None): | |
142 """ | |
143 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
144 @param name(unicode): canonical name of the item | |
145 @param name_i18n(unicode): translated name of the item | |
146 @param callback(callable): local callback. Will be called with the caller as only argument if data_collector is None, else with the caller + requested data + profile. | |
147 @param extra(dict[unicode, unicode], None): same as in [addMenus] | |
148 """ | |
149 MenuItem.__init__(self, name, name_i18n, extra, type_) | |
150 self.callback = callback | |
151 | |
152 def call(self, caller, profile=C.PROF_KEY_NONE): | |
153 data_collector = QuickMenusManager.getDataCollector(self.type) | |
154 if data_collector is None: | |
155 self.callback() | |
156 else: | |
157 self.callback(caller, self.collectData(caller), profile) | |
158 | |
159 | |
160 class MenuHook(MenuItemLocal): | |
161 """A MenuItem which replace an expected item from backend""" | |
162 pass | |
163 | |
164 | |
165 class MenuPlaceHolder(MenuItem): | |
166 """A non existant menu which is used to keep a position""" | |
167 ACTIVE=False | |
168 | |
169 def __init__(self, name): | |
170 MenuItem.__init__(self, name, name) | |
171 | |
172 | |
173 class MenuSeparator(MenuItem): | |
174 """A separation between items/categories""" | |
175 SEP_IDX=0 | |
176 | |
177 def __init__(self): | |
178 MenuSeparator.SEP_IDX +=1 | |
179 name = u"___separator_{}".format(MenuSeparator.SEP_IDX) | |
180 MenuItem.__init__(self, name, name) | |
181 | |
182 | |
183 ## containers ## | |
184 | |
185 | |
186 class MenuContainer(MenuBase): | |
187 | |
188 def __init__(self, name, extra=None): | |
189 MenuBase.__init__(self, name, extra) | |
190 self._items = OrderedDict() | |
191 | |
192 def __len__(self): | |
193 return len(self._items) | |
194 | |
195 def __contains__(self, item): | |
196 return item.canonical in self._items | |
197 | |
198 def __iter__(self): | |
199 return self._items.itervalues() | |
200 | |
201 def __getitem__(self, item): | |
202 try: | |
203 return self._items[item.canonical] | |
204 except KeyError: | |
205 raise KeyError(item) | |
206 | |
207 def getOrCreate(self, item): | |
208 log.debug("MenuContainer getOrCreate: item=%s name=%s\nlist=%s" % (item, item.canonical, self._items.keys())) | |
209 try: | |
210 return self[item] | |
211 except KeyError: | |
212 self.append(item) | |
213 return item | |
214 | |
215 def getActiveMenus(self): | |
216 """Return an iterator on active children""" | |
217 for child in self._items.itervalues(): | |
218 if child.ACTIVE: | |
219 yield child | |
220 | |
221 def append(self, item): | |
222 assert isinstance(item, MenuItem) or isinstance(item, MenuContainer) | |
223 self._items[item.canonical] = item | |
224 | |
225 | |
226 class MenuCategory(MenuContainer): | |
227 """A category which can hold other menus or categories""" | |
228 | |
229 def __init__(self, name, name_i18n=None, extra=None): | |
230 """ | |
231 @param name(unicode): canonical name | |
232 @param name_i18n(unicode, None): translated name | |
233 @param icon(unicode, None): same as in MenuBase.__init__ | |
234 """ | |
235 log.debug("creating menuCategory %s with extra %s" % (name, extra)) | |
236 MenuContainer.__init__(self, name, extra) | |
237 self._name_i18n = name_i18n or name | |
238 | |
239 @property | |
240 def name(self): | |
241 return self._name_i18n | |
242 | |
243 | |
244 class MenuType(MenuContainer): | |
245 """A type which can hold other menus or categories""" | |
246 pass | |
247 | |
248 | |
249 ## manager ## | |
250 | |
251 | |
252 class QuickMenusManager(object): | |
253 """Manage all the menus""" | |
254 _data_collectors={C.MENU_GLOBAL: None} # No data is associated with C.MENU_GLOBAL items | |
255 | |
256 def __init__(self, host, menus=None, language=None): | |
257 """ | |
258 @param host: %(doc_host)s | |
259 @param menus(iterable): menus as in [addMenus] | |
260 @param language: same as in [i18n.languageSwitch] | |
261 """ | |
262 self.host = host | |
263 MenuBase.host = host | |
264 self.language = language | |
265 self.menus = {} | |
266 if menus is not None: | |
267 self.addMenus(menus) | |
268 | |
269 def _getPathI18n(self, path): | |
270 """Return translated version of path""" | |
271 languageSwitch(self.language) | |
272 path_i18n = [_(elt) for elt in path] | |
273 languageSwitch() | |
274 return path_i18n | |
275 | |
276 def _createCategories(self, type_, path, path_i18n=None, top_extra=None): | |
277 """Create catogories of the path | |
278 | |
279 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
280 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
281 @param path_i18n(list[unicode], None): translated menu path (same lenght as path) or None to get deferred translation of path | |
282 @param top_extra: extra data to use on the first element of path only. If the first element already exists and is reused, top_extra will be ignored (you'll have to manually change it if you really want to). | |
283 @return (MenuContainer): last category created, or MenuType if path is empty | |
284 """ | |
285 if path_i18n is None: | |
286 path_i18n = self._getPathI18n(path) | |
287 assert len(path) == len(path_i18n) | |
288 menu_container = self.menus.setdefault(type_, MenuType(type_)) | |
289 | |
290 for idx, category in enumerate(path): | |
291 menu_category = MenuCategory(category, path_i18n[idx], extra=top_extra) | |
292 menu_container = menu_container.getOrCreate(menu_category) | |
293 top_extra = None | |
294 | |
295 return menu_container | |
296 | |
297 @staticmethod | |
298 def addDataCollector(type_, data_collector): | |
299 """Associate a data collector to a menu type | |
300 | |
301 A data collector is a method or a map which allow to collect context data to construct the dictionnary which will be sent to the bridge method managing the menu item. | |
302 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
303 @param data_collector(dict[unicode,unicode], callable, None): can be: | |
304 - a dict which map data name to local name. The attribute named after the dict values will be getted from caller, and put in data. e.g.: if data_collector={'room_jid':'target'}, then the "room_jid" data will be the value of the "target" attribute of the caller. | |
305 - a callable which must return the data dictionnary. callable will have caller and item name as argument | |
306 - None: an empty dict will be used | |
307 """ | |
308 QuickMenusManager._data_collectors[type_] = data_collector | |
309 | |
310 @staticmethod | |
311 def getDataCollector(type_): | |
312 """Get data_collector associated to type_ | |
313 | |
314 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
315 @return (callable, dict, None): data_collector | |
316 """ | |
317 try: | |
318 return QuickMenusManager._data_collectors[type_] | |
319 except KeyError: | |
320 log.error(u"No data collector registered for {}".format(type_)) | |
321 return None | |
322 | |
323 def addMenuItem(self, type_, path, item, path_i18n=None, top_extra=None): | |
324 """Add a MenuItemBase instance | |
325 | |
326 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
327 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu], stop at the last parent category | |
328 @param item(MenuItem): a instancied item | |
329 @param path_i18n(list[unicode],None): translated menu path (same lenght as path) or None to use deferred translation of path | |
330 @param top_extra: same as in [_createCategories] | |
331 """ | |
332 if path_i18n is None: | |
333 path_i18n = self._getPathI18n(path) | |
334 assert path and len(path) == len(path_i18n) | |
335 | |
336 menu_container = self._createCategories(type_, path, path_i18n, top_extra) | |
337 | |
338 if item in menu_container: | |
339 if isinstance(item, MenuHook): | |
340 menu_container.replace(item) | |
341 else: | |
342 container_item = menu_container[item] | |
343 if isinstance(container_item, MenuPlaceHolder): | |
344 menu_container.replace(item) | |
345 elif isinstance(container_item, MenuHook): | |
346 # MenuHook must not be replaced | |
347 log.debug(u"ignoring menu at path [{}] because a hook is already in place".format(path)) | |
348 else: | |
349 log.error(u"Conflicting menus at path [{}]".format(path)) | |
350 else: | |
351 log.debug(u"Adding menu [{type_}] {path}".format(type_=type_, path=path)) | |
352 menu_container.append(item) | |
353 self.host.callListeners('menu', type_, path, path_i18n, item) | |
354 | |
355 def addMenu(self, type_, path, path_i18n=None, extra=None, top_extra=None, id_=None, callback=None): | |
356 """Add a menu item | |
357 | |
358 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
359 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
360 @param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation | |
361 @param extra(dict[unicode, unicode], None): same as in [addMenus] | |
362 @param top_extra: same as in [_createCategories] | |
363 @param id_(unicode): callback id (mutually exclusive with callback) | |
364 @param callback(callable): local callback (mutually exclusive with id_) | |
365 """ | |
366 if path_i18n is None: | |
367 path_i18n = self._getPathI18n(path) | |
368 assert bool(id_) ^ bool(callback) # we must have id_ xor callback defined | |
369 if id_: | |
370 menu_item = MenuItemDistant(self.host, type_, path[-1], path_i18n[-1], id_=id_, extra=extra) | |
371 else: | |
372 menu_item = MenuItemLocal(type_, path[-1], path_i18n[-1], callback=callback, extra=extra) | |
373 self.addMenuItem(type_, path[:-1], menu_item, path_i18n[:-1], top_extra) | |
374 | |
375 def addMenus(self, menus, top_extra=None): | |
376 """Add several menus at once | |
377 | |
378 @param menus(iterable): iterable with: | |
379 id_(unicode,callable): id of distant callback or local callback | |
380 type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
381 path(iterable[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
382 path_i18n(iterable[unicode]): translated menu path (same lenght as path) | |
383 extra(dict[unicode,unicode]): dictionary of extra data (used on the leaf menu), can be: | |
384 - "icon": icon name | |
385 @param top_extra: same as in [_createCategories] | |
386 """ | |
387 # TODO: manage icons | |
388 for id_, type_, path, path_i18n, extra in menus: | |
389 if callable(id_): | |
390 self.addMenu(type_, path, path_i18n, callback=id_, extra=extra, top_extra=top_extra) | |
391 else: | |
392 self.addMenu(type_, path, path_i18n, id_=id_, extra=extra, top_extra=top_extra) | |
393 | |
394 def addCategory(self, type_, path, path_i18n=None, extra=None, top_extra=None): | |
395 """Create a category with all parents, and set extra on the last one | |
396 | |
397 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
398 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
399 @param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation of path | |
400 @param extra(dict[unicode, unicode], None): same as in [addMenus] (added on the leaf category only) | |
401 @param top_extra: same as in [_createCategories] | |
402 @return (MenuCategory): last category add | |
403 """ | |
404 if path_i18n is None: | |
405 path_i18n = self._getPathI18n(path) | |
406 last_container = self._createCategories(type_, path, path_i18n, top_extra=top_extra) | |
407 last_container.setExtra(extra) | |
408 return last_container | |
409 | |
410 def getMainContainer(self, type_): | |
411 """Get a main MenuType container | |
412 | |
413 @param type_: a C.MENU_* constant | |
414 @return(MenuContainer): the main container | |
415 """ | |
416 menu_container = self.menus.setdefault(type_, MenuType(type_)) | |
417 return menu_container |