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

@ -68,7 +68,7 @@ class QuickWidgetsManager(object):
for widget in widget_instances:
yield widget
def getRealClass(self, class_):
def get_real_class(self, class_):
"""Return class registered for given class_
@param class_: subclass of QuickWidget
@ -86,20 +86,20 @@ class QuickWidgetsManager(object):
)
return cls
def getWidgetInstances(self, widget):
def get_widget_instances(self, widget):
"""Get all instance of a widget
This is a helper method which call getWidgets
This is a helper method which call get_widgets
@param widget(QuickWidget): retrieve instances of this widget
@return: iterator on widgets
"""
return self.getWidgets(widget.__class__, widget.target, widget.profiles)
return self.get_widgets(widget.__class__, widget.target, widget.profiles)
def getWidgets(self, class_, target=None, profiles=None, with_duplicates=True):
def get_widgets(self, class_, target=None, profiles=None, with_duplicates=True):
"""Get all subclassed widgets instances
@param class_: subclass of QuickWidget, same parameter as used in
[getOrCreateWidget]
[get_or_create_widget]
@param target: if not None, construct a hash with this target and filter
corresponding widgets
recreated widgets are handled
@ -109,14 +109,14 @@ class QuickWidgetsManager(object):
returned
@return: iterator on widgets
"""
class_ = self.getRealClass(class_)
class_ = self.get_real_class(class_)
try:
widgets_map = self._widgets[class_.__name__]
except KeyError:
return
else:
if target is not None:
filter_hash = str(class_.getWidgetHash(target, profiles))
filter_hash = str(class_.get_widget_hash(target, profiles))
else:
filter_hash = None
if filter_hash is not None:
@ -133,11 +133,11 @@ class QuickWidgetsManager(object):
# we only return the first widget of the list
break
def getWidget(self, class_, target=None, profiles=None):
def get_widget(self, class_, target=None, profiles=None):
"""Get a widget without creating it if it doesn't exist.
if several instances of widgets with this hash exist, the first one is returned
@param class_: subclass of QuickWidget, same parameter as used in [getOrCreateWidget]
@param class_: subclass of QuickWidget, same parameter as used in [get_or_create_widget]
@param target: target depending of the widget, usually a JID instance
@param profiles (unicode, iterable[unicode], None): profile(s) to use (may or may not be
used, depending of the widget class)
@ -146,17 +146,17 @@ class QuickWidgetsManager(object):
assert (target is not None) or (profiles is not None)
if profiles is not None and isinstance(profiles, str):
profiles = [profiles]
class_ = self.getRealClass(class_)
hash_ = class_.getWidgetHash(target, profiles)
class_ = self.get_real_class(class_)
hash_ = class_.get_widget_hash(target, profiles)
try:
return self._widgets[class_.__name__][hash_][0]
except KeyError:
return None
def getOrCreateWidget(self, class_, target, *args, **kwargs):
def get_or_create_widget(self, class_, target, *args, **kwargs):
"""Get an existing widget or create a new one when necessary
If the widget is new, self.host.newWidget will be called with it.
If the widget is new, self.host.new_widget will be called with it.
@param class_(class): class of the widget to create
@param target: target depending of the widget, usually a JID instance
@param args(list): optional args to create a new instance of class_
@ -164,22 +164,22 @@ class QuickWidgetsManager(object):
if 'profile' key is present, it will be popped and put in 'profiles'
if there is neither 'profile' nor 'profiles', None will be used for 'profiles'
if 'on_new_widget' is present it can have the following values:
C.WIDGET_NEW [default]: self.host.newWidget will be called on widget creation
[callable]: this method will be called instead of self.host.newWidget
C.WIDGET_NEW [default]: self.host.new_widget will be called on widget creation
[callable]: this method will be called instead of self.host.new_widget
None: do nothing
if 'on_existing_widget' is present it can have the following values:
C.WIDGET_KEEP [default]: return the existing widget
C.WIDGET_RAISE: raise WidgetAlreadyExistsError
C.WIDGET_RECREATE: create a new widget
if the existing widget has a "recreateArgs" method, it will be called with args list and kwargs dict
if the existing widget has a "recreate_args" method, it will be called with args list and kwargs dict
so the values can be completed to create correctly the new instance
[callable]: this method will be called with existing widget as argument, the widget to use must be returned
if 'force_hash' is present, the hash given in value will be used instead of the one returned by class_.getWidgetHash
if 'force_hash' is present, the hash given in value will be used instead of the one returned by class_.get_widget_hash
other keys will be used to instanciate class_ if the case happen (e.g. if type_ is present and class_ is a QuickChat subclass,
it will be used to create a new QuickChat instance).
@return: a class_ instance, either new or already existing
"""
cls = self.getRealClass(class_)
cls = self.get_real_class(class_)
## arguments management ##
_args = [self.host, target] + list(
@ -212,7 +212,7 @@ class QuickWidgetsManager(object):
try:
hash_ = _kwargs.pop("force_hash")
except KeyError:
hash_ = cls.getWidgetHash(target, _kwargs["profiles"])
hash_ = cls.get_widget_hash(target, _kwargs["profiles"])
## widget creation or retrieval ##
@ -227,17 +227,17 @@ class QuickWidgetsManager(object):
except KeyError:
widget = None
else:
widget.addTarget(target)
widget.add_target(target)
if widget is None:
# we need to create a new widget
log.debug(f"Creating new widget for target {target} {cls}")
widget = cls(*_args, **_kwargs)
widgets_map.setdefault(hash_, []).append(widget)
self.host.callListeners("widgetNew", widget)
self.host.call_listeners("widgetNew", widget)
if on_new_widget == C.WIDGET_NEW:
self.host.newWidget(widget)
self.host.new_widget(widget)
elif callable(on_new_widget):
on_new_widget(widget)
else:
@ -250,11 +250,11 @@ class QuickWidgetsManager(object):
raise WidgetAlreadyExistsError(hash_)
elif on_existing_widget == C.WIDGET_RECREATE:
try:
recreateArgs = widget.recreateArgs
recreate_args = widget.recreate_args
except AttributeError:
pass
else:
recreateArgs(_args, _kwargs)
recreate_args(_args, _kwargs)
widget = cls(*_args, **_kwargs)
widgets_map[hash_].append(widget)
log.debug("widget <{wid}> already exists, a new one has been recreated"
@ -274,22 +274,22 @@ class QuickWidgetsManager(object):
return widget
def deleteWidget(self, widget_to_delete, *args, **kwargs):
def delete_widget(self, widget_to_delete, *args, **kwargs):
"""Delete a widget instance
this method must be called by frontends when a widget is deleted
widget's onDelete method will be called before deletion, and deletion will be
widget's on_delete method will be called before deletion, and deletion will be
stopped if it returns False.
@param widget_to_delete(QuickWidget): widget which need to deleted
@param *args: extra arguments to pass to onDelete
@param *kwargs: extra keywords arguments to pass to onDelete
@param *args: extra arguments to pass to on_delete
@param *kwargs: extra keywords arguments to pass to on_delete
the extra arguments are not used by QuickFrontend, it's is up to
the frontend to use them or not.
following extra arguments are well known:
- "all_instances" can be used as kwarg, if it evaluate to True,
all instances of the widget will be deleted (if onDelete is
all instances of the widget will be deleted (if on_delete is
not returning False for any of the instance). This arguments
is not sent to onDelete methods.
is not sent to on_delete methods.
- "explicit_close" is used when the deletion is requested by
the user or a leave signal, "all_instances" is usually set at
the same time.
@ -299,26 +299,26 @@ class QuickWidgetsManager(object):
all_instances = kwargs.get('all_instances', False)
if all_instances:
for w in self.getWidgetInstances(widget_to_delete):
if w.onDelete(**kwargs) == False:
for w in self.get_widget_instances(widget_to_delete):
if w.on_delete(**kwargs) == False:
log.debug(
f"Deletion of {widget_to_delete} cancelled by widget itself")
return
else:
if widget_to_delete.onDelete(**kwargs) == False:
if widget_to_delete.on_delete(**kwargs) == False:
log.debug(f"Deletion of {widget_to_delete} cancelled by widget itself")
return
if self.host.selected_widget == widget_to_delete:
self.host.selected_widget = None
class_ = self.getRealClass(widget_to_delete.__class__)
class_ = self.get_real_class(widget_to_delete.__class__)
try:
widgets_map = self._widgets[class_.__name__]
except KeyError:
log.error("no widgets_map found for class {cls}".format(cls=class_))
return
widget_hash = str(class_.getWidgetHash(widget_to_delete.target,
widget_hash = str(class_.get_widget_hash(widget_to_delete.target,
widget_to_delete.profiles))
try:
widget_instances = widgets_map[widget_hash]
@ -342,7 +342,7 @@ class QuickWidgetsManager(object):
del widgets_map[widget_hash]
log.debug("All instances of {cls} with hash {widget_hash!r} have been deleted"
.format(cls=class_, widget_hash=widget_hash))
self.host.callListeners("widgetDeleted", widget_to_delete)
self.host.call_listeners("widgetDeleted", widget_to_delete)
class QuickWidget(object):
@ -368,17 +368,17 @@ class QuickWidget(object):
"""
self.host = host
self.targets = set()
self.addTarget(target)
self.add_target(target)
self.profiles = set()
self._sync = True
if isinstance(profiles, str):
self.addProfile(profiles)
self.add_profile(profiles)
elif profiles is None:
if not self.PROFILES_ALLOW_NONE:
raise ValueError("profiles can't have a value of None")
else:
for profile in profiles:
self.addProfile(profile)
self.add_profile(profile)
if not self.profiles:
raise ValueError("no profile found, use None for no profile classes")
@ -402,7 +402,7 @@ class QuickWidget(object):
@property
def widget_hash(self):
"""Return quick widget hash"""
return self.getWidgetHash(self.target, self.profiles)
return self.get_widget_hash(self.target, self.profiles)
# synchronisation state
@ -429,14 +429,14 @@ class QuickWidget(object):
# target/profile
def addTarget(self, target):
def add_target(self, target):
"""Add a target if it doesn't already exists
@param target: target to add
"""
self.targets.add(target)
def addProfile(self, profile):
def add_profile(self, profile):
"""Add a profile is if doesn't already exists
@param profile: profile to add
@ -448,7 +448,7 @@ class QuickWidget(object):
# widget identitication
@staticmethod
def getWidgetHash(target, profiles):
def get_widget_hash(target, profiles):
"""Return the hash associated with this target for this widget class
some widget classes can manage several target on the same instance
@ -465,7 +465,7 @@ class QuickWidget(object):
# widget life events
def onDelete(self, *args, **kwargs):
def on_delete(self, *args, **kwargs):
"""Called when a widget is being deleted
@return (boot, None): False to cancel deletion
@ -473,6 +473,6 @@ class QuickWidget(object):
"""
return True
def onSelected(self):
def on_selected(self):
"""Called when host.selected_widget is this instance"""
pass