Mercurial > libervia-backend
comparison sat_frontends/quick_frontend/quick_menus.py @ 2562:26edcf3a30eb
core, setup: huge cleaning:
- moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention
- move twisted directory to root
- removed all hacks from setup.py, and added missing dependencies, it is now clean
- use https URL for website in setup.py
- removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed
- renamed sat.sh to sat and fixed its installation
- added python_requires to specify Python version needed
- replaced glib2reactor which use deprecated code by gtk3reactor
sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 02 Apr 2018 19:44:50 +0200 |
parents | frontends/src/quick_frontend/quick_menus.py@0046283a285d |
children | 56f94936df1e |
comparison
equal
deleted
inserted
replaced
2561:bd30dc3ffe5a | 2562:26edcf3a30eb |
---|---|
1 #!/usr/bin/env python2 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # helper class for making a SAT frontend | |
5 # Copyright (C) 2009-2018 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 local 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. | |
147 Will be called with no argument if data_collector is None | |
148 and with caller, profile, and requested data otherwise | |
149 @param extra(dict[unicode, unicode], None): same as in [addMenus] | |
150 """ | |
151 MenuItem.__init__(self, name, name_i18n, extra, type_) | |
152 self.callback = callback | |
153 | |
154 def call(self, caller, profile=C.PROF_KEY_NONE): | |
155 data_collector = QuickMenusManager.getDataCollector(self.type) | |
156 if data_collector is None: | |
157 # FIXME: would not it be better if caller and profile where used as arguments? | |
158 self.callback() | |
159 else: | |
160 self.callback(caller, self.collectData(caller), profile) | |
161 | |
162 | |
163 class MenuHook(MenuItemLocal): | |
164 """A MenuItem which replace an expected item from backend""" | |
165 pass | |
166 | |
167 | |
168 class MenuPlaceHolder(MenuItem): | |
169 """A non existant menu which is used to keep a position""" | |
170 ACTIVE=False | |
171 | |
172 def __init__(self, name): | |
173 MenuItem.__init__(self, name, name) | |
174 | |
175 | |
176 class MenuSeparator(MenuItem): | |
177 """A separation between items/categories""" | |
178 SEP_IDX=0 | |
179 | |
180 def __init__(self): | |
181 MenuSeparator.SEP_IDX +=1 | |
182 name = u"___separator_{}".format(MenuSeparator.SEP_IDX) | |
183 MenuItem.__init__(self, name, name) | |
184 | |
185 | |
186 ## containers ## | |
187 | |
188 | |
189 class MenuContainer(MenuBase): | |
190 | |
191 def __init__(self, name, extra=None): | |
192 MenuBase.__init__(self, name, extra) | |
193 self._items = OrderedDict() | |
194 | |
195 def __len__(self): | |
196 return len(self._items) | |
197 | |
198 def __contains__(self, item): | |
199 return item.canonical in self._items | |
200 | |
201 def __iter__(self): | |
202 return self._items.itervalues() | |
203 | |
204 def __getitem__(self, item): | |
205 try: | |
206 return self._items[item.canonical] | |
207 except KeyError: | |
208 raise KeyError(item) | |
209 | |
210 def getOrCreate(self, item): | |
211 log.debug(u"MenuContainer getOrCreate: item=%s name=%s\nlist=%s" % (item, item.canonical, self._items.keys())) | |
212 try: | |
213 return self[item] | |
214 except KeyError: | |
215 self.append(item) | |
216 return item | |
217 | |
218 def getActiveMenus(self): | |
219 """Return an iterator on active children""" | |
220 for child in self._items.itervalues(): | |
221 if child.ACTIVE: | |
222 yield child | |
223 | |
224 def append(self, item): | |
225 """add an item at the end of current ones | |
226 | |
227 @param item: instance of MenuBase (must be unique in container) | |
228 """ | |
229 assert isinstance(item, MenuItem) or isinstance(item, MenuContainer) | |
230 assert item.canonical not in self._items | |
231 self._items[item.canonical] = item | |
232 | |
233 def replace(self, item): | |
234 """add an item at the end of current ones or replace an existing one""" | |
235 self._items[item.canonical] = item | |
236 | |
237 | |
238 class MenuCategory(MenuContainer): | |
239 """A category which can hold other menus or categories""" | |
240 | |
241 def __init__(self, name, name_i18n=None, extra=None): | |
242 """ | |
243 @param name(unicode): canonical name | |
244 @param name_i18n(unicode, None): translated name | |
245 @param icon(unicode, None): same as in MenuBase.__init__ | |
246 """ | |
247 log.debug("creating menuCategory %s with extra %s" % (name, extra)) | |
248 MenuContainer.__init__(self, name, extra) | |
249 self._name_i18n = name_i18n or name | |
250 | |
251 @property | |
252 def name(self): | |
253 return self._name_i18n | |
254 | |
255 | |
256 class MenuType(MenuContainer): | |
257 """A type which can hold other menus or categories""" | |
258 pass | |
259 | |
260 | |
261 ## manager ## | |
262 | |
263 | |
264 class QuickMenusManager(object): | |
265 """Manage all the menus""" | |
266 _data_collectors={C.MENU_GLOBAL: None} # No data is associated with C.MENU_GLOBAL items | |
267 | |
268 def __init__(self, host, menus=None, language=None): | |
269 """ | |
270 @param host: %(doc_host)s | |
271 @param menus(iterable): menus as in [addMenus] | |
272 @param language: same as in [i18n.languageSwitch] | |
273 """ | |
274 self.host = host | |
275 MenuBase.host = host | |
276 self.language = language | |
277 self.menus = {} | |
278 if menus is not None: | |
279 self.addMenus(menus) | |
280 | |
281 def _getPathI18n(self, path): | |
282 """Return translated version of path""" | |
283 languageSwitch(self.language) | |
284 path_i18n = [_(elt) for elt in path] | |
285 languageSwitch() | |
286 return path_i18n | |
287 | |
288 def _createCategories(self, type_, path, path_i18n=None, top_extra=None): | |
289 """Create catogories of the path | |
290 | |
291 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
292 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
293 @param path_i18n(list[unicode], None): translated menu path (same lenght as path) or None to get deferred translation of path | |
294 @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). | |
295 @return (MenuContainer): last category created, or MenuType if path is empty | |
296 """ | |
297 if path_i18n is None: | |
298 path_i18n = self._getPathI18n(path) | |
299 assert len(path) == len(path_i18n) | |
300 menu_container = self.menus.setdefault(type_, MenuType(type_)) | |
301 | |
302 for idx, category in enumerate(path): | |
303 menu_category = MenuCategory(category, path_i18n[idx], extra=top_extra) | |
304 menu_container = menu_container.getOrCreate(menu_category) | |
305 top_extra = None | |
306 | |
307 return menu_container | |
308 | |
309 @staticmethod | |
310 def addDataCollector(type_, data_collector): | |
311 """Associate a data collector to a menu type | |
312 | |
313 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. | |
314 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
315 @param data_collector(dict[unicode,unicode], callable, None): can be: | |
316 - a dict which map data name to local name. | |
317 The attribute named after the dict values will be getted from caller, and put in data. | |
318 e.g.: if data_collector={'room_jid':'target'}, then the "room_jid" data will be the value of the "target" attribute of the caller. | |
319 - a callable which must return the data dictionnary. callable will have caller and item name as argument | |
320 - None: an empty dict will be used | |
321 """ | |
322 QuickMenusManager._data_collectors[type_] = data_collector | |
323 | |
324 @staticmethod | |
325 def getDataCollector(type_): | |
326 """Get data_collector associated to type_ | |
327 | |
328 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
329 @return (callable, dict, None): data_collector | |
330 """ | |
331 try: | |
332 return QuickMenusManager._data_collectors[type_] | |
333 except KeyError: | |
334 log.error(u"No data collector registered for {}".format(type_)) | |
335 return None | |
336 | |
337 def addMenuItem(self, type_, path, item, path_i18n=None, top_extra=None): | |
338 """Add a MenuItemBase instance | |
339 | |
340 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
341 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu], stop at the last parent category | |
342 @param item(MenuItem): a instancied item | |
343 @param path_i18n(list[unicode],None): translated menu path (same lenght as path) or None to use deferred translation of path | |
344 @param top_extra: same as in [_createCategories] | |
345 """ | |
346 if path_i18n is None: | |
347 path_i18n = self._getPathI18n(path) | |
348 assert path and len(path) == len(path_i18n) | |
349 | |
350 menu_container = self._createCategories(type_, path, path_i18n, top_extra) | |
351 | |
352 if item in menu_container: | |
353 if isinstance(item, MenuHook): | |
354 menu_container.replace(item) | |
355 else: | |
356 container_item = menu_container[item] | |
357 if isinstance(container_item, MenuPlaceHolder): | |
358 menu_container.replace(item) | |
359 elif isinstance(container_item, MenuHook): | |
360 # MenuHook must not be replaced | |
361 log.debug(u"ignoring menu at path [{}] because a hook is already in place".format(path)) | |
362 else: | |
363 log.error(u"Conflicting menus at path [{}]".format(path)) | |
364 else: | |
365 log.debug(u"Adding menu [{type_}] {path}".format(type_=type_, path=path)) | |
366 menu_container.append(item) | |
367 self.host.callListeners('menu', type_, path, path_i18n, item) | |
368 | |
369 def addMenu(self, type_, path, path_i18n=None, extra=None, top_extra=None, id_=None, callback=None): | |
370 """Add a menu item | |
371 | |
372 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
373 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
374 @param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation | |
375 @param extra(dict[unicode, unicode], None): same as in [addMenus] | |
376 @param top_extra: same as in [_createCategories] | |
377 @param id_(unicode): callback id (mutually exclusive with callback) | |
378 @param callback(callable): local callback (mutually exclusive with id_) | |
379 """ | |
380 if path_i18n is None: | |
381 path_i18n = self._getPathI18n(path) | |
382 assert bool(id_) ^ bool(callback) # we must have id_ xor callback defined | |
383 if id_: | |
384 menu_item = MenuItemDistant(self.host, type_, path[-1], path_i18n[-1], id_=id_, extra=extra) | |
385 else: | |
386 menu_item = MenuItemLocal(type_, path[-1], path_i18n[-1], callback=callback, extra=extra) | |
387 self.addMenuItem(type_, path[:-1], menu_item, path_i18n[:-1], top_extra) | |
388 | |
389 def addMenus(self, menus, top_extra=None): | |
390 """Add several menus at once | |
391 | |
392 @param menus(iterable): iterable with: | |
393 id_(unicode,callable): id of distant callback or local callback | |
394 type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
395 path(iterable[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
396 path_i18n(iterable[unicode]): translated menu path (same lenght as path) | |
397 extra(dict[unicode,unicode]): dictionary of extra data (used on the leaf menu), can be: | |
398 - "icon": icon name | |
399 @param top_extra: same as in [_createCategories] | |
400 """ | |
401 # TODO: manage icons | |
402 for id_, type_, path, path_i18n, extra in menus: | |
403 if callable(id_): | |
404 self.addMenu(type_, path, path_i18n, callback=id_, extra=extra, top_extra=top_extra) | |
405 else: | |
406 self.addMenu(type_, path, path_i18n, id_=id_, extra=extra, top_extra=top_extra) | |
407 | |
408 def addMenuHook(self, type_, path, path_i18n=None, extra=None, top_extra=None, callback=None): | |
409 """Helper method to add a menu hook | |
410 | |
411 Menu hooks are local menus which override menu given by backend | |
412 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
413 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
414 @param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation | |
415 @param extra(dict[unicode, unicode], None): same as in [addMenus] | |
416 @param top_extra: same as in [_createCategories] | |
417 @param callback(callable): local callback (mutually exclusive with id_) | |
418 """ | |
419 if path_i18n is None: | |
420 path_i18n = self._getPathI18n(path) | |
421 menu_item = MenuHook(type_, path[-1], path_i18n[-1], callback=callback, extra=extra) | |
422 self.addMenuItem(type_, path[:-1], menu_item, path_i18n[:-1], top_extra) | |
423 log.info(u"Menu hook set on {path} ({type_})".format(path=path, type_=type_)) | |
424 | |
425 def addCategory(self, type_, path, path_i18n=None, extra=None, top_extra=None): | |
426 """Create a category with all parents, and set extra on the last one | |
427 | |
428 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] | |
429 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu] | |
430 @param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation of path | |
431 @param extra(dict[unicode, unicode], None): same as in [addMenus] (added on the leaf category only) | |
432 @param top_extra: same as in [_createCategories] | |
433 @return (MenuCategory): last category add | |
434 """ | |
435 if path_i18n is None: | |
436 path_i18n = self._getPathI18n(path) | |
437 last_container = self._createCategories(type_, path, path_i18n, top_extra=top_extra) | |
438 last_container.setExtra(extra) | |
439 return last_container | |
440 | |
441 def getMainContainer(self, type_): | |
442 """Get a main MenuType container | |
443 | |
444 @param type_: a C.MENU_* constant | |
445 @return(MenuContainer): the main container | |
446 """ | |
447 menu_container = self.menus.setdefault(type_, MenuType(type_)) | |
448 return menu_container |