diff sat/plugins/plugin_xep_0048.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 32d714a8ea51
children
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0048.py	Fri Apr 07 15:18:39 2023 +0200
+++ b/sat/plugins/plugin_xep_0048.py	Sat Apr 08 13:54:42 2023 +0200
@@ -57,39 +57,39 @@
     def __init__(self, host):
         log.info(_("Bookmarks plugin initialization"))
         self.host = host
-        # self.__menu_id = host.registerCallback(self._bookmarksMenu, with_data=True)
-        self.__bm_save_id = host.registerCallback(self._bookmarksSaveCb, with_data=True)
-        host.importMenu(
+        # self.__menu_id = host.register_callback(self._bookmarks_menu, with_data=True)
+        self.__bm_save_id = host.register_callback(self._bookmarks_save_cb, with_data=True)
+        host.import_menu(
             (D_("Groups"), D_("Bookmarks")),
-            self._bookmarksMenu,
+            self._bookmarks_menu,
             security_limit=0,
             help_string=D_("Use and manage bookmarks"),
         )
-        self.__selected_id = host.registerCallback(
-            self._bookmarkSelectedCb, with_data=True
+        self.__selected_id = host.register_callback(
+            self._bookmark_selected_cb, with_data=True
         )
-        host.bridge.addMethod(
-            "bookmarksList",
+        host.bridge.add_method(
+            "bookmarks_list",
             ".plugin",
             in_sign="sss",
             out_sign="a{sa{sa{ss}}}",
-            method=self._bookmarksList,
+            method=self._bookmarks_list,
             async_=True,
         )
-        host.bridge.addMethod(
-            "bookmarksRemove",
+        host.bridge.add_method(
+            "bookmarks_remove",
             ".plugin",
             in_sign="ssss",
             out_sign="",
-            method=self._bookmarksRemove,
+            method=self._bookmarks_remove,
             async_=True,
         )
-        host.bridge.addMethod(
-            "bookmarksAdd",
+        host.bridge.add_method(
+            "bookmarks_add",
             ".plugin",
             in_sign="ssa{ss}ss",
             out_sign="",
-            method=self._bookmarksAdd,
+            method=self._bookmarks_add,
             async_=True,
         )
         try:
@@ -97,11 +97,11 @@
         except KeyError:
             self.private_plg = None
         try:
-            self.host.plugins[C.TEXT_CMDS].registerTextCommands(self)
+            self.host.plugins[C.TEXT_CMDS].register_text_commands(self)
         except KeyError:
             log.info(_("Text commands not available"))
 
-    async def profileConnected(self, client):
+    async def profile_connected(self, client):
         local = client.bookmarks_local = PersistentBinaryDict(
             NS_BOOKMARKS, client.profile
         )
@@ -109,7 +109,7 @@
         if not local:
             local[XEP_0048.MUC_TYPE] = dict()
             local[XEP_0048.URL_TYPE] = dict()
-        private = await self._getServerBookmarks("private", client.profile)
+        private = await self._get_server_bookmarks("private", client.profile)
         pubsub = client.bookmarks_pubsub = None
 
         for bookmarks in (local, private, pubsub):
@@ -125,7 +125,7 @@
         # slow down a lot the connection process, and result in a bad user experience.
 
     @defer.inlineCallbacks
-    def _getServerBookmarks(self, storage_type, profile):
+    def _get_server_bookmarks(self, storage_type, profile):
         """Get distants bookmarks
 
         update also the client.bookmarks_[type] key, with None if service is not available
@@ -135,13 +135,13 @@
         @param profile: %(doc_profile)s
         @return: data dictionary, or None if feature is not available
         """
-        client = self.host.getClient(profile)
+        client = self.host.get_client(profile)
         if storage_type == "private":
             try:
-                bookmarks_private_xml = yield self.private_plg.privateXMLGet(
+                bookmarks_private_xml = yield self.private_plg.private_xml_get(
                     "storage", NS_BOOKMARKS, profile
                 )
-                data = client.bookmarks_private = self._bookmarkElt2Dict(
+                data = client.bookmarks_private = self._bookmark_elt_2_dict(
                     bookmarks_private_xml
                 )
             except (StanzaError, AttributeError):
@@ -154,7 +154,7 @@
         defer.returnValue(data)
 
     @defer.inlineCallbacks
-    def _setServerBookmarks(self, storage_type, bookmarks_elt, profile):
+    def _set_server_bookmarks(self, storage_type, bookmarks_elt, profile):
         """Save bookmarks on server
 
         @param storage_type: storage type, can be:
@@ -164,19 +164,19 @@
         @param profile: %(doc_profile)s
         """
         if storage_type == "private":
-            yield self.private_plg.privateXMLStore(bookmarks_elt, profile)
+            yield self.private_plg.private_xml_store(bookmarks_elt, profile)
         elif storage_type == "pubsub":
             raise NotImplementedError
         else:
             raise ValueError("storage_type must be 'private' or 'pubsub'")
 
-    def _bookmarkElt2Dict(self, storage_elt):
+    def _bookmark_elt_2_dict(self, storage_elt):
         """Parse bookmarks to get dictionary
         @param storage_elt (domish.Element): bookmarks storage
         @return (dict): bookmark data (key: bookmark type, value: list) where key can be:
             - XEP_0048.MUC_TYPE
             - XEP_0048.URL_TYPE
-            - value (dict): data as for addBookmark
+            - value (dict): data as for add_bookmark
         """
         conf_data = {}
         url_data = {}
@@ -218,12 +218,12 @@
 
         return {XEP_0048.MUC_TYPE: conf_data, XEP_0048.URL_TYPE: url_data}
 
-    def _dict2BookmarkElt(self, type_, data):
+    def _dict_2_bookmark_elt(self, type_, data):
         """Construct a bookmark element from a data dict
         @param data (dict): bookmark data (key: bookmark type, value: list) where key can be:
             - XEP_0048.MUC_TYPE
             - XEP_0048.URL_TYPE
-            - value (dict): data as for addBookmark
+            - value (dict): data as for add_bookmark
         @return (domish.Element): bookmark element
         """
         rooms_data = data.get(XEP_0048.MUC_TYPE, {})
@@ -253,7 +253,7 @@
 
         return storage_elt
 
-    def _bookmarkSelectedCb(self, data, profile):
+    def _bookmark_selected_cb(self, data, profile):
         try:
             room_jid_s, nick = data["index"].split(" ", 1)
             room_jid = jid.JID(room_jid_s)
@@ -261,7 +261,7 @@
             log.warning(_("No room jid selected"))
             return {}
 
-        client = self.host.getClient(profile)
+        client = self.host.get_client(profile)
         d = self.host.plugins["XEP-0045"].join(client, room_jid, nick, {})
 
         def join_eb(failure):
@@ -272,14 +272,14 @@
         d.addCallbacks(lambda __: {}, join_eb)
         return d
 
-    def _bookmarksMenu(self, data, profile):
+    def _bookmarks_menu(self, data, profile):
         """ XMLUI activated by menu: return Gateways UI
         @param profile: %(doc_profile)s
 
         """
-        client = self.host.getClient(profile)
+        client = self.host.get_client(profile)
         xmlui = xml_tools.XMLUI(title=_("Bookmarks manager"))
-        adv_list = xmlui.changeContainer(
+        adv_list = xmlui.change_container(
             "advanced_list",
             columns=3,
             selectable="single",
@@ -297,7 +297,7 @@
                 key=lambda item: item[1].get("name", item[0].user),
             ):
                 room_jid_s = room_jid.full()
-                adv_list.setRowIndex(
+                adv_list.set_row_index(
                     "%s %s" % (room_jid_s, data.get("nick") or client.jid.user)
                 )
                 xmlui.addText(data.get("name", ""))
@@ -309,7 +309,7 @@
         adv_list.end()
         xmlui.addDivider("dash")
         xmlui.addText(_("add a bookmark"))
-        xmlui.changeContainer("pairs")
+        xmlui.change_container("pairs")
         xmlui.addLabel(_("Name"))
         xmlui.addString("name")
         xmlui.addLabel(_("jid"))
@@ -318,22 +318,22 @@
         xmlui.addString("nick", client.jid.user)
         xmlui.addLabel(_("Autojoin"))
         xmlui.addBool("autojoin")
-        xmlui.changeContainer("vertical")
+        xmlui.change_container("vertical")
         xmlui.addButton(self.__bm_save_id, _("Save"), ("name", "jid", "nick", "autojoin"))
         return {"xmlui": xmlui.toXml()}
 
-    def _bookmarksSaveCb(self, data, profile):
-        bm_data = xml_tools.XMLUIResult2DataFormResult(data)
+    def _bookmarks_save_cb(self, data, profile):
+        bm_data = xml_tools.xmlui_result_2_data_form_result(data)
         try:
             location = jid.JID(bm_data.pop("jid"))
         except KeyError:
             raise exceptions.InternalError("Can't find mandatory key")
-        d = self.addBookmark(XEP_0048.MUC_TYPE, location, bm_data, profile_key=profile)
+        d = self.add_bookmark(XEP_0048.MUC_TYPE, location, bm_data, profile_key=profile)
         d.addCallback(lambda __: {})
         return d
 
     @defer.inlineCallbacks
-    def addBookmark(
+    def add_bookmark(
         self, type_, location, data, storage_type="auto", profile_key=C.PROF_KEY_NONE
     ):
         """Store a new bookmark
@@ -357,7 +357,7 @@
         assert storage_type in ("auto", "pubsub", "private", "local")
         if type_ == XEP_0048.URL_TYPE and {"autojoin", "nick"}.intersection(list(data.keys())):
             raise ValueError("autojoin or nick can't be used with URLs")
-        client = self.host.getClient(profile_key)
+        client = self.host.get_client(profile_key)
         if storage_type == "auto":
             if client.bookmarks_pubsub is not None:
                 storage_type = "pubsub"
@@ -372,13 +372,13 @@
             client.bookmarks_local[type_][location] = data
             yield client.bookmarks_local.force(type_)
         else:
-            bookmarks = yield self._getServerBookmarks(storage_type, client.profile)
+            bookmarks = yield self._get_server_bookmarks(storage_type, client.profile)
             bookmarks[type_][location] = data
-            bookmark_elt = self._dict2BookmarkElt(type_, bookmarks)
-            yield self._setServerBookmarks(storage_type, bookmark_elt, client.profile)
+            bookmark_elt = self._dict_2_bookmark_elt(type_, bookmarks)
+            yield self._set_server_bookmarks(storage_type, bookmark_elt, client.profile)
 
     @defer.inlineCallbacks
-    def removeBookmark(
+    def remove_bookmark(
         self, type_, location, storage_type="all", profile_key=C.PROF_KEY_NONE
     ):
         """Remove a stored bookmark
@@ -395,7 +395,7 @@
         @param profile_key: %(doc_profile_key)s
         """
         assert storage_type in ("all", "pubsub", "private", "local")
-        client = self.host.getClient(profile_key)
+        client = self.host.get_client(profile_key)
 
         if storage_type in ("all", "local"):
             try:
@@ -405,18 +405,18 @@
                 log.debug("Bookmark is not present in local storage")
 
         if storage_type in ("all", "private"):
-            bookmarks = yield self._getServerBookmarks("private", client.profile)
+            bookmarks = yield self._get_server_bookmarks("private", client.profile)
             try:
                 del bookmarks[type_][location]
-                bookmark_elt = self._dict2BookmarkElt(type_, bookmarks)
-                yield self._setServerBookmarks("private", bookmark_elt, client.profile)
+                bookmark_elt = self._dict_2_bookmark_elt(type_, bookmarks)
+                yield self._set_server_bookmarks("private", bookmark_elt, client.profile)
             except KeyError:
                 log.debug("Bookmark is not present in private storage")
 
         if storage_type == "pubsub":
             raise NotImplementedError
 
-    def _bookmarksList(self, type_, storage_location, profile_key=C.PROF_KEY_NONE):
+    def _bookmarks_list(self, type_, storage_location, profile_key=C.PROF_KEY_NONE):
         """Return stored bookmarks
 
         @param type_: bookmark type, one of:
@@ -431,11 +431,11 @@
         @param return (dict): (key: storage_location, value dict) with:
             - value (dict): (key: bookmark_location, value: bookmark data)
         """
-        client = self.host.getClient(profile_key)
+        client = self.host.get_client(profile_key)
         ret = {}
         ret_d = defer.succeed(ret)
 
-        def fillBookmarks(__, _storage_location):
+        def fill_bookmarks(__, _storage_location):
             bookmarks_ori = getattr(client, "bookmarks_" + _storage_location)
             if bookmarks_ori is None:
                 return ret
@@ -452,15 +452,15 @@
                 ret[_storage_location] = {}
                 if _storage_location in ("private",):
                     # we update distant bookmarks, just in case an other client added something
-                    d = self._getServerBookmarks(_storage_location, client.profile)
+                    d = self._get_server_bookmarks(_storage_location, client.profile)
                 else:
                     d = defer.succeed(None)
-                d.addCallback(fillBookmarks, _storage_location)
+                d.addCallback(fill_bookmarks, _storage_location)
                 ret_d.addCallback(lambda __: d)
 
         return ret_d
 
-    def _bookmarksRemove(
+    def _bookmarks_remove(
         self, type_, location, storage_location, profile_key=C.PROF_KEY_NONE
     ):
         """Return stored bookmarks
@@ -478,14 +478,14 @@
         """
         if type_ == XEP_0048.MUC_TYPE:
             location = jid.JID(location)
-        return self.removeBookmark(type_, location, storage_location, profile_key)
+        return self.remove_bookmark(type_, location, storage_location, profile_key)
 
-    def _bookmarksAdd(
+    def _bookmarks_add(
         self, type_, location, data, storage_type="auto", profile_key=C.PROF_KEY_NONE
     ):
         if type_ == XEP_0048.MUC_TYPE:
             location = jid.JID(location)
-        return self.addBookmark(type_, location, data, storage_type, profile_key)
+        return self.add_bookmark(type_, location, data, storage_type, profile_key)
 
     def cmd_bookmark(self, client, mess_data):
         """(Un)bookmark a MUC room
@@ -498,14 +498,14 @@
 
         options = mess_data["unparsed"].strip().split()
         if options and options[0] not in ("autojoin", "remove"):
-            txt_cmd.feedBack(client, _("Bad arguments"), mess_data)
+            txt_cmd.feed_back(client, _("Bad arguments"), mess_data)
             return False
 
         room_jid = mess_data["to"].userhostJID()
 
         if "remove" in options:
-            self.removeBookmark(XEP_0048.MUC_TYPE, room_jid, profile_key=client.profile)
-            txt_cmd.feedBack(
+            self.remove_bookmark(XEP_0048.MUC_TYPE, room_jid, profile_key=client.profile)
+            txt_cmd.feed_back(
                 client,
                 _("All [%s] bookmarks are being removed") % room_jid.full(),
                 mess_data,
@@ -517,7 +517,7 @@
             "nick": client.jid.user,
             "autojoin": "true" if "autojoin" in options else "false",
         }
-        self.addBookmark(XEP_0048.MUC_TYPE, room_jid, data, profile_key=client.profile)
-        txt_cmd.feedBack(client, _("Bookmark added"), mess_data)
+        self.add_bookmark(XEP_0048.MUC_TYPE, room_jid, data, profile_key=client.profile)
+        txt_cmd.feed_back(client, _("Bookmark added"), mess_data)
 
         return False