diff sat_frontends/quick_frontend/quick_contact_list.py @ 4037:524856bd7b19

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.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents be6d91572633
children 4b842c1fb686
line wrap: on
line diff
--- a/sat_frontends/quick_frontend/quick_contact_list.py	Fri Apr 07 15:18:39 2023 +0200
+++ b/sat_frontends/quick_frontend/quick_contact_list.py	Sat Apr 08 13:54:42 2023 +0200
@@ -83,7 +83,7 @@
 
         # options
         self.show_disconnected = False
-        self.show_empty_groups = True
+        self._show_empty_groups = True
         self.show_resources = False
         self.show_status = False
         # do we show entities with notifications?
@@ -91,45 +91,45 @@
         # (e.g. not in contact list) if they have notifications attached
         self.show_entities_with_notifs = True
 
-        self.host.bridge.asyncGetParamA(
+        self.host.bridge.param_get_a_async(
             C.SHOW_EMPTY_GROUPS,
             "General",
             profile_key=profile,
-            callback=self._showEmptyGroups,
+            callback=self._show_empty_groups_cb,
         )
 
-        self.host.bridge.asyncGetParamA(
+        self.host.bridge.param_get_a_async(
             C.SHOW_OFFLINE_CONTACTS,
             "General",
             profile_key=profile,
-            callback=self._showOfflineContacts,
+            callback=self._show_offline_contacts,
         )
 
-        self.host.addListener("presence", self.onPresenceUpdate, [self.profile])
-        self.host.addListener("nicknames", self.onNicknamesUpdate, [self.profile])
-        self.host.addListener("notification", self.onNotification, [self.profile])
-        # onNotification only updates the entity, so we can re-use it
-        self.host.addListener("notificationsClear", self.onNotification, [self.profile])
+        self.host.addListener("presence", self.on_presence_update, [self.profile])
+        self.host.addListener("nicknames", self.on_nicknames_update, [self.profile])
+        self.host.addListener("notification", self.on_notification, [self.profile])
+        # on_notification only updates the entity, so we can re-use it
+        self.host.addListener("notificationsClear", self.on_notification, [self.profile])
 
     @property
     def whoami(self):
         return self.host.profiles[self.profile].whoami
 
-    def _showEmptyGroups(self, show_str):
+    def _show_empty_groups_cb(self, show_str):
         # Called only by __init__
         # self.update is not wanted here, as it is done by
         # handler when all profiles are ready
-        self.showEmptyGroups(C.bool(show_str))
+        self.show_empty_groups(C.bool(show_str))
 
-    def _showOfflineContacts(self, show_str):
-        # same comments as for _showEmptyGroups
-        self.showOfflineContacts(C.bool(show_str))
+    def _show_offline_contacts(self, show_str):
+        # same comments as for _show_empty_groups
+        self.show_offline_contacts(C.bool(show_str))
 
     def __contains__(self, entity):
         """Check if entity is in contact list
 
         An entity can be in contact list even if not in roster
-        use isInRoster to check if entity is in roster.
+        use is_in_roster to check if entity is in roster.
         @param entity (jid.JID): jid of the entity (resource is not ignored,
             use bare jid if needed)
         """
@@ -209,10 +209,10 @@
         return {
             jid_: cache
             for jid_, cache in self._cache.items()
-            if self.entityVisible(jid_)
+            if self.entity_visible(jid_)
         }
 
-    def getItem(self, entity):
+    def get_item(self, entity):
         """Return item representation of requested entity
 
         @param entity(jid.JID): bare jid of entity
@@ -220,7 +220,7 @@
         """
         return self._cache[entity]
 
-    def _gotContacts(self, contacts):
+    def _got_contacts(self, contacts):
         """Add contacts and notice parent that contacts are filled
 
         Called during initial contact list filling
@@ -235,16 +235,16 @@
                     "Roster entities with resources are not managed, ignoring {entity}"
                     .format(entity=entity))
                 continue
-            self.host.newContactHandler(*contact, profile=self.profile)
-        handler._contactsFilled(self.profile)
+            self.host.contact_new_handler(*contact, profile=self.profile)
+        handler._contacts_filled(self.profile)
 
     def _fill(self):
         """Get all contacts from backend
 
         Contacts will be cleared before refilling them
         """
-        self.clearContacts(keep_cache=True)
-        self.host.bridge.getContacts(self.profile, callback=self._gotContacts)
+        self.clear_contacts(keep_cache=True)
+        self.host.bridge.contacts_get(self.profile, callback=self._got_contacts)
 
     def fill(self):
         handler.fill(self.profile)
@@ -285,7 +285,7 @@
             cache = self._cache[entity.bare]
         except KeyError:
             if create_if_not_found:
-                self.setContact(entity)
+                self.set_contact(entity)
                 cache = self._cache[entity.bare]
             else:
                 raise exceptions.NotFound
@@ -319,7 +319,7 @@
                 return cache[C.CONTACT_RESOURCES][entity.resource][name]
             except KeyError as e:
                 if bare_default is None:
-                    bare_default = not self.isRoom(entity.bare)
+                    bare_default = not self.is_room(entity.bare)
                 if not bare_default:
                     if default is Exception:
                         raise e
@@ -334,15 +334,15 @@
             else:
                 return default
 
-    def setCache(self, entity, name, value):
+    def set_cache(self, entity, name, value):
         """Set or update value for one data in cache
 
         @param entity(JID): entity to update
         @param name(str): value to set or update
         """
-        self.setContact(entity, attributes={name: value})
+        self.set_contact(entity, attributes={name: value})
 
-    def getFullJid(self, entity):
+    def get_full_jid(self, entity):
         """Get full jid from a bare jid
 
         @param entity(jid.JID): must be a bare jid
@@ -350,11 +350,11 @@
         @raise ValueError: the entity is not bare
         """
         if entity.resource:
-            raise ValueError("getFullJid must be used with a bare jid")
+            raise ValueError("get_full_jid must be used with a bare jid")
         main_resource = self.getCache(entity, C.CONTACT_MAIN_RESOURCE)
         return jid.JID("{}/{}".format(entity, main_resource))
 
-    def setGroupData(self, group, name, value):
+    def set_group_data(self, group, name, value):
         """Register a data for a group
 
         @param group: a valid (existing) group name
@@ -364,7 +364,7 @@
         assert name != "jids"
         self._groups[group][name] = value
 
-    def getGroupData(self, group, name=None):
+    def get_group_data(self, group, name=None):
         """Return value associated to group data
 
         @param group: a valid (existing) group name
@@ -375,7 +375,7 @@
             return self._groups[group]
         return self._groups[group][name]
 
-    def isInRoster(self, entity):
+    def is_in_roster(self, entity):
         """Tell if an entity is in roster
 
         @param entity(jid.JID): jid of the entity
@@ -383,7 +383,7 @@
         """
         return entity.bare in self._roster
 
-    def isRoom(self, entity):
+    def is_room(self, entity):
         """Helper method to know if entity is a MUC room
 
         @param entity(jid.JID): jid of the entity
@@ -391,9 +391,9 @@
         @return (bool): True if entity is a room
         """
         assert entity.resource is None  # FIXME: this may change when MIX will be handled
-        return self.isSpecial(entity, C.CONTACT_SPECIAL_GROUP)
+        return self.is_special(entity, C.CONTACT_SPECIAL_GROUP)
 
-    def isSpecial(self, entity, special_type):
+    def is_special(self, entity, special_type):
         """Tell if an entity is of a specialy _type
 
         @param entity(jid.JID): jid of the special entity
@@ -403,7 +403,7 @@
         """
         return self.getCache(entity, C.CONTACT_SPECIAL, default=None) == special_type
 
-    def setSpecial(self, entity, special_type):
+    def set_special(self, entity, special_type):
         """Set special flag on an entity
 
         @param entity(jid.JID): jid of the special entity
@@ -412,9 +412,9 @@
             or None to remove special flag
         """
         assert special_type in C.CONTACT_SPECIAL_ALLOWED + (None,)
-        self.setCache(entity, C.CONTACT_SPECIAL, special_type)
+        self.set_cache(entity, C.CONTACT_SPECIAL, special_type)
 
-    def getSpecials(self, special_type=None, bare=False):
+    def get_specials(self, special_type=None, bare=False):
         """Return all the bare JIDs of the special roster entities of with given type.
 
         @param special_type(unicode, None): if not None, filter by special type
@@ -434,9 +434,9 @@
 
     def disconnect(self):
         # for now we just clear contacts on disconnect
-        self.clearContacts()
+        self.clear_contacts()
 
-    def clearContacts(self, keep_cache=False):
+    def clear_contacts(self, keep_cache=False):
         """Clear all the contact list
 
         @param keep_cache: if True, don't reset the cache
@@ -449,7 +449,7 @@
         self._roster.clear()
         self.update()
 
-    def setContact(self, entity, groups=None, attributes=None, in_roster=False):
+    def set_contact(self, entity, groups=None, attributes=None, in_roster=False):
         """Add a contact to the list if it doesn't exist, else update it.
 
         This method can be called with groups=None for the purpose of updating
@@ -473,7 +473,7 @@
         # we check if the entity is visible before changing anything
         # this way we know if we need to do an UPDATE_ADD, UPDATE_MODIFY
         # or an UPDATE_DELETE
-        was_visible = self.entityVisible(entity_bare)
+        was_visible = self.entity_visible(entity_bare)
 
         if in_roster:
             self._roster.add(entity_bare)
@@ -526,7 +526,7 @@
             else cache
         )
         for attribute, value in attributes.items():
-            if attribute == "nicknames" and self.isSpecial(
+            if attribute == "nicknames" and self.is_special(
                 entity, C.CONTACT_SPECIAL_GROUP
             ):
                 # we don't want to keep nicknames for MUC rooms
@@ -540,7 +540,7 @@
             cache_attr[attribute] = value
 
         # we can update the display if needed
-        if self.entityVisible(entity_bare):
+        if self.entity_visible(entity_bare):
             # if the contact was not visible, we need to add a widget
             # else we just update id
             update_type = C.UPDATE_MODIFY if was_visible else C.UPDATE_ADD
@@ -549,7 +549,7 @@
             # the entity was visible and is not anymore, we remove it
             self.update([entity], C.UPDATE_DELETE, self.profile)
 
-    def entityVisible(self, entity, check_resource=False):
+    def entity_visible(self, entity, check_resource=False):
         """Tell if the contact should be showed or hidden.
 
         @param entity (jid.JID): jid of the contact
@@ -571,12 +571,12 @@
             or entity in selected
             or (
                 self.show_entities_with_notifs
-                and next(self.host.getNotifs(entity.bare, profile=self.profile), None)
+                and next(self.host.get_notifs(entity.bare, profile=self.profile), None)
             )
-            or entity.resource is None and self.isRoom(entity.bare)
+            or entity.resource is None and self.is_room(entity.bare)
         )
 
-    def anyEntityVisible(self, entities, check_resources=False):
+    def any_entity_visible(self, entities, check_resources=False):
         """Tell if in a list of entities, at least one should be shown
 
         @param entities (list[jid.JID]): list of jids
@@ -585,26 +585,26 @@
         """
         # FIXME: looks inefficient, really needed?
         for entity in entities:
-            if self.entityVisible(entity, check_resources):
+            if self.entity_visible(entity, check_resources):
                 return True
         return False
 
-    def isEntityInGroup(self, entity, group):
+    def is_entity_in_group(self, entity, group):
         """Tell if an entity is in a roster group
 
         @param entity(jid.JID): jid of the entity
         @param group(unicode): group to check
         @return (bool): True if the entity is in the group
         """
-        return entity in self.getGroupData(group, "jids")
+        return entity in self.get_group_data(group, "jids")
 
-    def removeContact(self, entity):
+    def remove_contact(self, entity):
         """remove a contact from the list
 
         @param entity(jid.JID): jid of the entity to remove (bare jid is used)
         """
         entity_bare = entity.bare
-        was_visible = self.entityVisible(entity_bare)
+        was_visible = self.entity_visible(entity_bare)
         try:
             groups = self._cache[entity_bare].get(C.CONTACT_GROUPS, set())
         except KeyError:
@@ -629,7 +629,7 @@
         if was_visible:
             self.update([entity], C.UPDATE_DELETE, self.profile)
 
-    def onPresenceUpdate(self, entity, show, priority, statuses, profile):
+    def on_presence_update(self, entity, show, priority, statuses, profile):
         """Update entity's presence status
 
         @param entity(jid.JID): entity updated
@@ -638,9 +638,9 @@
         @param statuses: dict of statuses
         @param profile: %(doc_profile)s
         """
-        # FIXME: cache modification should be done with setContact
+        # FIXME: cache modification should be done with set_contact
         #        the resources/presence handling logic should be moved there
-        was_visible = self.entityVisible(entity.bare)
+        was_visible = self.entity_visible(entity.bare)
         cache = self.getCache(entity, create_if_not_found=True)
         if show == C.PRESENCE_UNAVAILABLE:
             if not entity.resource:
@@ -680,13 +680,13 @@
                     ),
                 )
                 cache[C.CONTACT_MAIN_RESOURCE] = priority_resource
-        if self.entityVisible(entity.bare):
+        if self.entity_visible(entity.bare):
             update_type = C.UPDATE_MODIFY if was_visible else C.UPDATE_ADD
             self.update([entity], update_type, self.profile)
         elif was_visible:
             self.update([entity], C.UPDATE_DELETE, self.profile)
 
-    def onNicknamesUpdate(self, entity, nicknames, profile):
+    def on_nicknames_update(self, entity, nicknames, profile):
         """Update entity's nicknames
 
         @param entity(jid.JID): entity updated
@@ -694,9 +694,9 @@
         @param profile: %(doc_profile)s
         """
         assert profile == self.profile
-        self.setCache(entity, "nicknames", nicknames)
+        self.set_cache(entity, "nicknames", nicknames)
 
-    def onNotification(self, entity, notif, profile):
+    def on_notification(self, entity, notif, profile):
         """Update entity with notification
 
         @param entity(jid.JID): entity updated
@@ -704,7 +704,7 @@
         @param profile: %(doc_profile)s
         """
         assert profile == self.profile
-        if entity is not None and self.entityVisible(entity):
+        if entity is not None and self.entity_visible(entity):
             self.update([entity], C.UPDATE_MODIFY, profile)
 
     def unselect(self, entity):
@@ -747,7 +747,7 @@
                 self._selected.add(entity)
                 self.update([entity], C.UPDATE_SELECTION, profile=self.profile)
 
-    def showOfflineContacts(self, show):
+    def show_offline_contacts(self, show):
         """Tell if offline contacts should be shown
 
         @param show(bool): True if offline contacts should be shown
@@ -758,14 +758,14 @@
         self.show_disconnected = show
         self.update(type_=C.UPDATE_STRUCTURE, profile=self.profile)
 
-    def showEmptyGroups(self, show):
+    def show_empty_groups(self, show):
         assert isinstance(show, bool)
-        if self.show_empty_groups == show:
+        if self._show_empty_groups == show:
             return
-        self.show_empty_groups = show
+        self._show_empty_groups = show
         self.update(type_=C.UPDATE_STRUCTURE, profile=self.profile)
 
-    def showResources(self, show):
+    def show_resources(self, show):
         assert isinstance(show, bool)
         if self.show_resources == show:
             return
@@ -773,10 +773,10 @@
         self.update(type_=C.UPDATE_STRUCTURE, profile=self.profile)
 
     def plug(self):
-        handler.addProfile(self.profile)
+        handler.add_profile(self.profile)
 
     def unplug(self):
-        handler.removeProfile(self.profile)
+        handler.remove_profile(self.profile)
 
     def update(self, entities=None, type_=None, profile=None):
         handler.update(entities, type_, profile)
@@ -930,7 +930,7 @@
         """
         self._widgets.remove(widget)
 
-    def addProfiles(self, profiles):
+    def add_profiles(self, profiles):
         """Add a contact list for plugged profiles
 
         @param profile(iterable[unicode]): plugged profiles
@@ -940,10 +940,10 @@
                 self._clist[profile] = ProfileContactList(profile)
         return [self._clist[profile] for profile in profiles]
 
-    def addProfile(self, profile):
-        return self.addProfiles([profile])[0]
+    def add_profile(self, profile):
+        return self.add_profiles([profile])[0]
 
-    def removeProfiles(self, profiles):
+    def remove_profiles(self, profiles):
         """Remove given unplugged profiles from contact list
 
         @param profile(iterable[unicode]): unplugged profiles
@@ -951,10 +951,10 @@
         for profile in profiles:
             del self._clist[profile]
 
-    def removeProfile(self, profile):
-        self.removeProfiles([profile])
+    def remove_profile(self, profile):
+        self.remove_profiles([profile])
 
-    def getSpecialExtras(self, special_type=None):
+    def get_special_extras(self, special_type=None):
         """Return special extras with given type
 
         If special_type is None, return all special extras.
@@ -966,16 +966,16 @@
         """
         entities = set()
         for contact_list in self._clist.values():
-            entities.update(contact_list.getSpecialExtras(special_type))
+            entities.update(contact_list.get_special_extras(special_type))
         return entities
 
-    def _contactsFilled(self, profile):
+    def _contacts_filled(self, profile):
         self._to_fill.remove(profile)
         if not self._to_fill:
             del self._to_fill
             # we need a full update when all contacts are filled
             self.update()
-        self.host.callListeners("contactsFilled", profile=profile)
+        self.host.call_listeners("contactsFilled", profile=profile)
 
     def fill(self, profile=None):
         """Get all contacts from backend, and fill the widget
@@ -1009,13 +1009,13 @@
         for profile in remaining:
             self._clist[profile]._fill()
 
-    def clearContacts(self, keep_cache=False):
+    def clear_contacts(self, keep_cache=False):
         """Clear all the contact list
 
         @param keep_cache: if True, don't reset the cache
         """
         for contact_list in self._clist.values():
-            contact_list.clearContacts(keep_cache)
+            contact_list.clear_contacts(keep_cache)
         # we need a full update
         self.update()
 
@@ -1027,7 +1027,7 @@
         for contact_list in self._clist.values():
             contact_list.select(entity)
 
-    def lockUpdate(self, locked=True, do_update=True):
+    def lock_update(self, locked=True, do_update=True):
         """Forbid contact list updates
 
         Used mainly while profiles are plugged, as many updates can occurs, causing
@@ -1067,11 +1067,11 @@
         # for next values, None means use indivual value per profile
         # True or False mean override these values for all profiles
         self.show_disconnected = None  # TODO
-        self.show_empty_groups = None  # TODO
+        self._show_empty_groups = None  # TODO
         self.show_resources = None  # TODO
         self.show_status = None  # TODO
 
-    def postInit(self):
+    def post_init(self):
         """Method to be called by frontend after widget is initialised"""
         handler.register(self)
 
@@ -1108,6 +1108,6 @@
         """
         raise NotImplementedError
 
-    def onDelete(self):
-        QuickWidget.onDelete(self)
+    def on_delete(self):
+        QuickWidget.on_delete(self)
         handler.unregister(self)