diff sat/plugins/plugin_misc_account.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 420897488080
children 82eee2c383d9
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_account.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_misc_account.py	Tue Aug 13 19:08:41 2019 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 # SAT plugin for account creation (experimental)
@@ -26,7 +26,7 @@
 from sat.memory.memory import Sessions
 from sat.memory.crypto import PasswordHasher
 from sat.core.constants import Const as C
-import ConfigParser
+import configparser
 from twisted.internet import defer
 from twisted.python.failure import Failure
 from twisted.words.protocols.jabber import jid
@@ -45,7 +45,7 @@
     C.PI_RECOMMENDATIONS: ["GROUPBLOG"],
     C.PI_MAIN: "MiscAccount",
     C.PI_HANDLER: "no",
-    C.PI_DESCRIPTION: _(u"""SàT account creation"""),
+    C.PI_DESCRIPTION: _("""SàT account creation"""),
 }
 
 CONFIG_SECTION = "plugin account"
@@ -71,7 +71,7 @@
 }
 
 WELCOME_MSG = D_(
-    u"""Welcome to Libervia, the web interface of Salut à Toi.
+    """Welcome to Libervia, the web interface of Salut à Toi.
 
 Your account on {domain} has been successfully created.
 This is a demonstration version to show you the current status of the project.
@@ -94,7 +94,7 @@
 """
 )
 
-DEFAULT_DOMAIN = u"example.net"
+DEFAULT_DOMAIN = "example.net"
 
 
 class MiscAccount(object):
@@ -104,7 +104,7 @@
     # TODO: cleaning, separate email handling, more configuration/tests, fixes
 
     def __init__(self, host):
-        log.info(_(u"Plugin Account initialization"))
+        log.info(_("Plugin Account initialization"))
         self.host = host
         host.bridge.addMethod(
             "registerSatAccount",
@@ -112,7 +112,7 @@
             in_sign="sss",
             out_sign="",
             method=self._registerAccount,
-            async=True,
+            async_=True,
         )
         host.bridge.addMethod(
             "getNewAccountDomain",
@@ -120,7 +120,7 @@
             in_sign="",
             out_sign="s",
             method=self.getNewAccountDomain,
-            async=False,
+            async_=False,
         )
         host.bridge.addMethod(
             "getAccountDialogUI",
@@ -128,7 +128,7 @@
             in_sign="s",
             out_sign="s",
             method=self._getAccountDialogUI,
-            async=False,
+            async_=False,
         )
         host.bridge.addMethod(
             "asyncConnectWithXMPPCredentials",
@@ -136,7 +136,7 @@
             in_sign="ss",
             out_sign="b",
             method=self.asyncConnectWithXMPPCredentials,
-            async=True,
+            async_=True,
         )
 
         self.fixEmailAdmins()
@@ -175,13 +175,13 @@
         if not admin_email:
             return
         log.warning(
-            u"admin_email parameter is deprecated, please use email_admins_list instead"
+            "admin_email parameter is deprecated, please use email_admins_list instead"
         )
         param_name = "email_admins_list"
         try:
             section = ""
             value = self.host.memory.getConfig(section, param_name, Exception)
-        except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
+        except (configparser.NoOptionError, configparser.NoSectionError):
             section = CONFIG_SECTION
             value = self.host.memory.getConfig(
                 section, param_name, default_conf[param_name]
@@ -198,7 +198,7 @@
             #      they can now be in [DEFAULT] section
             try:
                 value = self.host.memory.getConfig(None, name, Exception)
-            except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
+            except (configparser.NoOptionError, configparser.NoSectionError):
                 pass
             else:
                 return value
@@ -263,7 +263,7 @@
             d = defer.succeed(None)
             jid_ = jid.JID(jid_s)
         else:
-            jid_s = profile + u"@" + self.getNewAccountDomain()
+            jid_s = profile + "@" + self.getNewAccountDomain()
             jid_ = jid.JID(jid_s)
             d = self.host.plugins["XEP-0077"].registerNewAccount(jid_, password)
 
@@ -289,7 +289,7 @@
     def _sendEmailEb(self, failure_, email):
         # TODO: return error code to user
         log.error(
-            _(u"Failed to send account creation confirmation to {email}: {msg}").format(
+            _("Failed to send account creation confirmation to {email}: {msg}").format(
                 email=email, msg=failure_
             )
         )
@@ -303,27 +303,27 @@
         admins_emails = self.getConfig("email_admins_list")
         if not admins_emails:
             log.warning(
-                u"No known admin email, we can't send email to administrator(s).\nPlease fill email_admins_list parameter"
+                "No known admin email, we can't send email to administrator(s).\nPlease fill email_admins_list parameter"
             )
             d_admin = defer.fail(exceptions.DataError("no admin email"))
         else:
-            subject = _(u"New Libervia account created")
-            body = u"""New account created: {profile} [{email}]""".format(
+            subject = _("New Libervia account created")
+            body = """New account created: {profile} [{email}]""".format(
                 profile=profile,
                 # there is no email when an existing XMPP account is used
-                email=email or u"<no email>",
+                email=email or "<no email>",
             )
             d_admin = sat_email.sendEmail(self.host, admins_emails, subject, body)
 
-        admins_emails_txt = u", ".join([u"<" + addr + u">" for addr in admins_emails])
+        admins_emails_txt = ", ".join(["<" + addr + ">" for addr in admins_emails])
         d_admin.addCallbacks(
             lambda __: log.debug(
-                u"Account creation notification sent to admin(s) {}".format(
+                "Account creation notification sent to admin(s) {}".format(
                     admins_emails_txt
                 )
             ),
             lambda __: log.error(
-                u"Failed to send account creation notification to admin {}".format(
+                "Failed to send account creation notification to admin {}".format(
                     admins_emails_txt
                 )
             ),
@@ -333,9 +333,9 @@
             return d_admin
 
         jid_s = self.host.memory.getParamA(
-            u"JabberID", u"Connection", profile_key=profile
+            "JabberID", "Connection", profile_key=profile
         )
-        subject = _(u"Your Libervia account has been created")
+        subject = _("Your Libervia account has been created")
         body = _(WELCOME_MSG).format(profile=profile, jid=jid_s, domain=domain)
 
         # XXX: this will not fail when the email address doesn't exist
@@ -344,7 +344,7 @@
         d_user = sat_email.sendEmail(self.host, [email], subject, body)
         d_user.addCallbacks(
             lambda __: log.debug(
-                u"Account creation confirmation sent to <{}>".format(email)
+                "Account creation confirmation sent to <{}>".format(email)
             ),
             self._sendEmailEb,
         )
@@ -359,7 +359,7 @@
         if not domain:
             log.warning(
                 _(
-                    u'xmpp_domain needs to be set in sat.conf. Using "{default}" meanwhile'
+                    'xmpp_domain needs to be set in sat.conf. Using "{default}" meanwhile'
                 ).format(default=DEFAULT_DOMAIN)
             )
             return DEFAULT_DOMAIN