diff sat/plugins/plugin_xep_0096.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 69e4716d6268
children 9d0df638c8b4
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0096.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_xep_0096.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 managing xep-0096
@@ -60,7 +60,7 @@
         ]  # Stream methods managed
         self._f = self.host.plugins["FILE"]
         self._f.register(
-            NS_SI_FT, self.sendFile, priority=0, method_name=u"Stream Initiation"
+            NS_SI_FT, self.sendFile, priority=0, method_name="Stream Initiation"
         )
         self._si = self.host.plugins["XEP-0095"]
         self._si.registerSIProfile(SI_PROFILE_NAME, self._transferRequest)
@@ -91,7 +91,7 @@
             - range_length
         """
         try:
-            range_elt = parent_elt.elements(NS_SI_FT, "range").next()
+            range_elt = next(parent_elt.elements(NS_SI_FT, "range"))
         except StopIteration:
             range_ = False
             range_offset = None
@@ -126,7 +126,7 @@
         peer_jid = jid.JID(iq_elt["from"])
 
         try:
-            file_elt = si_elt.elements(NS_SI_FT, "file").next()
+            file_elt = next(si_elt.elements(NS_SI_FT, "file"))
         except StopIteration:
             return self._badRequest(
                 client, iq_elt, "No <file/> element found in SI File Transfer request"
@@ -149,13 +149,13 @@
         file_hash = file_elt.getAttribute("hash")
 
         log.info(
-            u"File proposed: name=[{name}] size={size}".format(
+            "File proposed: name=[{name}] size={size}".format(
                 name=filename, size=file_size
             )
         )
 
         try:
-            file_desc = unicode(file_elt.elements(NS_SI_FT, "desc").next())
+            file_desc = str(next(file_elt.elements(NS_SI_FT, "desc")))
         except StopIteration:
             file_desc = ""
 
@@ -178,10 +178,10 @@
                 plugin = self.host.plugins["XEP-0047"]
             else:
                 log.error(
-                    u"Unknown stream method, this should not happen at this stage, cancelling transfer"
+                    "Unknown stream method, this should not happen at this stage, cancelling transfer"
                 )
         else:
-            log.warning(u"Can't find a valid stream method")
+            log.warning("Can't find a valid stream method")
             self._si.sendError(client, iq_elt, "not-acceptable")
             return
 
@@ -213,7 +213,7 @@
         @param data(dict): session data
         """
         if not accepted:
-            log.info(u"File transfer declined")
+            log.info("File transfer declined")
             self._si.sendError(client, iq_elt, "forbidden")
             return
         # data, timeout, stream_method, failed_methods = client._xep_0096_waiting_for_approval[sid]
@@ -264,7 +264,7 @@
         """
         # TODO: check hash
         data["stream_object"].close()
-        log.info(u"Transfer {si_id} successfuly finished".format(**data))
+        log.info("Transfer {si_id} successfuly finished".format(**data))
 
     def _transferEb(self, failure, client, data):
         """Called when something went wrong with the transfer
@@ -273,8 +273,8 @@
         @param data: session data
         """
         log.warning(
-            u"Transfer {si_id} failed: {reason}".format(
-                reason=unicode(failure.value), **data
+            "Transfer {si_id} failed: {reason}".format(
+                reason=str(failure.value), **data
             )
         )
         data["stream_object"].close()
@@ -327,7 +327,7 @@
         try:
             feature_elt = self.host.plugins["XEP-0020"].getFeatureElt(si_elt)
         except exceptions.NotFound:
-            log.warning(u"No <feature/> element found in result while expected")
+            log.warning("No <feature/> element found in result while expected")
             return
 
         choosed_options = self.host.plugins["XEP-0020"].getChoosedOptions(
@@ -336,11 +336,11 @@
         try:
             stream_method = choosed_options["stream-method"]
         except KeyError:
-            log.warning(u"No stream method choosed")
+            log.warning("No stream method choosed")
             return
 
         try:
-            file_elt = si_elt.elements(NS_SI_FT, "file").next()
+            file_elt = next(si_elt.elements(NS_SI_FT, "file"))
         except StopIteration:
             pass
         else:
@@ -351,7 +351,7 @@
         elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
             plugin = self.host.plugins["XEP-0047"]
         else:
-            log.warning(u"Invalid stream method received")
+            log.warning("Invalid stream method received")
             return
 
         stream_object = stream.FileStreamObject(
@@ -367,25 +367,25 @@
             stanza_err = failure.value
             if stanza_err.code == "403" and stanza_err.condition == "forbidden":
                 from_s = stanza_err.stanza["from"]
-                log.info(u"File transfer refused by {}".format(from_s))
-                msg = D_(u"The contact {} has refused your file").format(from_s)
-                title = D_(u"File refused")
+                log.info("File transfer refused by {}".format(from_s))
+                msg = D_("The contact {} has refused your file").format(from_s)
+                title = D_("File refused")
                 xml_tools.quickNote(self.host, client, msg, title, C.XMLUI_DATA_LVL_INFO)
             else:
-                log.warning(_(u"Error during file transfer"))
+                log.warning(_("Error during file transfer"))
                 msg = D_(
-                    u"Something went wrong during the file transfer session initialisation: {reason}"
-                ).format(reason=unicode(stanza_err))
-                title = D_(u"File transfer error")
+                    "Something went wrong during the file transfer session initialisation: {reason}"
+                ).format(reason=str(stanza_err))
+                title = D_("File transfer error")
                 xml_tools.quickNote(self.host, client, msg, title, C.XMLUI_DATA_LVL_ERROR)
         elif failure.check(exceptions.DataError):
-            log.warning(u"Invalid stanza received")
+            log.warning("Invalid stanza received")
         else:
-            log.error(u"Error while proposing stream: {}".format(failure))
+            log.error("Error while proposing stream: {}".format(failure))
 
     def _sendCb(self, __, client, sid, stream_object):
         log.info(
-            _(u"transfer {sid} successfuly finished [{profile}]").format(
+            _("transfer {sid} successfuly finished [{profile}]").format(
                 sid=sid, profile=client.profile
             )
         )
@@ -393,8 +393,8 @@
 
     def _sendEb(self, failure, client, sid, stream_object):
         log.warning(
-            _(u"transfer {sid} failed [{profile}]: {reason}").format(
-                sid=sid, profile=client.profile, reason=unicode(failure.value)
+            _("transfer {sid} failed [{profile}]: {reason}").format(
+                sid=sid, profile=client.profile, reason=str(failure.value)
             )
         )
         stream_object.close()