diff sat/plugins/plugin_misc_text_commands.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 ae5f63e5ed2c
children
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_text_commands.py	Fri Apr 07 15:18:39 2023 +0200
+++ b/sat/plugins/plugin_misc_text_commands.py	Sat Apr 08 13:54:42 2023 +0200
@@ -66,12 +66,12 @@
         log.info(_("Text commands initialization"))
         self.host = host
         # this is internal command, so we set high priority
-        host.trigger.add("sendMessage", self.sendMessageTrigger, priority=1000000)
+        host.trigger.add("sendMessage", self.send_message_trigger, priority=1000000)
         self._commands = {}
         self._whois = []
-        self.registerTextCommands(self)
+        self.register_text_commands(self)
 
-    def _parseDocString(self, cmd, cmd_name):
+    def _parse_doc_string(self, cmd, cmd_name):
         """Parse a docstring to get text command data
 
         @param cmd: function or method callback for the command,
@@ -150,7 +150,7 @@
 
         return data
 
-    def registerTextCommands(self, instance):
+    def register_text_commands(self, instance):
         """ Add a text command
 
         @param instance: instance of a class containing text commands
@@ -176,10 +176,10 @@
                     )
                     cmd_name = new_name
                 self._commands[cmd_name] = cmd_data = {"callback": cmd}
-                cmd_data.update(self._parseDocString(cmd, cmd_name))
+                cmd_data.update(self._parse_doc_string(cmd, cmd_name))
                 log.info(_("Registered text command [%s]") % cmd_name)
 
-    def addWhoIsCb(self, callback, priority=0):
+    def add_who_is_cb(self, callback, priority=0):
         """Add a callback which give information to the /whois command
 
         @param callback: a callback which will be called with the following arguments
@@ -193,14 +193,14 @@
         self._whois.append((priority, callback))
         self._whois.sort(key=lambda item: item[0], reverse=True)
 
-    def sendMessageTrigger(
+    def send_message_trigger(
         self, client, mess_data, pre_xml_treatments, post_xml_treatments
     ):
         """Install SendMessage command hook """
-        pre_xml_treatments.addCallback(self._sendMessageCmdHook, client)
+        pre_xml_treatments.addCallback(self._send_message_cmd_hook, client)
         return True
 
-    def _sendMessageCmdHook(self, mess_data, client):
+    def _send_message_cmd_hook(self, mess_data, client):
         """ Check text commands in message, and react consequently
 
         msg starting with / are potential command. If a command is found, it is executed,
@@ -239,7 +239,7 @@
         d = None
         command = msg[1:].partition(" ")[0].lower().strip()
         if not command.isidentifier():
-            self.feedBack(
+            self.feed_back(
                 client,
                 _("Invalid command /%s. ") % command + self.HELP_SUGGESTION,
                 mess_data,
@@ -247,7 +247,7 @@
             raise failure.Failure(exceptions.CancelError())
 
         # looks like an actual command, we try to call the corresponding method
-        def retHandling(ret):
+        def ret_handling(ret):
             """ Handle command return value:
             if ret is True, normally send message (possibly modified by command)
             else, abord message sending
@@ -258,12 +258,12 @@
                 log.debug("text command detected ({})".format(command))
                 raise failure.Failure(exceptions.CancelError())
 
-        def genericErrback(failure):
+        def generic_errback(failure):
             try:
                 msg = "with condition {}".format(failure.value.condition)
             except AttributeError:
                 msg = "with error {}".format(failure.value)
-            self.feedBack(client, "Command failed {}".format(msg), mess_data)
+            self.feed_back(client, "Command failed {}".format(msg), mess_data)
             return False
 
         mess_data["unparsed"] = msg[
@@ -272,7 +272,7 @@
         try:
             cmd_data = self._commands[command]
         except KeyError:
-            self.feedBack(
+            self.feed_back(
                 client,
                 _("Unknown command /%s. ") % command + self.HELP_SUGGESTION,
                 mess_data,
@@ -280,7 +280,7 @@
             log.debug("text command help message")
             raise failure.Failure(exceptions.CancelError())
         else:
-            if not self._contextValid(mess_data, cmd_data):
+            if not self._context_valid(mess_data, cmd_data):
                 # The command is not launched in the right context, we throw a message with help instructions
                 context_txt = (
                     _("group discussions")
@@ -290,23 +290,23 @@
                 feedback = _("/{command} command only applies in {context}.").format(
                     command=command, context=context_txt
                 )
-                self.feedBack(
+                self.feed_back(
                     client, "{} {}".format(feedback, self.HELP_SUGGESTION), mess_data
                 )
                 log.debug("text command invalid message")
                 raise failure.Failure(exceptions.CancelError())
             else:
-                d = utils.asDeferred(cmd_data["callback"], client, mess_data)
-                d.addErrback(genericErrback)
-                d.addCallback(retHandling)
+                d = utils.as_deferred(cmd_data["callback"], client, mess_data)
+                d.addErrback(generic_errback)
+                d.addCallback(ret_handling)
 
         return d
 
-    def _contextValid(self, mess_data, cmd_data):
+    def _context_valid(self, mess_data, cmd_data):
         """Tell if a command can be used in the given context
 
         @param mess_data(dict): message data as given in sendMessage trigger
-        @param cmd_data(dict): command data as returned by self._parseDocString
+        @param cmd_data(dict): command data as returned by self._parse_doc_string
         @return (bool): True if command can be used in this context
         """
         if (cmd_data["type"] == "group" and mess_data["type"] != "groupchat") or (
@@ -315,7 +315,7 @@
             return False
         return True
 
-    def getRoomJID(self, arg, service_jid):
+    def get_room_jid(self, arg, service_jid):
         """Return a room jid with a shortcut
 
         @param arg: argument: can be a full room jid (e.g.: sat@chat.jabberfr.org)
@@ -329,7 +329,7 @@
             return jid.JID(arg + service_jid)
         return jid.JID(f"{arg}@{service_jid}")
 
-    def feedBack(self, client, message, mess_data, info_type=FEEDBACK_INFO_TYPE):
+    def feed_back(self, client, message, mess_data, info_type=FEEDBACK_INFO_TYPE):
         """Give a message back to the user"""
         if mess_data["type"] == "groupchat":
             to_ = mess_data["to"].userhostJID()
@@ -342,7 +342,7 @@
         mess_data["type"] = C.MESS_TYPE_INFO
         mess_data["message"] = {"": message}
         mess_data["extra"]["info_type"] = info_type
-        client.messageSendToBridge(mess_data)
+        client.message_send_to_bridge(mess_data)
 
     def cmd_whois(self, client, mess_data):
         """show informations on entity
@@ -358,7 +358,7 @@
         if mess_data["type"] == "groupchat":
             room = mess_data["to"].userhostJID()
             try:
-                if self.host.plugins["XEP-0045"].isNickInRoom(client, room, entity):
+                if self.host.plugins["XEP-0045"].is_nick_in_room(client, room, entity):
                     entity = "%s/%s" % (room, entity)
             except KeyError:
                 log.warning("plugin XEP-0045 is not present")
@@ -371,11 +371,11 @@
                 if not target_jid.user or not target_jid.host:
                     raise jid.InvalidFormat
             except (RuntimeError, jid.InvalidFormat, AttributeError):
-                self.feedBack(client, _("Invalid jid, can't whois"), mess_data)
+                self.feed_back(client, _("Invalid jid, can't whois"), mess_data)
                 return False
 
         if not target_jid.resource:
-            target_jid.resource = self.host.memory.getMainResource(client, target_jid)
+            target_jid.resource = self.host.memory.main_resource_get(client, target_jid)
 
         whois_msg = [_("whois for %(jid)s") % {"jid": target_jid}]
 
@@ -385,14 +385,14 @@
                 lambda __: callback(client, whois_msg, mess_data, target_jid)
             )
 
-        def feedBack(__):
-            self.feedBack(client, "\n".join(whois_msg), mess_data)
+        def feed_back(__):
+            self.feed_back(client, "\n".join(whois_msg), mess_data)
             return False
 
-        d.addCallback(feedBack)
+        d.addCallback(feed_back)
         return d
 
-    def _getArgsHelp(self, cmd_data):
+    def _get_args_help(self, cmd_data):
         """Return help string for args of cmd_name, according to docstring data
 
         @param cmd_data: command data
@@ -420,7 +420,7 @@
 
     def cmd_whoami(self, client, mess_data):
         """give your own jid"""
-        self.feedBack(client, client.jid.full(), mess_data)
+        self.feed_back(client, client.jid.full(), mess_data)
 
     def cmd_help(self, client, mess_data):
         """show help on available commands
@@ -432,7 +432,7 @@
         if cmd_name and cmd_name[0] == "/":
             cmd_name = cmd_name[1:]
         if cmd_name and cmd_name not in self._commands:
-            self.feedBack(
+            self.feed_back(
                 client, _("Invalid command name [{}]\n".format(cmd_name)), mess_data
             )
             cmd_name = ""
@@ -443,7 +443,7 @@
 
             for command in sorted(self._commands):
                 cmd_data = self._commands[command]
-                if not self._contextValid(mess_data, cmd_data):
+                if not self._context_valid(mess_data, cmd_data):
                     continue
                 spaces = (longuest - len(command)) * " "
                 help_cmds.append(
@@ -464,8 +464,8 @@
                 short_help=cmd_data["doc_short_help"],
                 syntax=_(" " * 4 + "syntax: {}\n").format(syntax) if syntax else "",
                 args_help="\n".join(
-                    [" " * 8 + "{}".format(line) for line in self._getArgsHelp(cmd_data)]
+                    [" " * 8 + "{}".format(line) for line in self._get_args_help(cmd_data)]
                 ),
             )
 
-        self.feedBack(client, help_mess, mess_data)
+        self.feed_back(client, help_mess, mess_data)