Mercurial > libervia-backend
comparison frontends/src/quick_frontend/quick_menus.py @ 1367:f71a0fc26886
merged branch frontends_multi_profiles
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 18 Mar 2015 10:52:28 +0100 |
parents | 584d45bb36d9 |
children | 069ad98b360d |
comparison
equal
deleted
inserted
replaced
1295:1e3b1f9ad6e2 | 1367:f71a0fc26886 |
---|---|
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 """add an item at the end of current ones | |
223 | |
224 @param item: instance of MenuBase (must be unique in container) | |
225 """ | |
226 assert isinstance(item, MenuItem) or isinstance(item, MenuContainer) | |
227 assert item.canonical not in self._items | |
228 self._items[item.canonical] = item | |
229 | |
230 def replace(self, item): | |
231 """add an item at the end of current ones or replace an existing one""" | |
232 self._items[item.canonical] = item | |
233 | |
234 | |
235 class MenuCategory(MenuContainer): | |
236 """A category which can hold other menus or categories""" | |
237 | |
238 def __init__(self, name, name_i18n=None, extra=None): | |
239 """ | |
240 @param name(unicode): canonical name | |
241 @param name_i18n(unicode, None): translated name | |
242 @param icon(unicode, None): same as in MenuBase.__init__ | |
243 """ | |
244 log.debug("creating menuCategory %s with extra %s" % (name, extra)) | |
245 MenuContainer.__init__(self, name, extra) | |
246 self._name_i18n = name_i18n or name | |
247 | |
248 @property | |
249 def name(self): | |
250 return self._name_i18n | |
251 | |
252 | |
253 class MenuType(MenuContainer): | |
254 """A type which can hold other menus or categories""" | |
255 pass | |
256 | |
257 | |
258 ## manager ## | |
259 | |
260 | |
261 class QuickMenusManager(object): | |
262 """Manage all the menus""" | |
263 _data_collectors={C.MENU_GLOBAL: None} # No data is associated with C.MENU_GLOBAL items | |
264 | |
265 def __init__(self, host, menus=None, language=None): | |
266 """ | |
267 @param host: %(doc_host)s | |
268 @param menus(iterable): menus as in [addMenus] | |
269 @param language: same as in [i18n.languageSwitch] | |
270 """ | |
271 self.host = host | |
272 MenuBase.host = host | |
273 self.language = language | |
274 self.menus = {} | |
275 if menus is not None: | |
276 self.addMenus(menus) | |
277 | |
278 def _getPathI18n(self, path): | |
279 """Return translated version of path""" | |
280 languageSwitch(self.language) | |
281 path_i18n = [_(elt) for elt in path] | |
282 languageSwitch() | |
283 return path_i18n | |
284 | |
285 def _createCategories(self, type_, path, path_i18n=None, top_extra=None): | |
286 """Create catogories of the path | |
287 | |
288 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
289 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
290 @param path_i18n(list[unicode], None): translated menu path (same lenght as path) or None to get deferred translation of path | |
291 @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). | |
292 @return (MenuContainer): last category created, or MenuType if path is empty | |
293 """ | |
294 if path_i18n is None: | |
295 path_i18n = self._getPathI18n(path) | |
296 assert len(path) == len(path_i18n) | |
297 menu_container = self.menus.setdefault(type_, MenuType(type_)) | |
298 | |
299 for idx, category in enumerate(path): | |
300 menu_category = MenuCategory(category, path_i18n[idx], extra=top_extra) | |
301 menu_container = menu_container.getOrCreate(menu_category) | |
302 top_extra = None | |
303 | |
304 return menu_container | |
305 | |
306 @staticmethod | |
307 def addDataCollector(type_, data_collector): | |
308 """Associate a data collector to a menu type | |
309 | |
310 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. | |
311 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
312 @param data_collector(dict[unicode,unicode], callable, None): can be: | |
313 - 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. | |
314 - a callable which must return the data dictionnary. callable will have caller and item name as argument | |
315 - None: an empty dict will be used | |
316 """ | |
317 QuickMenusManager._data_collectors[type_] = data_collector | |
318 | |
319 @staticmethod | |
320 def getDataCollector(type_): | |
321 """Get data_collector associated to type_ | |
322 | |
323 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
324 @return (callable, dict, None): data_collector | |
325 """ | |
326 try: | |
327 return QuickMenusManager._data_collectors[type_] | |
328 except KeyError: | |
329 log.error(u"No data collector registered for {}".format(type_)) | |
330 return None | |
331 | |
332 def addMenuItem(self, type_, path, item, path_i18n=None, top_extra=None): | |
333 """Add a MenuItemBase instance | |
334 | |
335 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
336 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu], stop at the last parent category | |
337 @param item(MenuItem): a instancied item | |
338 @param path_i18n(list[unicode],None): translated menu path (same lenght as path) or None to use deferred translation of path | |
339 @param top_extra: same as in [_createCategories] | |
340 """ | |
341 if path_i18n is None: | |
342 path_i18n = self._getPathI18n(path) | |
343 assert path and len(path) == len(path_i18n) | |
344 | |
345 menu_container = self._createCategories(type_, path, path_i18n, top_extra) | |
346 | |
347 if item in menu_container: | |
348 if isinstance(item, MenuHook): | |
349 menu_container.replace(item) | |
350 else: | |
351 container_item = menu_container[item] | |
352 if isinstance(container_item, MenuPlaceHolder): | |
353 menu_container.replace(item) | |
354 elif isinstance(container_item, MenuHook): | |
355 # MenuHook must not be replaced | |
356 log.debug(u"ignoring menu at path [{}] because a hook is already in place".format(path)) | |
357 else: | |
358 log.error(u"Conflicting menus at path [{}]".format(path)) | |
359 else: | |
360 log.debug(u"Adding menu [{type_}] {path}".format(type_=type_, path=path)) | |
361 menu_container.append(item) | |
362 self.host.callListeners('menu', type_, path, path_i18n, item) | |
363 | |
364 def addMenu(self, type_, path, path_i18n=None, extra=None, top_extra=None, id_=None, callback=None): | |
365 """Add a menu item | |
366 | |
367 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
368 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
369 @param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation | |
370 @param extra(dict[unicode, unicode], None): same as in [addMenus] | |
371 @param top_extra: same as in [_createCategories] | |
372 @param id_(unicode): callback id (mutually exclusive with callback) | |
373 @param callback(callable): local callback (mutually exclusive with id_) | |
374 """ | |
375 if path_i18n is None: | |
376 path_i18n = self._getPathI18n(path) | |
377 assert bool(id_) ^ bool(callback) # we must have id_ xor callback defined | |
378 if id_: | |
379 menu_item = MenuItemDistant(self.host, type_, path[-1], path_i18n[-1], id_=id_, extra=extra) | |
380 else: | |
381 menu_item = MenuItemLocal(type_, path[-1], path_i18n[-1], callback=callback, extra=extra) | |
382 self.addMenuItem(type_, path[:-1], menu_item, path_i18n[:-1], top_extra) | |
383 | |
384 def addMenus(self, menus, top_extra=None): | |
385 """Add several menus at once | |
386 | |
387 @param menus(iterable): iterable with: | |
388 id_(unicode,callable): id of distant callback or local callback | |
389 type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
390 path(iterable[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
391 path_i18n(iterable[unicode]): translated menu path (same lenght as path) | |
392 extra(dict[unicode,unicode]): dictionary of extra data (used on the leaf menu), can be: | |
393 - "icon": icon name | |
394 @param top_extra: same as in [_createCategories] | |
395 """ | |
396 # TODO: manage icons | |
397 for id_, type_, path, path_i18n, extra in menus: | |
398 if callable(id_): | |
399 self.addMenu(type_, path, path_i18n, callback=id_, extra=extra, top_extra=top_extra) | |
400 else: | |
401 self.addMenu(type_, path, path_i18n, id_=id_, extra=extra, top_extra=top_extra) | |
402 | |
403 def addMenuHook(self, type_, path, path_i18n=None, extra=None, top_extra=None, callback=None): | |
404 """Helper method to add a menu hook | |
405 | |
406 Menu hooks are local menus which override menu given by backend | |
407 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
408 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
409 @param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation | |
410 @param extra(dict[unicode, unicode], None): same as in [addMenus] | |
411 @param top_extra: same as in [_createCategories] | |
412 @param callback(callable): local callback (mutually exclusive with id_) | |
413 """ | |
414 if path_i18n is None: | |
415 path_i18n = self._getPathI18n(path) | |
416 menu_item = MenuHook(type_, path[-1], path_i18n[-1], callback=callback, extra=extra) | |
417 self.addMenuItem(type_, path[:-1], menu_item, path_i18n[:-1], top_extra) | |
418 log.info(u"Menu hook set on {path} ({type_})".format(path=path, type_=type_)) | |
419 | |
420 def addCategory(self, type_, path, path_i18n=None, extra=None, top_extra=None): | |
421 """Create a category with all parents, and set extra on the last one | |
422 | |
423 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
424 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
425 @param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation of path | |
426 @param extra(dict[unicode, unicode], None): same as in [addMenus] (added on the leaf category only) | |
427 @param top_extra: same as in [_createCategories] | |
428 @return (MenuCategory): last category add | |
429 """ | |
430 if path_i18n is None: | |
431 path_i18n = self._getPathI18n(path) | |
432 last_container = self._createCategories(type_, path, path_i18n, top_extra=top_extra) | |
433 last_container.setExtra(extra) | |
434 return last_container | |
435 | |
436 def getMainContainer(self, type_): | |
437 """Get a main MenuType container | |
438 | |
439 @param type_: a C.MENU_* constant | |
440 @return(MenuContainer): the main container | |
441 """ | |
442 menu_container = self.menus.setdefault(type_, MenuType(type_)) | |
443 return menu_container |