code style reformatting using black

This commit is contained in:
Goffi 2018-06-27 20:14:46 +02:00
commit 744cb0c503

View file

@ -19,12 +19,16 @@
try:
# FIXME: to be removed when an acceptable solution is here
unicode('') # XXX: unicode doesn't exist in pyjamas
except (TypeError, AttributeError): # Error raised is not the same depending on pyjsbuild options
unicode("") # XXX: unicode doesn't exist in pyjamas
except (
TypeError,
AttributeError,
): # Error raised is not the same depending on pyjsbuild options
unicode = str
from sat.core.log import getLogger
from sat.core.i18n import _, languageSwitch
log = getLogger(__name__)
from sat_frontends.quick_frontend.constants import Const as C
from collections import OrderedDict
@ -32,8 +36,9 @@ from collections import OrderedDict
## items ##
class MenuBase(object):
ACTIVE=True
ACTIVE = True
def __init__(self, name, extra=None):
"""
@ -61,7 +66,8 @@ class MenuBase(object):
class MenuItem(MenuBase):
"""A callable item in the menu"""
CALLABLE=False
CALLABLE = False
def __init__(self, name, name_i18n, extra=None, type_=None):
"""
@ -83,7 +89,7 @@ class MenuItem(MenuBase):
@param caller: Menu caller
"""
assert self.type is not None # if data collector are used, type must be set
assert self.type is not None # if data collector are used, type must be set
data_collector = QuickMenusManager.getDataCollector(self.type)
if data_collector is None:
@ -101,7 +107,6 @@ class MenuItem(MenuBase):
data[data_key] = unicode(getattr(caller, caller_attr))
return data
def call(self, caller, profile=C.PROF_KEY_NONE):
"""Execute the menu item
@ -113,7 +118,8 @@ class MenuItem(MenuBase):
class MenuItemDistant(MenuItem):
"""A MenuItem with a distant callback"""
CALLABLE=True
CALLABLE = True
def __init__(self, host, type_, name, name_i18n, id_, extra=None):
"""
@ -129,14 +135,15 @@ class MenuItemDistant(MenuItem):
self.id = id_
def call(self, caller, profile=C.PROF_KEY_NONE):
data = self.collectData(caller)
data = self.collectData(caller)
log.debug("data collected: %s" % data)
self.host.launchAction(self.id, data, profile=profile)
class MenuItemLocal(MenuItem):
"""A MenuItem with a local callback"""
CALLABLE=True
CALLABLE = True
def __init__(self, type_, name, name_i18n, callback, extra=None):
"""
@ -162,12 +169,14 @@ class MenuItemLocal(MenuItem):
class MenuHook(MenuItemLocal):
"""A MenuItem which replace an expected item from backend"""
pass
class MenuPlaceHolder(MenuItem):
"""A non existant menu which is used to keep a position"""
ACTIVE=False
ACTIVE = False
def __init__(self, name):
MenuItem.__init__(self, name, name)
@ -175,10 +184,11 @@ class MenuPlaceHolder(MenuItem):
class MenuSeparator(MenuItem):
"""A separation between items/categories"""
SEP_IDX=0
SEP_IDX = 0
def __init__(self):
MenuSeparator.SEP_IDX +=1
MenuSeparator.SEP_IDX += 1
name = u"___separator_{}".format(MenuSeparator.SEP_IDX)
MenuItem.__init__(self, name, name)
@ -187,7 +197,6 @@ class MenuSeparator(MenuItem):
class MenuContainer(MenuBase):
def __init__(self, name, extra=None):
MenuBase.__init__(self, name, extra)
self._items = OrderedDict()
@ -208,7 +217,10 @@ class MenuContainer(MenuBase):
raise KeyError(item)
def getOrCreate(self, item):
log.debug(u"MenuContainer getOrCreate: item=%s name=%s\nlist=%s" % (item, item.canonical, self._items.keys()))
log.debug(
u"MenuContainer getOrCreate: item=%s name=%s\nlist=%s"
% (item, item.canonical, self._items.keys())
)
try:
return self[item]
except KeyError:
@ -255,6 +267,7 @@ class MenuCategory(MenuContainer):
class MenuType(MenuContainer):
"""A type which can hold other menus or categories"""
pass
@ -263,7 +276,10 @@ class MenuType(MenuContainer):
class QuickMenusManager(object):
"""Manage all the menus"""
_data_collectors={C.MENU_GLOBAL: None} # No data is associated with C.MENU_GLOBAL items
_data_collectors = {
C.MENU_GLOBAL: None
} # No data is associated with C.MENU_GLOBAL items
def __init__(self, host, menus=None, language=None):
"""
@ -358,15 +374,28 @@ class QuickMenusManager(object):
menu_container.replace(item)
elif isinstance(container_item, MenuHook):
# MenuHook must not be replaced
log.debug(u"ignoring menu at path [{}] because a hook is already in place".format(path))
log.debug(
u"ignoring menu at path [{}] because a hook is already in place".format(
path
)
)
else:
log.error(u"Conflicting menus at path [{}]".format(path))
else:
log.debug(u"Adding menu [{type_}] {path}".format(type_=type_, path=path))
menu_container.append(item)
self.host.callListeners('menu', type_, path, path_i18n, item)
self.host.callListeners("menu", type_, path, path_i18n, item)
def addMenu(self, type_, path, path_i18n=None, extra=None, top_extra=None, id_=None, callback=None):
def addMenu(
self,
type_,
path,
path_i18n=None,
extra=None,
top_extra=None,
id_=None,
callback=None,
):
"""Add a menu item
@param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]
@ -379,11 +408,15 @@ class QuickMenusManager(object):
"""
if path_i18n is None:
path_i18n = self._getPathI18n(path)
assert bool(id_) ^ bool(callback) # we must have id_ xor callback defined
assert bool(id_) ^ bool(callback) # we must have id_ xor callback defined
if id_:
menu_item = MenuItemDistant(self.host, type_, path[-1], path_i18n[-1], id_=id_, extra=extra)
menu_item = MenuItemDistant(
self.host, type_, path[-1], path_i18n[-1], id_=id_, extra=extra
)
else:
menu_item = MenuItemLocal(type_, path[-1], path_i18n[-1], callback=callback, extra=extra)
menu_item = MenuItemLocal(
type_, path[-1], path_i18n[-1], callback=callback, extra=extra
)
self.addMenuItem(type_, path[:-1], menu_item, path_i18n[:-1], top_extra)
def addMenus(self, menus, top_extra=None):
@ -401,11 +434,17 @@ class QuickMenusManager(object):
# TODO: manage icons
for id_, type_, path, path_i18n, extra in menus:
if callable(id_):
self.addMenu(type_, path, path_i18n, callback=id_, extra=extra, top_extra=top_extra)
self.addMenu(
type_, path, path_i18n, callback=id_, extra=extra, top_extra=top_extra
)
else:
self.addMenu(type_, path, path_i18n, id_=id_, extra=extra, top_extra=top_extra)
self.addMenu(
type_, path, path_i18n, id_=id_, extra=extra, top_extra=top_extra
)
def addMenuHook(self, type_, path, path_i18n=None, extra=None, top_extra=None, callback=None):
def addMenuHook(
self, type_, path, path_i18n=None, extra=None, top_extra=None, callback=None
):
"""Helper method to add a menu hook
Menu hooks are local menus which override menu given by backend
@ -418,7 +457,9 @@ class QuickMenusManager(object):
"""
if path_i18n is None:
path_i18n = self._getPathI18n(path)
menu_item = MenuHook(type_, path[-1], path_i18n[-1], callback=callback, extra=extra)
menu_item = MenuHook(
type_, path[-1], path_i18n[-1], callback=callback, extra=extra
)
self.addMenuItem(type_, path[:-1], menu_item, path_i18n[:-1], top_extra)
log.info(u"Menu hook set on {path} ({type_})".format(path=path, type_=type_))
@ -434,7 +475,9 @@ class QuickMenusManager(object):
"""
if path_i18n is None:
path_i18n = self._getPathI18n(path)
last_container = self._createCategories(type_, path, path_i18n, top_extra=top_extra)
last_container = self._createCategories(
type_, path, path_i18n, top_extra=top_extra
)
last_container.setExtra(extra)
return last_container