diff sat/plugins/plugin_xep_0353.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 6e34307319c0
children 877145b4ba01
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0353.py	Fri Apr 07 15:18:39 2023 +0200
+++ b/sat/plugins/plugin_xep_0353.py	Sat Apr 08 13:54:42 2023 +0200
@@ -52,19 +52,19 @@
     def __init__(self, host):
         log.info(_("plugin {name} initialization").format(name=PLUGIN_INFO[C.PI_NAME]))
         self.host = host
-        host.registerNamespace("jingle-message", NS_JINGLE_MESSAGE)
+        host.register_namespace("jingle-message", NS_JINGLE_MESSAGE)
         self._j = host.plugins["XEP-0166"]
-        host.trigger.add("XEP-0166_initiate", self._onInitiateTrigger)
-        host.trigger.add("messageReceived", self._onMessageReceived)
+        host.trigger.add("XEP-0166_initiate", self._on_initiate_trigger)
+        host.trigger.add("messageReceived", self._on_message_received)
 
-    def getHandler(self, client):
+    def get_handler(self, client):
         return Handler()
 
-    def profileConnecting(self, client):
+    def profile_connecting(self, client):
         # mapping from session id to deferred used to wait for destinee answer
         client._xep_0353_pending_sessions = {}
 
-    def buildMessageData(self, client, peer_jid, verb, session_id):
+    def build_message_data(self, client, peer_jid, verb, session_id):
         mess_data = {
             'from': client.jid,
             'to': peer_jid,
@@ -74,19 +74,19 @@
             'subject': {},
             'extra': {}
         }
-        client.generateMessageXML(mess_data)
+        client.generate_message_xml(mess_data)
         verb_elt = mess_data["xml"].addElement((NS_JINGLE_MESSAGE, verb))
         verb_elt["id"] = session_id
         return mess_data
 
-    async def _onInitiateTrigger(self, client, session, contents):
+    async def _on_initiate_trigger(self, client, session, contents):
         # FIXME: check that at least one resource of the peer_jid can handle the feature
         peer_jid = session['peer_jid']
         if peer_jid.resource:
             return True
 
         try:
-            infos = await self.host.memory.disco.getInfos(client, peer_jid)
+            infos = await self.host.memory.disco.get_infos(client, peer_jid)
         except error.StanzaError as e:
             if e.condition == "service-unavailable":
                 categories = {}
@@ -103,18 +103,18 @@
             # according to XEP-0353 §3.1
             await client.presence.available(peer_jid)
 
-        mess_data = self.buildMessageData(client, peer_jid, "propose", session['id'])
+        mess_data = self.build_message_data(client, peer_jid, "propose", session['id'])
         for content in contents:
-            application, app_args, app_kwargs, content_name = self._j.getContentData(
+            application, app_args, app_kwargs, content_name = self._j.get_content_data(
                 content)
             try:
-                jingleDescriptionElt = application.handler.jingleDescriptionElt
+                jingle_description_elt = application.handler.jingle_description_elt
             except AttributeError:
-                log.debug(f"no jingleDescriptionElt set for {application.handler}")
+                log.debug(f"no jingle_description_elt set for {application.handler}")
                 description_elt = domish.Element((content["app_ns"], "description"))
             else:
-                description_elt = await utils.asDeferred(
-                    jingleDescriptionElt,
+                description_elt = await utils.as_deferred(
+                    jingle_description_elt,
                     client, session, content_name, *app_args, **app_kwargs
                 )
         mess_data["xml"].propose.addChild(description_elt)
@@ -122,7 +122,7 @@
         # we wait for 2 min before cancelling the session init
         response_d.addTimeout(2*60, reactor)
         client._xep_0353_pending_sessions[session['id']] = response_d
-        await client.sendMessageData(mess_data)
+        await client.send_message_data(mess_data)
         try:
             accepting_jid = await response_d
         except defer.TimeoutError:
@@ -134,25 +134,25 @@
         del client._xep_0353_pending_sessions[session['id']]
         return True
 
-    async def _onMessageReceived(self, client, message_elt, post_treat):
+    async def _on_message_received(self, client, message_elt, post_treat):
         for elt in message_elt.elements():
             if elt.uri == NS_JINGLE_MESSAGE:
                 if elt.name == "propose":
-                    return await self._handlePropose(client, message_elt, elt)
+                    return await self._handle_propose(client, message_elt, elt)
                 elif elt.name == "retract":
-                    return self._handleRetract(client, message_elt, elt)
+                    return self._handle_retract(client, message_elt, elt)
                 elif elt.name == "proceed":
-                    return self._handleProceed(client, message_elt, elt)
+                    return self._handle_proceed(client, message_elt, elt)
                 elif elt.name == "accept":
-                    return self._handleAccept(client, message_elt, elt)
+                    return self._handle_accept(client, message_elt, elt)
                 elif elt.name == "reject":
-                    return self._handleAccept(client, message_elt, elt)
+                    return self._handle_accept(client, message_elt, elt)
                 else:
                     log.warning(f"invalid element: {elt.toXml}")
                     return True
         return True
 
-    async def _handlePropose(self, client, message_elt, elt):
+    async def _handle_propose(self, client, message_elt, elt):
         peer_jid = jid.JID(message_elt["from"])
         session_id = elt["id"]
         if peer_jid.userhostJID() not in client.roster:
@@ -176,7 +176,7 @@
                 "possibly you IP (internet localisation), do you accept?"
             ).format(peer_jid=peer_jid, human_name=human_name)
             confirm_title = D_("Invitation from an unknown contact")
-            accept = await xml_tools.deferConfirm(
+            accept = await xml_tools.defer_confirm(
                 self.host, confirm_msg, confirm_title, profile=client.profile,
                 action_extra={
                     "meta_type": C.META_TYPE_NOT_IN_ROSTER_LEAK,
@@ -185,27 +185,27 @@
                 }
             )
             if not accept:
-                mess_data = self.buildMessageData(
+                mess_data = self.build_message_data(
                     client, client.jid.userhostJID(), "reject", session_id)
-                await client.sendMessageData(mess_data)
+                await client.send_message_data(mess_data)
                 # we don't sent anything to sender, to avoid leaking presence
                 return False
             else:
                 await client.presence.available(peer_jid)
         session_id = elt["id"]
-        mess_data = self.buildMessageData(
+        mess_data = self.build_message_data(
             client, client.jid.userhostJID(), "accept", session_id)
-        await client.sendMessageData(mess_data)
-        mess_data = self.buildMessageData(
+        await client.send_message_data(mess_data)
+        mess_data = self.build_message_data(
             client, peer_jid, "proceed", session_id)
-        await client.sendMessageData(mess_data)
+        await client.send_message_data(mess_data)
         return False
 
-    def _handleRetract(self, client, message_elt, proceed_elt):
+    def _handle_retract(self, client, message_elt, proceed_elt):
         log.warning("retract is not implemented yet")
         return False
 
-    def _handleProceed(self, client, message_elt, proceed_elt):
+    def _handle_proceed(self, client, message_elt, proceed_elt):
         try:
             session_id = proceed_elt["id"]
         except KeyError:
@@ -223,10 +223,10 @@
         response_d.callback(jid.JID(message_elt["from"]))
         return False
 
-    def _handleAccept(self, client, message_elt, accept_elt):
+    def _handle_accept(self, client, message_elt, accept_elt):
         pass
 
-    def _handleReject(self, client, message_elt, accept_elt):
+    def _handle_reject(self, client, message_elt, accept_elt):
         pass