comparison sat_frontends/quick_frontend/quick_menus.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 003b8b4b56a7
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 try: 20 try:
21 # FIXME: to be removed when an acceptable solution is here 21 # FIXME: to be removed when an acceptable solution is here
22 unicode("") # XXX: unicode doesn't exist in pyjamas 22 str("") # XXX: unicode doesn't exist in pyjamas
23 except ( 23 except (
24 TypeError, 24 TypeError,
25 AttributeError, 25 AttributeError,
26 ): # Error raised is not the same depending on pyjsbuild options 26 ): # Error raised is not the same depending on pyjsbuild options
27 unicode = str 27 str = str
28 28
29 from sat.core.log import getLogger 29 from sat.core.log import getLogger
30 from sat.core.i18n import _, languageSwitch 30 from sat.core.i18n import _, languageSwitch
31 31
32 log = getLogger(__name__) 32 log = getLogger(__name__)
98 elif callable(data_collector): 98 elif callable(data_collector):
99 return data_collector(caller, self.name) 99 return data_collector(caller, self.name)
100 100
101 else: 101 else:
102 if caller is None: 102 if caller is None:
103 log.error(u"Caller can't be None with a dictionary as data_collector") 103 log.error("Caller can't be None with a dictionary as data_collector")
104 return {} 104 return {}
105 data = {} 105 data = {}
106 for data_key, caller_attr in data_collector.iteritems(): 106 for data_key, caller_attr in data_collector.items():
107 data[data_key] = unicode(getattr(caller, caller_attr)) 107 data[data_key] = str(getattr(caller, caller_attr))
108 return data 108 return data
109 109
110 def call(self, caller, profile=C.PROF_KEY_NONE): 110 def call(self, caller, profile=C.PROF_KEY_NONE):
111 """Execute the menu item 111 """Execute the menu item
112 112
187 187
188 SEP_IDX = 0 188 SEP_IDX = 0
189 189
190 def __init__(self): 190 def __init__(self):
191 MenuSeparator.SEP_IDX += 1 191 MenuSeparator.SEP_IDX += 1
192 name = u"___separator_{}".format(MenuSeparator.SEP_IDX) 192 name = "___separator_{}".format(MenuSeparator.SEP_IDX)
193 MenuItem.__init__(self, name, name) 193 MenuItem.__init__(self, name, name)
194 194
195 195
196 ## containers ## 196 ## containers ##
197 197
206 206
207 def __contains__(self, item): 207 def __contains__(self, item):
208 return item.canonical in self._items 208 return item.canonical in self._items
209 209
210 def __iter__(self): 210 def __iter__(self):
211 return self._items.itervalues() 211 return iter(self._items.values())
212 212
213 def __getitem__(self, item): 213 def __getitem__(self, item):
214 try: 214 try:
215 return self._items[item.canonical] 215 return self._items[item.canonical]
216 except KeyError: 216 except KeyError:
217 raise KeyError(item) 217 raise KeyError(item)
218 218
219 def getOrCreate(self, item): 219 def getOrCreate(self, item):
220 log.debug( 220 log.debug(
221 u"MenuContainer getOrCreate: item=%s name=%s\nlist=%s" 221 "MenuContainer getOrCreate: item=%s name=%s\nlist=%s"
222 % (item, item.canonical, self._items.keys()) 222 % (item, item.canonical, list(self._items.keys()))
223 ) 223 )
224 try: 224 try:
225 return self[item] 225 return self[item]
226 except KeyError: 226 except KeyError:
227 self.append(item) 227 self.append(item)
228 return item 228 return item
229 229
230 def getActiveMenus(self): 230 def getActiveMenus(self):
231 """Return an iterator on active children""" 231 """Return an iterator on active children"""
232 for child in self._items.itervalues(): 232 for child in self._items.values():
233 if child.ACTIVE: 233 if child.ACTIVE:
234 yield child 234 yield child
235 235
236 def append(self, item): 236 def append(self, item):
237 """add an item at the end of current ones 237 """add an item at the end of current ones
345 @return (callable, dict, None): data_collector 345 @return (callable, dict, None): data_collector
346 """ 346 """
347 try: 347 try:
348 return QuickMenusManager._data_collectors[type_] 348 return QuickMenusManager._data_collectors[type_]
349 except KeyError: 349 except KeyError:
350 log.error(u"No data collector registered for {}".format(type_)) 350 log.error("No data collector registered for {}".format(type_))
351 return None 351 return None
352 352
353 def addMenuItem(self, type_, path, item, path_i18n=None, top_extra=None): 353 def addMenuItem(self, type_, path, item, path_i18n=None, top_extra=None):
354 """Add a MenuItemBase instance 354 """Add a MenuItemBase instance
355 355
373 if isinstance(container_item, MenuPlaceHolder): 373 if isinstance(container_item, MenuPlaceHolder):
374 menu_container.replace(item) 374 menu_container.replace(item)
375 elif isinstance(container_item, MenuHook): 375 elif isinstance(container_item, MenuHook):
376 # MenuHook must not be replaced 376 # MenuHook must not be replaced
377 log.debug( 377 log.debug(
378 u"ignoring menu at path [{}] because a hook is already in place".format( 378 "ignoring menu at path [{}] because a hook is already in place".format(
379 path 379 path
380 ) 380 )
381 ) 381 )
382 else: 382 else:
383 log.error(u"Conflicting menus at path [{}]".format(path)) 383 log.error("Conflicting menus at path [{}]".format(path))
384 else: 384 else:
385 log.debug(u"Adding menu [{type_}] {path}".format(type_=type_, path=path)) 385 log.debug("Adding menu [{type_}] {path}".format(type_=type_, path=path))
386 menu_container.append(item) 386 menu_container.append(item)
387 self.host.callListeners("menu", type_, path, path_i18n, item) 387 self.host.callListeners("menu", type_, path, path_i18n, item)
388 388
389 def addMenu( 389 def addMenu(
390 self, 390 self,
459 path_i18n = self._getPathI18n(path) 459 path_i18n = self._getPathI18n(path)
460 menu_item = MenuHook( 460 menu_item = MenuHook(
461 type_, path[-1], path_i18n[-1], callback=callback, extra=extra 461 type_, path[-1], path_i18n[-1], callback=callback, extra=extra
462 ) 462 )
463 self.addMenuItem(type_, path[:-1], menu_item, path_i18n[:-1], top_extra) 463 self.addMenuItem(type_, path[:-1], menu_item, path_i18n[:-1], top_extra)
464 log.info(u"Menu hook set on {path} ({type_})".format(path=path, type_=type_)) 464 log.info("Menu hook set on {path} ({type_})".format(path=path, type_=type_))
465 465
466 def addCategory(self, type_, path, path_i18n=None, extra=None, top_extra=None): 466 def addCategory(self, type_, path, path_i18n=None, extra=None, top_extra=None):
467 """Create a category with all parents, and set extra on the last one 467 """Create a category with all parents, and set extra on the last one
468 468
469 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu] 469 @param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]