annotate frontends/src/quick_frontend/quick_menus.py @ 1526:bb451fd1cea3

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