massive refactoring to switch from camelCase to snake_case:

historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.

However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.

To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.

This is a massive change, and may result in some bugs.
This commit is contained in:
Goffi 2023-04-08 13:54:42 +02:00
commit 6a6f15707a

View file

@ -27,7 +27,7 @@ except (
str = str
from sat.core.log import getLogger
from sat.core.i18n import _, languageSwitch
from sat.core.i18n import _, language_switch
log = getLogger(__name__)
from sat_frontends.quick_frontend.constants import Const as C
@ -43,10 +43,10 @@ class MenuBase(object):
def __init__(self, name, extra=None):
"""
@param name(unicode): canonical name of the item
@param extra(dict[unicode, unicode], None): same as in [addMenus]
@param extra(dict[unicode, unicode], None): same as in [add_menus]
"""
self._name = name
self.setExtra(extra)
self.set_extra(extra)
@property
def canonical(self):
@ -58,7 +58,7 @@ class MenuBase(object):
"""Return the name of the container, can be translated"""
return self._name
def setExtra(self, extra):
def set_extra(self, extra):
if extra is None:
extra = {}
self.icon = extra.get("icon")
@ -73,8 +73,8 @@ class MenuItem(MenuBase):
"""
@param name(unicode): canonical name of the item
@param name_i18n(unicode): translated name of the item
@param extra(dict[unicode, unicode], None): same as in [addMenus]
@param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]
@param extra(dict[unicode, unicode], None): same as in [add_menus]
@param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
"""
MenuBase.__init__(self, name, extra)
self._name_i18n = name_i18n if name_i18n else name
@ -84,13 +84,13 @@ class MenuItem(MenuBase):
def name(self):
return self._name_i18n
def collectData(self, caller):
def collect_data(self, caller):
"""Get data according to data_collector
@param caller: Menu caller
"""
assert self.type is not None # if data collector are used, type must be set
data_collector = QuickMenusManager.getDataCollector(self.type)
data_collector = QuickMenusManager.get_data_collector(self.type)
if data_collector is None:
return {}
@ -124,20 +124,20 @@ class MenuItemDistant(MenuItem):
def __init__(self, host, type_, name, name_i18n, id_, extra=None):
"""
@param host: %(doc_host)s
@param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]
@param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
@param name(unicode): canonical name of the item
@param name_i18n(unicode): translated name of the item
@param id_(unicode): id of the distant callback
@param extra(dict[unicode, unicode], None): same as in [addMenus]
@param extra(dict[unicode, unicode], None): same as in [add_menus]
"""
MenuItem.__init__(self, name, name_i18n, extra, type_)
self.host = host
self.id = id_
def call(self, caller, profile=C.PROF_KEY_NONE):
data = self.collectData(caller)
data = self.collect_data(caller)
log.debug("data collected: %s" % data)
self.host.launchAction(self.id, data, profile=profile)
self.host.action_launch(self.id, data, profile=profile)
class MenuItemLocal(MenuItem):
@ -147,24 +147,24 @@ class MenuItemLocal(MenuItem):
def __init__(self, type_, name, name_i18n, callback, extra=None):
"""
@param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]
@param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
@param name(unicode): canonical name of the item
@param name_i18n(unicode): translated name of the item
@param callback(callable): local callback.
Will be called with no argument if data_collector is None
and with caller, profile, and requested data otherwise
@param extra(dict[unicode, unicode], None): same as in [addMenus]
@param extra(dict[unicode, unicode], None): same as in [add_menus]
"""
MenuItem.__init__(self, name, name_i18n, extra, type_)
self.callback = callback
def call(self, caller, profile=C.PROF_KEY_NONE):
data_collector = QuickMenusManager.getDataCollector(self.type)
data_collector = QuickMenusManager.get_data_collector(self.type)
if data_collector is None:
# FIXME: would not it be better if caller and profile where used as arguments?
self.callback()
else:
self.callback(caller, self.collectData(caller), profile)
self.callback(caller, self.collect_data(caller), profile)
class MenuHook(MenuItemLocal):
@ -216,9 +216,9 @@ class MenuContainer(MenuBase):
except KeyError:
raise KeyError(item)
def getOrCreate(self, item):
def get_or_create(self, item):
log.debug(
"MenuContainer getOrCreate: item=%s name=%s\nlist=%s"
"MenuContainer get_or_create: item=%s name=%s\nlist=%s"
% (item, item.canonical, list(self._items.keys()))
)
try:
@ -227,7 +227,7 @@ class MenuContainer(MenuBase):
self.append(item)
return item
def getActiveMenus(self):
def get_active_menus(self):
"""Return an iterator on active children"""
for child in self._items.values():
if child.ACTIVE:
@ -284,50 +284,50 @@ class QuickMenusManager(object):
def __init__(self, host, menus=None, language=None):
"""
@param host: %(doc_host)s
@param menus(iterable): menus as in [addMenus]
@param language: same as in [i18n.languageSwitch]
@param menus(iterable): menus as in [add_menus]
@param language: same as in [i18n.language_switch]
"""
self.host = host
MenuBase.host = host
self.language = language
self.menus = {}
if menus is not None:
self.addMenus(menus)
self.add_menus(menus)
def _getPathI18n(self, path):
def _get_path_i_1_8_n(self, path):
"""Return translated version of path"""
languageSwitch(self.language)
language_switch(self.language)
path_i18n = [_(elt) for elt in path]
languageSwitch()
language_switch()
return path_i18n
def _createCategories(self, type_, path, path_i18n=None, top_extra=None):
def _create_categories(self, type_, path, path_i18n=None, top_extra=None):
"""Create catogories of the path
@param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]
@param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu]
@param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
@param path(list[unicode]): same as in [sat.core.sat_main.SAT.import_menu]
@param path_i18n(list[unicode], None): translated menu path (same lenght as path) or None to get deferred translation of path
@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).
@return (MenuContainer): last category created, or MenuType if path is empty
"""
if path_i18n is None:
path_i18n = self._getPathI18n(path)
path_i18n = self._get_path_i_1_8_n(path)
assert len(path) == len(path_i18n)
menu_container = self.menus.setdefault(type_, MenuType(type_))
for idx, category in enumerate(path):
menu_category = MenuCategory(category, path_i18n[idx], extra=top_extra)
menu_container = menu_container.getOrCreate(menu_category)
menu_container = menu_container.get_or_create(menu_category)
top_extra = None
return menu_container
@staticmethod
def addDataCollector(type_, data_collector):
def add_data_collector(type_, data_collector):
"""Associate a data collector to a menu type
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.
@param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]
@param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
@param data_collector(dict[unicode,unicode], callable, None): can be:
- a dict which map data name to local name.
The attribute named after the dict values will be getted from caller, and put in data.
@ -338,10 +338,10 @@ class QuickMenusManager(object):
QuickMenusManager._data_collectors[type_] = data_collector
@staticmethod
def getDataCollector(type_):
def get_data_collector(type_):
"""Get data_collector associated to type_
@param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]
@param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
@return (callable, dict, None): data_collector
"""
try:
@ -350,20 +350,20 @@ class QuickMenusManager(object):
log.error("No data collector registered for {}".format(type_))
return None
def addMenuItem(self, type_, path, item, path_i18n=None, top_extra=None):
def add_menu_item(self, type_, path, item, path_i18n=None, top_extra=None):
"""Add a MenuItemBase instance
@param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]
@param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu], stop at the last parent category
@param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
@param path(list[unicode]): same as in [sat.core.sat_main.SAT.import_menu], stop at the last parent category
@param item(MenuItem): a instancied item
@param path_i18n(list[unicode],None): translated menu path (same lenght as path) or None to use deferred translation of path
@param top_extra: same as in [_createCategories]
@param top_extra: same as in [_create_categories]
"""
if path_i18n is None:
path_i18n = self._getPathI18n(path)
path_i18n = self._get_path_i_1_8_n(path)
assert path and len(path) == len(path_i18n)
menu_container = self._createCategories(type_, path, path_i18n, top_extra)
menu_container = self._create_categories(type_, path, path_i18n, top_extra)
if item in menu_container:
if isinstance(item, MenuHook):
@ -384,9 +384,9 @@ class QuickMenusManager(object):
else:
log.debug("Adding menu [{type_}] {path}".format(type_=type_, path=path))
menu_container.append(item)
self.host.callListeners("menu", type_, path, path_i18n, item)
self.host.call_listeners("menu", type_, path, path_i18n, item)
def addMenu(
def add_menu(
self,
type_,
path,
@ -398,16 +398,16 @@ class QuickMenusManager(object):
):
"""Add a menu item
@param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]
@param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu]
@param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
@param path(list[unicode]): same as in [sat.core.sat_main.SAT.import_menu]
@param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation
@param extra(dict[unicode, unicode], None): same as in [addMenus]
@param top_extra: same as in [_createCategories]
@param extra(dict[unicode, unicode], None): same as in [add_menus]
@param top_extra: same as in [_create_categories]
@param id_(unicode): callback id (mutually exclusive with callback)
@param callback(callable): local callback (mutually exclusive with id_)
"""
if path_i18n is None:
path_i18n = self._getPathI18n(path)
path_i18n = self._get_path_i_1_8_n(path)
assert bool(id_) ^ bool(callback) # we must have id_ xor callback defined
if id_:
menu_item = MenuItemDistant(
@ -417,71 +417,71 @@ class QuickMenusManager(object):
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)
self.add_menu_item(type_, path[:-1], menu_item, path_i18n[:-1], top_extra)
def addMenus(self, menus, top_extra=None):
def add_menus(self, menus, top_extra=None):
"""Add several menus at once
@param menus(iterable): iterable with:
id_(unicode,callable): id of distant callback or local callback
type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]
path(iterable[unicode]): same as in [sat.core.sat_main.SAT.importMenu]
type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
path(iterable[unicode]): same as in [sat.core.sat_main.SAT.import_menu]
path_i18n(iterable[unicode]): translated menu path (same lenght as path)
extra(dict[unicode,unicode]): dictionary of extra data (used on the leaf menu), can be:
- "icon": icon name
@param top_extra: same as in [_createCategories]
@param top_extra: same as in [_create_categories]
"""
# TODO: manage icons
for id_, type_, path, path_i18n, extra in menus:
if callable(id_):
self.addMenu(
self.add_menu(
type_, path, path_i18n, callback=id_, extra=extra, top_extra=top_extra
)
else:
self.addMenu(
self.add_menu(
type_, path, path_i18n, id_=id_, extra=extra, top_extra=top_extra
)
def addMenuHook(
def add_menu_hook(
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
@param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]
@param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu]
@param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
@param path(list[unicode]): same as in [sat.core.sat_main.SAT.import_menu]
@param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation
@param extra(dict[unicode, unicode], None): same as in [addMenus]
@param top_extra: same as in [_createCategories]
@param extra(dict[unicode, unicode], None): same as in [add_menus]
@param top_extra: same as in [_create_categories]
@param callback(callable): local callback (mutually exclusive with id_)
"""
if path_i18n is None:
path_i18n = self._getPathI18n(path)
path_i18n = self._get_path_i_1_8_n(path)
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)
self.add_menu_item(type_, path[:-1], menu_item, path_i18n[:-1], top_extra)
log.info("Menu hook set on {path} ({type_})".format(path=path, type_=type_))
def addCategory(self, type_, path, path_i18n=None, extra=None, top_extra=None):
def add_category(self, type_, path, path_i18n=None, extra=None, top_extra=None):
"""Create a category with all parents, and set extra on the last one
@param type_(unicode): same as in [sat.core.sat_main.SAT.importMenu]
@param path(list[unicode]): same as in [sat.core.sat_main.SAT.importMenu]
@param type_(unicode): same as in [sat.core.sat_main.SAT.import_menu]
@param path(list[unicode]): same as in [sat.core.sat_main.SAT.import_menu]
@param path_i18n(list[unicode], None): translated menu path (same lenght as path), or None to get deferred translation of path
@param extra(dict[unicode, unicode], None): same as in [addMenus] (added on the leaf category only)
@param top_extra: same as in [_createCategories]
@param extra(dict[unicode, unicode], None): same as in [add_menus] (added on the leaf category only)
@param top_extra: same as in [_create_categories]
@return (MenuCategory): last category add
"""
if path_i18n is None:
path_i18n = self._getPathI18n(path)
last_container = self._createCategories(
path_i18n = self._get_path_i_1_8_n(path)
last_container = self._create_categories(
type_, path, path_i18n, top_extra=top_extra
)
last_container.setExtra(extra)
last_container.set_extra(extra)
return last_container
def getMainContainer(self, type_):
def get_main_container(self, type_):
"""Get a main MenuType container
@param type_: a C.MENU_* constant