annotate libervia/frontends/quick_frontend/quick_menus.py @ 4261:5f83bba6cd27

doc (configuration): replace deprecated `SafeConfigParser`.
author Goffi <goffi@goffi.org>
date Wed, 12 Jun 2024 22:33:10 +0200
parents 26b7ed2817da
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #!/usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
1364
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
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 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
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
22 str("") # XXX: unicode doesn't exist in pyjamas
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
23 except (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
24 TypeError,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
25 AttributeError,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
26 ): # Error raised is not the same depending on pyjsbuild options
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
27 str = str
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
29 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
30 from libervia.backend.core.i18n import _, language_switch
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
31
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 log = getLogger(__name__)
4074
26b7ed2817da refactoring: rename `sat_frontends` to `libervia.frontends`
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
33 from libervia.frontends.quick_frontend.constants import Const as C
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 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
35
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 ## items ##
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
38
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
39
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 class MenuBase(object):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
41 ACTIVE = True
1364
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 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
44 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 @param name(unicode): canonical name of the item
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
46 @param extra(dict[unicode, unicode], None): same as in [add_menus]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 self._name = name
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
49 self.set_extra(extra)
1364
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 canonical(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 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
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 @property
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def name(self):
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 """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
59 return self._name
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
61 def set_extra(self, extra):
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 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
63 extra = {}
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 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
65
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
66
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 class MenuItem(MenuBase):
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """A callable item in the menu"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
70 CALLABLE = False
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
71
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 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
73 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 @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
75 @param name_i18n(unicode): translated name of the item
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
76 @param extra(dict[unicode, unicode], None): same as in [add_menus]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
77 @param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 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
80 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
81 self.type = type_
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 @property
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 def name(self):
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 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
86
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
87 def collect_data(self, caller):
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 """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
89
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 @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
91 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 assert self.type is not None # if data collector are used, type must be set
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
93 data_collector = QuickMenusManager.get_data_collector(self.type)
1364
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 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
96 return {}
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 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
99 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
100
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 else:
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 if caller is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
103 log.error("Caller can't be None with a dictionary as data_collector")
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 return {}
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 data = {}
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
106 for data_key, caller_attr in data_collector.items():
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
107 data[data_key] = str(getattr(caller, caller_attr))
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 return data
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 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
111 """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
112
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 @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
114 @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
115 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 raise NotImplementedError
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
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 class MenuItemDistant(MenuItem):
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 """A MenuItem with a distant callback"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
121
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
122 CALLABLE = True
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
123
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 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
125 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 @param host: %(doc_host)s
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
127 @param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 @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
129 @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
130 @param id_(unicode): id of the distant callback
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
131 @param extra(dict[unicode, unicode], None): same as in [add_menus]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 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
134 self.host = host
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 self.id = id_
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 def call(self, caller, profile=C.PROF_KEY_NONE):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
138 data = self.collect_data(caller)
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 log.debug("data collected: %s" % data)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
140 self.host.action_launch(self.id, data, profile=profile)
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
141
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 class MenuItemLocal(MenuItem):
2070
58f0c96d60e5 quick frontend (menus): minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
144 """A MenuItem with a local callback"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
145
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
146 CALLABLE = True
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
147
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 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
149 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
150 @param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 @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
152 @param name_i18n(unicode): translated name of the item
2070
58f0c96d60e5 quick frontend (menus): minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
153 @param callback(callable): local callback.
58f0c96d60e5 quick frontend (menus): minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
154 Will be called with no argument if data_collector is None
58f0c96d60e5 quick frontend (menus): minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
155 and with caller, profile, and requested data otherwise
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
156 @param extra(dict[unicode, unicode], None): same as in [add_menus]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 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
159 self.callback = callback
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
160
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 def call(self, caller, profile=C.PROF_KEY_NONE):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
162 data_collector = QuickMenusManager.get_data_collector(self.type)
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 if data_collector is None:
2070
58f0c96d60e5 quick frontend (menus): minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
164 # FIXME: would not it be better if caller and profile where used as arguments?
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 self.callback()
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
167 self.callback(caller, self.collect_data(caller), profile)
1364
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
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 class MenuHook(MenuItemLocal):
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 """A MenuItem which replace an expected item from backend"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
172
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 pass
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
174
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
175
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 class MenuPlaceHolder(MenuItem):
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 """A non existant menu which is used to keep a position"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
178
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
179 ACTIVE = False
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
180
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 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
182 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
183
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 class MenuSeparator(MenuItem):
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 """A separation between items/categories"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
187
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
188 SEP_IDX = 0
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
189
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 def __init__(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
191 MenuSeparator.SEP_IDX += 1
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
192 name = "___separator_{}".format(MenuSeparator.SEP_IDX)
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 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
194
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
195
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 ## containers ##
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
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 class MenuContainer(MenuBase):
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 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
201 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
202 self._items = OrderedDict()
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
203
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 def __len__(self):
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 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
206
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 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
208 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
209
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 def __iter__(self):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
211 return iter(self._items.values())
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
212
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 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
214 try:
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 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
216 except KeyError:
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 raise KeyError(item)
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
218
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
219 def get_or_create(self, item):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
220 log.debug(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
221 "MenuContainer get_or_create: item=%s name=%s\nlist=%s"
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
222 % (item, item.canonical, list(self._items.keys()))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
223 )
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 try:
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 return self[item]
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 except KeyError:
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 self.append(item)
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 return item
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
229
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
230 def get_active_menus(self):
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 """Return an iterator on active children"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
232 for child in self._items.values():
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 if child.ACTIVE:
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 yield child
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
235
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 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
237 """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
238
584d45bb36d9 quick_frontends(menus): added MenuContainer.replace method + addMenuHook helper method in QuickMenusManager
Goffi <goffi@goffi.org>
parents: 1364
diff changeset
239 @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
240 """
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 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
242 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
243 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
244
584d45bb36d9 quick_frontends(menus): added MenuContainer.replace method + addMenuHook helper method in QuickMenusManager
Goffi <goffi@goffi.org>
parents: 1364
diff changeset
245 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
246 """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
247 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
248
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
249
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 class MenuCategory(MenuContainer):
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 """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
252
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 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
254 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 @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
256 @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
257 @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
258 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 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
260 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
261 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
262
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 @property
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 def name(self):
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 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
266
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
267
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 class MenuType(MenuContainer):
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 """A type which can hold other menus or categories"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
270
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 pass
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
272
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
273
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 ## manager ##
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
275
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
276
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 class QuickMenusManager(object):
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 """Manage all the menus"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
279
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
280 _data_collectors = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
281 C.MENU_GLOBAL: None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
282 } # No data is associated with C.MENU_GLOBAL items
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
283
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 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
285 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 @param host: %(doc_host)s
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
287 @param menus(iterable): menus as in [add_menus]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
288 @param language: same as in [i18n.language_switch]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 self.host = host
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 MenuBase.host = host
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 self.language = language
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 self.menus = {}
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 if menus is not None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
295 self.add_menus(menus)
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
296
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
297 def _get_path_i_1_8_n(self, path):
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 """Return translated version of path"""
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
299 language_switch(self.language)
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 path_i18n = [_(elt) for elt in path]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
301 language_switch()
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 return path_i18n
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
303
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
304 def _create_categories(self, type_, path, path_i18n=None, top_extra=None):
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
305 """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
306
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
307 @param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
308 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.import_menu]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 @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
310 @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
311 @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
312 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 if path_i18n is None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
314 path_i18n = self._get_path_i_1_8_n(path)
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 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
316 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
317
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 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
319 menu_category = MenuCategory(category, path_i18n[idx], extra=top_extra)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
320 menu_container = menu_container.get_or_create(menu_category)
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 top_extra = None
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 return menu_container
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
324
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 @staticmethod
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
326 def add_data_collector(type_, data_collector):
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
327 """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
328
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 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.
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
330 @param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 @param data_collector(dict[unicode,unicode], callable, None): can be:
2070
58f0c96d60e5 quick frontend (menus): minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
332 - a dict which map data name to local name.
58f0c96d60e5 quick frontend (menus): minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
333 The attribute named after the dict values will be getted from caller, and put in data.
58f0c96d60e5 quick frontend (menus): minor docstring fixes
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
334 e.g.: if data_collector={'room_jid':'target'}, then the "room_jid" data will be the value of the "target" attribute of the caller.
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 - 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
336 - 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
337 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 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
339
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
340 @staticmethod
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
341 def get_data_collector(type_):
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
342 """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
343
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
344 @param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
345 @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
346 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 try:
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
348 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
349 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
350 log.error("No data collector registered for {}".format(type_))
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
351 return None
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
352
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
353 def add_menu_item(self, type_, path, item, path_i18n=None, top_extra=None):
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
354 """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
355
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
356 @param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
357 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.import_menu], stop at the last parent category
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
358 @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
359 @param path_i18n(list[unicode],None): translated menu path (same lenght as path) or None to use deferred translation of path
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
360 @param top_extra: same as in [_create_categories]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
361 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
362 if path_i18n is None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
363 path_i18n = self._get_path_i_1_8_n(path)
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
364 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
365
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
366 menu_container = self._create_categories(type_, path, path_i18n, top_extra)
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
367
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
368 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
369 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
370 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
371 else:
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
372 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
373 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
374 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
375 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
376 # MenuHook must not be replaced
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
377 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
378 "ignoring menu at path [{}] because a hook is already in place".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
379 path
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
380 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
381 )
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
382 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
383 log.error("Conflicting menus at path [{}]".format(path))
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
384 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
385 log.debug("Adding menu [{type_}] {path}".format(type_=type_, path=path))
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
386 menu_container.append(item)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
387 self.host.call_listeners("menu", type_, path, path_i18n, item)
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
388
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
389 def add_menu(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
390 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
391 type_,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
392 path,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
393 path_i18n=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
394 extra=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
395 top_extra=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
396 id_=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
397 callback=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
398 ):
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
399 """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
400
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
401 @param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
402 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.import_menu]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
403 @param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
404 @param extra(dict[unicode, unicode], None): same as in [add_menus]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
405 @param top_extra: same as in [_create_categories]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
406 @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
407 @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
408 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
409 if path_i18n is None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
410 path_i18n = self._get_path_i_1_8_n(path)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
411 assert bool(id_) ^ bool(callback) # we must have id_ xor callback defined
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
412 if id_:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
413 menu_item = MenuItemDistant(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
414 self.host, type_, path[-1], path_i18n[-1], id_=id_, extra=extra
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
415 )
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
417 menu_item = MenuItemLocal(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
418 type_, path[-1], path_i18n[-1], callback=callback, extra=extra
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
419 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
420 self.add_menu_item(type_, path[:-1], menu_item, path_i18n[:-1], top_extra)
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
421
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
422 def add_menus(self, menus, top_extra=None):
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
423 """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
424
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
425 @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
426 id_(unicode,callable): id of distant callback or local callback
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
427 type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
428 path(iterable[unicode]): same as in [sat.core.sat_main.SAT.import_menu]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
429 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
430 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
431 - "icon": icon name
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
432 @param top_extra: same as in [_create_categories]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
433 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
434 # TODO: manage icons
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
435 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
436 if callable(id_):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
437 self.add_menu(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
438 type_, path, path_i18n, callback=id_, extra=extra, top_extra=top_extra
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
439 )
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
440 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
441 self.add_menu(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
442 type_, path, path_i18n, id_=id_, extra=extra, top_extra=top_extra
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
443 )
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
444
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
445 def add_menu_hook(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
446 self, type_, path, path_i18n=None, extra=None, top_extra=None, callback=None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
447 ):
1366
584d45bb36d9 quick_frontends(menus): added MenuContainer.replace method + addMenuHook helper method in QuickMenusManager
Goffi <goffi@goffi.org>
parents: 1364
diff changeset
448 """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
449
584d45bb36d9 quick_frontends(menus): added MenuContainer.replace method + addMenuHook helper method in QuickMenusManager
Goffi <goffi@goffi.org>
parents: 1364
diff changeset
450 Menu hooks are local menus which override menu given by backend
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
451 @param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
452 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.import_menu]
1366
584d45bb36d9 quick_frontends(menus): added MenuContainer.replace method + addMenuHook helper method in QuickMenusManager
Goffi <goffi@goffi.org>
parents: 1364
diff changeset
453 @param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
454 @param extra(dict[unicode, unicode], None): same as in [add_menus]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
455 @param top_extra: same as in [_create_categories]
1366
584d45bb36d9 quick_frontends(menus): added MenuContainer.replace method + addMenuHook helper method in QuickMenusManager
Goffi <goffi@goffi.org>
parents: 1364
diff changeset
456 @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
457 """
584d45bb36d9 quick_frontends(menus): added MenuContainer.replace method + addMenuHook helper method in QuickMenusManager
Goffi <goffi@goffi.org>
parents: 1364
diff changeset
458 if path_i18n is None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
459 path_i18n = self._get_path_i_1_8_n(path)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
460 menu_item = MenuHook(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
461 type_, path[-1], path_i18n[-1], callback=callback, extra=extra
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
462 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
463 self.add_menu_item(type_, path[:-1], menu_item, path_i18n[:-1], top_extra)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
464 log.info("Menu hook set on {path} ({type_})".format(path=path, type_=type_))
1366
584d45bb36d9 quick_frontends(menus): added MenuContainer.replace method + addMenuHook helper method in QuickMenusManager
Goffi <goffi@goffi.org>
parents: 1364
diff changeset
465
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
466 def add_category(self, type_, path, path_i18n=None, extra=None, top_extra=None):
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
467 """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
468
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
469 @param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
470 @param path(list[unicode]): same as in [sat.core.sat_main.SAT.import_menu]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
471 @param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation of path
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
472 @param extra(dict[unicode, unicode], None): same as in [add_menus] (added on the leaf category only)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
473 @param top_extra: same as in [_create_categories]
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
474 @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
475 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
476 if path_i18n is None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
477 path_i18n = self._get_path_i_1_8_n(path)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
478 last_container = self._create_categories(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
479 type_, path, path_i18n, top_extra=top_extra
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
480 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
481 last_container.set_extra(extra)
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
482 return last_container
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
483
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
484 def get_main_container(self, type_):
1364
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
485 """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
486
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
487 @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
488 @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
489 """
28f0b33ca17c quick_frontend (menus): added a quick_menus module to manage easily menus logic:
Goffi <goffi@goffi.org>
parents:
diff changeset
490 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
491 return menu_container