diff sat/plugins/plugin_xep_0084.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 88e332cec47b
children
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0084.py	Fri Apr 07 15:18:39 2023 +0200
+++ b/sat/plugins/plugin_xep_0084.py	Sat Apr 08 13:54:42 2023 +0200
@@ -61,39 +61,39 @@
 
     def __init__(self, host):
         log.info(_("XEP-0084 (User Avatar) plugin initialization"))
-        host.registerNamespace("avatar_metadata", NS_AVATAR_METADATA)
-        host.registerNamespace("avatar_data", NS_AVATAR_DATA)
+        host.register_namespace("avatar_metadata", NS_AVATAR_METADATA)
+        host.register_namespace("avatar_data", NS_AVATAR_DATA)
         self.host = host
         self._p = host.plugins["XEP-0060"]
         self._i = host.plugins['IDENTITY']
         self._i.register(
             IMPORT_NAME,
             "avatar",
-            self.getAvatar,
-            self.setAvatar,
+            self.get_avatar,
+            self.set_avatar,
             priority=2000
         )
-        host.plugins["XEP-0163"].addPEPEvent(
-            None, NS_AVATAR_METADATA, self._onMetadataUpdate
+        host.plugins["XEP-0163"].add_pep_event(
+            None, NS_AVATAR_METADATA, self._on_metadata_update
         )
 
-    def getHandler(self, client):
+    def get_handler(self, client):
         return XEP_0084_Handler()
 
-    def _onMetadataUpdate(self, itemsEvent, profile):
-        client = self.host.getClient(profile)
-        defer.ensureDeferred(self.onMetadataUpdate(client, itemsEvent))
+    def _on_metadata_update(self, itemsEvent, profile):
+        client = self.host.get_client(profile)
+        defer.ensureDeferred(self.on_metadata_update(client, itemsEvent))
 
-    async def onMetadataUpdate(
+    async def on_metadata_update(
         self,
         client: SatXMPPEntity,
         itemsEvent: pubsub.ItemsEvent
     ) -> None:
         entity = client.jid.userhostJID()
-        avatar_metadata = await self.getAvatar(client, entity)
+        avatar_metadata = await self.get_avatar(client, entity)
         await self._i.update(client, IMPORT_NAME, "avatar", avatar_metadata, entity)
 
-    async def getAvatar(
+    async def get_avatar(
             self,
             client: SatXMPPEntity,
             entity_jid: jid.JID
@@ -106,7 +106,7 @@
         service = entity_jid.userhostJID()
         # metadata
         try:
-            items, __ = await self._p.getItems(
+            items, __ = await self._p.get_items(
                 client,
                 service,
                 NS_AVATAR_METADATA,
@@ -146,10 +146,10 @@
             # (https://xmpp.org/extensions/xep-0084.html#pub-disable)
             return None
 
-        cache_data = self.host.common_cache.getMetadata(avatar_id)
+        cache_data = self.host.common_cache.get_metadata(avatar_id)
         if not cache_data:
             try:
-                data_items, __ = await self._p.getItems(
+                data_items, __ = await self._p.get_items(
                     client,
                     service,
                     NS_AVATAR_DATA,
@@ -172,7 +172,7 @@
                     f"{avatar_id!r}: {e}\n{data_item_elt.toXml()}"
                 )
                 return None
-            with self.host.common_cache.cacheData(
+            with self.host.common_cache.cache_data(
                 IMPORT_NAME,
                 avatar_id,
                 metadata["media_type"]
@@ -183,11 +183,11 @@
                     "mime_type": metadata["media_type"]
                 }
 
-        return self._i.avatarBuildMetadata(
+        return self._i.avatar_build_metadata(
                 cache_data['path'], cache_data['mime_type'], avatar_id
         )
 
-    def buildItemDataElt(self, avatar_data: Dict[str, Any]) -> domish.Element:
+    def build_item_data_elt(self, avatar_data: Dict[str, Any]) -> domish.Element:
         """Generate the item for the data node
 
         @param avatar_data: data as build by identity plugin (need to be filled with
@@ -197,7 +197,7 @@
         data_elt.addContent(avatar_data["base64"])
         return pubsub.Item(id=avatar_data["cache_uid"], payload=data_elt)
 
-    def buildItemMetadataElt(self, avatar_data: Dict[str, Any]) -> domish.Element:
+    def build_item_metadata_elt(self, avatar_data: Dict[str, Any]) -> domish.Element:
         """Generate the item for the metadata node
 
         @param avatar_data: data as build by identity plugin (need to be filled with
@@ -212,7 +212,7 @@
         info_elt["bytes"] = str(avatar_data["path"].stat().st_size)
         return pubsub.Item(id=self._p.ID_SINGLETON, payload=metadata_elt)
 
-    async def setAvatar(
+    async def set_avatar(
         self,
         client: SatXMPPEntity,
         avatar_data: Dict[str, Any],
@@ -227,7 +227,7 @@
         service = entity.userhostJID()
 
         # Data
-        await self._p.createIfNewNode(
+        await self._p.create_if_new_node(
             client,
             service,
             NS_AVATAR_DATA,
@@ -237,11 +237,11 @@
                 self._p.OPT_MAX_ITEMS: 1,
             }
         )
-        item_data_elt = self.buildItemDataElt(avatar_data)
-        await self._p.sendItems(client, service, NS_AVATAR_DATA, [item_data_elt])
+        item_data_elt = self.build_item_data_elt(avatar_data)
+        await self._p.send_items(client, service, NS_AVATAR_DATA, [item_data_elt])
 
         # Metadata
-        await self._p.createIfNewNode(
+        await self._p.create_if_new_node(
             client,
             service,
             NS_AVATAR_METADATA,
@@ -251,8 +251,8 @@
                 self._p.OPT_MAX_ITEMS: 1,
             }
         )
-        item_metadata_elt = self.buildItemMetadataElt(avatar_data)
-        await self._p.sendItems(client, service, NS_AVATAR_METADATA, [item_metadata_elt])
+        item_metadata_elt = self.build_item_metadata_elt(avatar_data)
+        await self._p.send_items(client, service, NS_AVATAR_METADATA, [item_metadata_elt])
 
 
 @implementer(iwokkel.IDisco)