diff sat/plugins/plugin_misc_text_commands.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents dbfedde9bc61
children 9d0df638c8b4
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_text_commands.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_misc_text_commands.py	Tue Aug 13 19:08:41 2019 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 # SàT plugin for managing text commands
@@ -57,8 +57,8 @@
     #       should be downloadable independently)
 
     HELP_SUGGESTION = _(
-        u"Type '/help' to get a list of the available commands. If you didn't want to "
-        u"use a command, please start your message with '//' to escape the slash."
+        "Type '/help' to get a list of the available commands. If you didn't want to "
+        "use a command, please start your message with '//' to escape the slash."
     )
 
     def __init__(self, host):
@@ -87,7 +87,7 @@
         data = OrderedDict([("doc_short_help", ""), ("type", "all"), ("args", "")])
         docstring = cmd.__doc__
         if docstring is None:
-            log.warning(u"No docstring found for command {}".format(cmd_name))
+            log.warning("No docstring found for command {}".format(cmd_name))
             docstring = ""
 
         doc_data = docstring.split("\n")
@@ -137,7 +137,7 @@
                     data["args"] = stripped[colon_idx + 1 :].strip()
         except InvalidCommandSyntax as e:
             log.warning(
-                u"Invalid command syntax for command {command}: {message}".format(
+                "Invalid command syntax for command {command}: {message}".format(
                     command=cmd_name, message=e.message
                 )
             )
@@ -153,7 +153,7 @@
             if attr.startswith("cmd_"):
                 cmd = getattr(instance, attr)
                 if not callable(cmd):
-                    log.warning(_(u"Skipping not callable [%s] attribute") % attr)
+                    log.warning(_("Skipping not callable [%s] attribute") % attr)
                     continue
                 cmd_name = attr[4:]
                 if not cmd_name:
@@ -165,7 +165,7 @@
                     new_name = cmd_name + str(suff)
                     log.warning(
                         _(
-                            u"Conflict for command [{old_name}], renaming it to [{new_name}]"
+                            "Conflict for command [{old_name}], renaming it to [{new_name}]"
                         ).format(old_name=cmd_name, new_name=new_name)
                     )
                     cmd_name = new_name
@@ -216,9 +216,9 @@
         except KeyError:
             try:
                 # we have not default message, we try to take the first found
-                msg_lang, msg = mess_data["message"].iteritems().next()
+                msg_lang, msg = next(iter(mess_data["message"].items()))
             except StopIteration:
-                log.debug(u"No message found, skipping text commands")
+                log.debug("No message found, skipping text commands")
                 return mess_data
 
         try:
@@ -244,15 +244,15 @@
                 if ret:
                     return mess_data
                 else:
-                    log.debug(u"text command detected ({})".format(command))
+                    log.debug("text command detected ({})".format(command))
                     raise failure.Failure(exceptions.CancelError())
 
             def genericErrback(failure):
                 try:
-                    msg = u"with condition {}".format(failure.value.condition)
+                    msg = "with condition {}".format(failure.value.condition)
                 except AttributeError:
-                    msg = u"with error {}".format(failure.value)
-                self.feedBack(client, u"Command failed {}".format(msg), mess_data)
+                    msg = "with error {}".format(failure.value)
+                self.feedBack(client, "Command failed {}".format(msg), mess_data)
                 return False
 
             mess_data["unparsed"] = msg[
@@ -280,7 +280,7 @@
                         command=command, context=context_txt
                     )
                     self.feedBack(
-                        client, u"{} {}".format(feedback, self.HELP_SUGGESTION), mess_data
+                        client, "{} {}".format(feedback, self.HELP_SUGGESTION), mess_data
                     )
                     log.debug("text command invalid message")
                     raise failure.Failure(exceptions.CancelError())
@@ -318,7 +318,7 @@
             if arg[-1] != "@":
                 return jid.JID(arg)
             return jid.JID(arg + service_jid)
-        return jid.JID(u"%s@%s" % (arg, service_jid))
+        return jid.JID("%s@%s" % (arg, service_jid))
 
     def feedBack(self, client, message, mess_data, info_type=FEEDBACK_INFO_TYPE):
         """Give a message back to the user"""
@@ -350,7 +350,7 @@
             room = mess_data["to"].userhostJID()
             try:
                 if self.host.plugins["XEP-0045"].isNickInRoom(client, room, entity):
-                    entity = u"%s/%s" % (room, entity)
+                    entity = "%s/%s" % (room, entity)
             except KeyError:
                 log.warning("plugin XEP-0045 is not present")
 
@@ -368,7 +368,7 @@
         if not target_jid.resource:
             target_jid.resource = self.host.memory.getMainResource(client, target_jid)
 
-        whois_msg = [_(u"whois for %(jid)s") % {"jid": target_jid}]
+        whois_msg = [_("whois for %(jid)s") % {"jid": target_jid}]
 
         d = defer.succeed(None)
         for ignore, callback in self._whois:
@@ -377,7 +377,7 @@
             )
 
         def feedBack(ignore):
-            self.feedBack(client, u"\n".join(whois_msg), mess_data)
+            self.feedBack(client, "\n".join(whois_msg), mess_data)
             return False
 
         d.addCallback(feedBack)
@@ -390,11 +390,11 @@
         @return (list[unicode]): help strings
         """
         strings = []
-        for doc_name, doc_help in cmd_data.iteritems():
+        for doc_name, doc_help in cmd_data.items():
             if doc_name.startswith("doc_arg_"):
                 arg_name = doc_name[8:]
                 strings.append(
-                    u"- {name}: {doc_help}".format(name=arg_name, doc_help=_(doc_help))
+                    "- {name}: {doc_help}".format(name=arg_name, doc_help=_(doc_help))
                 )
 
         return strings
@@ -424,7 +424,7 @@
             cmd_name = cmd_name[1:]
         if cmd_name and cmd_name not in self._commands:
             self.feedBack(
-                client, _(u"Invalid command name [{}]\n".format(cmd_name)), mess_data
+                client, _("Invalid command name [{}]\n".format(cmd_name)), mess_data
             )
             cmd_name = ""
         if not cmd_name:
@@ -445,17 +445,17 @@
                     )
                 )
 
-            help_mess = _(u"Text commands available:\n%s") % (u"\n".join(help_cmds),)
+            help_mess = _("Text commands available:\n%s") % ("\n".join(help_cmds),)
         else:
             # we show detailled help for a command
             cmd_data = self._commands[cmd_name]
             syntax = cmd_data["args"]
-            help_mess = _(u"/{name}: {short_help}\n{syntax}{args_help}").format(
+            help_mess = _("/{name}: {short_help}\n{syntax}{args_help}").format(
                 name=cmd_name,
                 short_help=cmd_data["doc_short_help"],
                 syntax=_(" " * 4 + "syntax: {}\n").format(syntax) if syntax else "",
-                args_help=u"\n".join(
-                    [u" " * 8 + "{}".format(line) for line in self._getArgsHelp(cmd_data)]
+                args_help="\n".join(
+                    [" " * 8 + "{}".format(line) for line in self._getArgsHelp(cmd_data)]
                 ),
             )