diff sat/plugins/plugin_xep_0085.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 6cf4bd6972c2
children c23cad65ae99
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0085.py	Fri Apr 07 15:18:39 2023 +0200
+++ b/sat/plugins/plugin_xep_0085.py	Sat Apr 08 13:54:42 2023 +0200
@@ -103,29 +103,29 @@
         self.map = {}  # FIXME: would be better to use client instead of mapping profile to data
 
         # parameter value is retrieved before each use
-        host.memory.updateParams(self.params)
+        host.memory.update_params(self.params)
 
         # triggers from core
-        host.trigger.add("messageReceived", self.messageReceivedTrigger)
-        host.trigger.add("sendMessage", self.sendMessageTrigger)
-        host.trigger.add("paramUpdateTrigger", self.paramUpdateTrigger)
+        host.trigger.add("messageReceived", self.message_received_trigger)
+        host.trigger.add("sendMessage", self.send_message_trigger)
+        host.trigger.add("param_update_trigger", self.param_update_trigger)
 
         # args: to_s (jid as string), profile
-        host.bridge.addMethod(
-            "chatStateComposing",
+        host.bridge.add_method(
+            "chat_state_composing",
             ".plugin",
             in_sign="ss",
             out_sign="",
-            method=self.chatStateComposing,
+            method=self.chat_state_composing,
         )
 
         # args: from (jid as string), state in CHAT_STATES, profile
-        host.bridge.addSignal("chatStateReceived", ".plugin", signature="sss")
+        host.bridge.add_signal("chat_state_received", ".plugin", signature="sss")
 
-    def getHandler(self, client):
+    def get_handler(self, client):
         return XEP_0085_handler(self, client.profile)
 
-    def profileDisconnected(self, client):
+    def profile_disconnected(self, client):
         """Eventually send a 'gone' state to all one2one contacts."""
         profile = client.profile
         if profile not in self.map:
@@ -137,7 +137,7 @@
             self.map[profile][to_jid]._onEvent("gone")
         del self.map[profile]
 
-    def updateCache(self, entity_jid, value, profile):
+    def update_cache(self, entity_jid, value, profile):
         """Update the entity data of the given profile for one or all contacts.
         Reset the chat state(s) display if the notification has been disabled.
 
@@ -145,18 +145,18 @@
         @param value: True, False or DELETE_VALUE to delete the entity data
         @param profile: current profile
         """
-        client = self.host.getClient(profile)
+        client = self.host.get_client(profile)
         if value == DELETE_VALUE:
-            self.host.memory.delEntityDatum(client, entity_jid, ENTITY_KEY)
+            self.host.memory.del_entity_datum(client, entity_jid, ENTITY_KEY)
         else:
-            self.host.memory.updateEntityData(
+            self.host.memory.update_entity_data(
                 client, entity_jid, ENTITY_KEY, value
             )
         if not value or value == DELETE_VALUE:
             # reinit chat state UI for this or these contact(s)
-            self.host.bridge.chatStateReceived(entity_jid.full(), "", profile)
+            self.host.bridge.chat_state_received(entity_jid.full(), "", profile)
 
-    def paramUpdateTrigger(self, name, value, category, type_, profile):
+    def param_update_trigger(self, name, value, category, type_, profile):
         """Reset all the existing chat state entity data associated with this profile after a parameter modification.
 
         @param name: parameter name
@@ -165,23 +165,23 @@
         @param type_: parameter type
         """
         if (category, name) == (PARAM_KEY, PARAM_NAME):
-            self.updateCache(
+            self.update_cache(
                 C.ENTITY_ALL, True if C.bool(value) else DELETE_VALUE, profile=profile
             )
             return False
         return True
 
-    def messageReceivedTrigger(self, client, message, post_treat):
+    def message_received_trigger(self, client, message, post_treat):
         """
         Update the entity cache when we receive a message with body.
         Check for a chat state in the message and signal frontends.
         """
         profile = client.profile
-        if not self.host.memory.getParamA(PARAM_NAME, PARAM_KEY, profile_key=profile):
+        if not self.host.memory.param_get_a(PARAM_NAME, PARAM_KEY, profile_key=profile):
             return True
 
         from_jid = JID(message.getAttribute("from"))
-        if self._isMUC(from_jid, profile):
+        if self._is_muc(from_jid, profile):
             from_jid = from_jid.userhostJID()
         else:  # update entity data for one2one chat
             # assert from_jid.resource # FIXME: assert doesn't work on normal message from server (e.g. server announce), because there is no resource
@@ -190,17 +190,17 @@
                 try:
                     next(domish.generateElementsNamed(message.elements(), name="active"))
                     # contact enabled Chat State Notifications
-                    self.updateCache(from_jid, True, profile=profile)
+                    self.update_cache(from_jid, True, profile=profile)
                 except StopIteration:
                     if message.getAttribute("type") == "chat":
                         # contact didn't enable Chat State Notifications
-                        self.updateCache(from_jid, False, profile=profile)
+                        self.update_cache(from_jid, False, profile=profile)
                         return True
             except StopIteration:
                 pass
 
         # send our next "composing" states to any MUC and to the contacts who enabled the feature
-        self._chatStateInit(from_jid, message.getAttribute("type"), profile)
+        self._chat_state_init(from_jid, message.getAttribute("type"), profile)
 
         state_list = [
             child.name
@@ -212,13 +212,13 @@
         for state in state_list:
             # there must be only one state according to the XEP
             if state != "gone" or message.getAttribute("type") != "groupchat":
-                self.host.bridge.chatStateReceived(
+                self.host.bridge.chat_state_received(
                     message.getAttribute("from"), state, profile
                 )
             break
         return True
 
-    def sendMessageTrigger(
+    def send_message_trigger(
         self, client, mess_data, pre_xml_treatments, post_xml_treatments
     ):
         """
@@ -230,16 +230,16 @@
         def treatment(mess_data):
             message = mess_data["xml"]
             to_jid = JID(message.getAttribute("to"))
-            if not self._checkActivation(to_jid, forceEntityData=True, profile=profile):
+            if not self._check_activation(to_jid, forceEntityData=True, profile=profile):
                 return mess_data
             try:
                 # message with a body always mean active state
                 next(domish.generateElementsNamed(message.elements(), name="body"))
                 message.addElement("active", NS_CHAT_STATES)
                 # launch the chat state machine (init the timer)
-                if self._isMUC(to_jid, profile):
+                if self._is_muc(to_jid, profile):
                     to_jid = to_jid.userhostJID()
-                self._chatStateActive(to_jid, mess_data["type"], profile)
+                self._chat_state_active(to_jid, mess_data["type"], profile)
             except StopIteration:
                 if "chat_state" in mess_data["extra"]:
                     state = mess_data["extra"].pop("chat_state")
@@ -250,16 +250,16 @@
         post_xml_treatments.addCallback(treatment)
         return True
 
-    def _isMUC(self, to_jid, profile):
+    def _is_muc(self, to_jid, profile):
         """Tell if that JID is a MUC or not
 
         @param to_jid (JID): full or bare JID to check
         @param profile (str): %(doc_profile)s
         @return: bool
         """
-        client = self.host.getClient(profile)
+        client = self.host.get_client(profile)
         try:
-            type_ = self.host.memory.getEntityDatum(
+            type_ = self.host.memory.get_entity_datum(
                 client, to_jid.userhostJID(), C.ENTITY_TYPE)
             if type_ == C.ENTITY_TYPE_MUC:
                 return True
@@ -267,7 +267,7 @@
             pass
         return False
 
-    def _checkActivation(self, to_jid, forceEntityData, profile):
+    def _check_activation(self, to_jid, forceEntityData, profile):
         """
         @param to_jid: the contact's full JID (or bare if you know it's a MUC)
         @param forceEntityData: if set to True, a non-existing
@@ -275,26 +275,26 @@
         @param: current profile
         @return: True if the notifications should be sent to this JID.
         """
-        client = self.host.getClient(profile)
+        client = self.host.get_client(profile)
         # check if the parameter is active
-        if not self.host.memory.getParamA(PARAM_NAME, PARAM_KEY, profile_key=profile):
+        if not self.host.memory.param_get_a(PARAM_NAME, PARAM_KEY, profile_key=profile):
             return False
         # check if notifications should be sent to this contact
-        if self._isMUC(to_jid, profile):
+        if self._is_muc(to_jid, profile):
             return True
         # FIXME: this assertion crash when we want to send a message to an online bare jid
-        # assert to_jid.resource or not self.host.memory.isEntityAvailable(to_jid, profile) # must either have a resource, or talk to an offline contact
+        # assert to_jid.resource or not self.host.memory.is_entity_available(to_jid, profile) # must either have a resource, or talk to an offline contact
         try:
-            return self.host.memory.getEntityDatum(client, to_jid, ENTITY_KEY)
+            return self.host.memory.get_entity_datum(client, to_jid, ENTITY_KEY)
         except (exceptions.UnknownEntityError, KeyError):
             if forceEntityData:
                 # enable it for the first time
-                self.updateCache(to_jid, True, profile=profile)
+                self.update_cache(to_jid, True, profile=profile)
                 return True
         # wait for the first message before sending states
         return False
 
-    def _chatStateInit(self, to_jid, mess_type, profile):
+    def _chat_state_init(self, to_jid, mess_type, profile):
         """
         Data initialization for the chat state machine.
 
@@ -309,7 +309,7 @@
             machine = ChatStateMachine(self.host, to_jid, mess_type, profile)
             self.map[profile][to_jid] = machine
 
-    def _chatStateActive(self, to_jid, mess_type, profile_key):
+    def _chat_state_active(self, to_jid, mess_type, profile_key):
         """
         Launch the chat state machine on "active" state.
 
@@ -317,13 +317,13 @@
         @param mess_type (str): "one2one" or "groupchat"
         @param profile (str): %(doc_profile)s
         """
-        profile = self.host.memory.getProfileName(profile_key)
+        profile = self.host.memory.get_profile_name(profile_key)
         if profile is None:
             raise exceptions.ProfileUnknownError
-        self._chatStateInit(to_jid, mess_type, profile)
+        self._chat_state_init(to_jid, mess_type, profile)
         self.map[profile][to_jid]._onEvent("active")
 
-    def chatStateComposing(self, to_jid_s, profile_key):
+    def chat_state_composing(self, to_jid_s, profile_key):
         """Move to the "composing" state when required.
 
         Since this method is called from the front-end, it needs to check the
@@ -334,13 +334,13 @@
         @param profile_key (str): %(doc_profile_key)s
         """
         # TODO: try to optimize this method which is called often
-        client = self.host.getClient(profile_key)
+        client = self.host.get_client(profile_key)
         to_jid = JID(to_jid_s)
-        if self._isMUC(to_jid, client.profile):
+        if self._is_muc(to_jid, client.profile):
             to_jid = to_jid.userhostJID()
         elif not to_jid.resource:
-            to_jid.resource = self.host.memory.getMainResource(client, to_jid)
-        if not self._checkActivation(
+            to_jid.resource = self.host.memory.main_resource_get(client, to_jid)
+        if not self._check_activation(
             to_jid, forceEntityData=False, profile=client.profile
         ):
             return
@@ -392,7 +392,7 @@
                         state=state, jid=self.to_jid.full()
                     )
                 )
-                client = self.host.getClient(self.profile)
+                client = self.host.get_client(self.profile)
                 mess_data = {
                     "from": client.jid,
                     "to": self.to_jid,
@@ -402,7 +402,7 @@
                     "subject": {},
                     "extra": {},
                 }
-                client.generateMessageXML(mess_data)
+                client.generate_message_xml(mess_data)
                 mess_data["xml"].addElement(state, NS_CHAT_STATES)
                 client.send(mess_data["xml"])