diff sat/plugins/plugin_xep_0363.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 8ce5748bfe97
children fee60f17ebac
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0363.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_xep_0363.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 HTTP File Upload (XEP-0363)
@@ -24,7 +24,7 @@
 log = getLogger(__name__)
 from sat.core import exceptions
 from wokkel import disco, iwokkel
-from zope.interface import implements
+from zope.interface import implementer
 from twisted.words.protocols.jabber import jid
 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
 from twisted.internet import reactor
@@ -50,7 +50,7 @@
     C.PI_DEPENDENCIES: ["FILE", "UPLOAD"],
     C.PI_MAIN: "XEP_0363",
     C.PI_HANDLER: "yes",
-    C.PI_DESCRIPTION: _(u"""Implementation of HTTP File Upload"""),
+    C.PI_DESCRIPTION: _("""Implementation of HTTP File Upload"""),
 }
 
 NS_HTTP_UPLOAD = "urn:xmpp:http:upload:0"
@@ -82,7 +82,7 @@
 
     def creatorForNetloc(self, hostname, port):
         log.warning(
-            u"TLS check disabled for {host} on port {port}".format(
+            "TLS check disabled for {host} on port {port}".format(
                 host=hostname, port=port
             )
         )
@@ -107,10 +107,10 @@
             in_sign="sisss",
             out_sign="(ss)",
             method=self._getSlot,
-            async=True,
+            async_=True,
         )
         host.plugins["UPLOAD"].register(
-            u"HTTP Upload", self.getHTTPUploadEntity, self.fileHTTPUpload
+            "HTTP Upload", self.getHTTPUploadEntity, self.fileHTTPUpload
         )
 
     def getHandler(self, client):
@@ -131,12 +131,12 @@
         except AttributeError:
             found_entities = yield self.host.findFeaturesSet(client, (NS_HTTP_UPLOAD,))
             try:
-                entity = client.http_upload_service = iter(found_entities).next()
+                entity = client.http_upload_service = next(iter(found_entities))
             except StopIteration:
                 entity = client.http_upload_service = None
 
         if entity is None:
-            raise failure.Failure(exceptions.NotFound(u"No HTTP upload entity found"))
+            raise failure.Failure(exceptions.NotFound("No HTTP upload entity found"))
 
         defer.returnValue(entity)
 
@@ -187,7 +187,7 @@
 
     def _getSlotEb(self, fail, client, progress_id_d, download_d):
         """an error happened while trying to get slot"""
-        log.warning(u"Can't get upload slot: {reason}".format(reason=fail.value))
+        log.warning("Can't get upload slot: {reason}".format(reason=fail.value))
         progress_id_d.errback(fail)
         download_d.errback(fail)
 
@@ -204,7 +204,7 @@
         @param ignore_tls_errors(bool): ignore TLS certificate is True
         @return (tuple
         """
-        log.debug(u"Got upload slot: {}".format(slot))
+        log.debug("Got upload slot: {}".format(slot))
         sat_file = self.host.plugins["FILE"].File(
             self.host, client, path, size=size, auto_end_signals=False
         )
@@ -243,7 +243,7 @@
             should be closed, be is needed to send the progressFinished signal
         @param slot(Slot): put/get urls
         """
-        log.info(u"HTTP upload finished")
+        log.info("HTTP upload finished")
         sat_file.progressFinished({"url": slot.get})
         download_d.callback(slot.get)
 
@@ -257,15 +257,15 @@
         try:
             wrapped_fail = fail.value.reasons[0]
         except (AttributeError, IndexError) as e:
-            log.warning(_(u"upload failed: {reason}").format(reason=e))
-            sat_file.progressError(unicode(fail))
+            log.warning(_("upload failed: {reason}").format(reason=e))
+            sat_file.progressError(str(fail))
             raise fail
         else:
             if wrapped_fail.check(SSL.Error):
-                msg = u"TLS validation error, can't connect to HTTPS server"
+                msg = "TLS validation error, can't connect to HTTPS server"
             else:
-                msg = u"can't upload file"
-            log.warning(msg + ": " + unicode(wrapped_fail.value))
+                msg = "can't upload file"
+            log.warning(msg + ": " + str(wrapped_fail.value))
             sat_file.progressError(msg)
 
     def _gotSlot(self, iq_elt, client):
@@ -275,26 +275,26 @@
         @param iq_elt(domish.Element): <IQ/> result as specified in XEP-0363
         """
         try:
-            slot_elt = iq_elt.elements(NS_HTTP_UPLOAD, "slot").next()
-            put_elt = slot_elt.elements(NS_HTTP_UPLOAD, "put").next()
+            slot_elt = next(iq_elt.elements(NS_HTTP_UPLOAD, "slot"))
+            put_elt = next(slot_elt.elements(NS_HTTP_UPLOAD, "put"))
             put_url = put_elt['url']
-            get_elt = slot_elt.elements(NS_HTTP_UPLOAD, "get").next()
+            get_elt = next(slot_elt.elements(NS_HTTP_UPLOAD, "get"))
             get_url = get_elt['url']
         except (StopIteration, KeyError):
-            raise exceptions.DataError(u"Incorrect stanza received from server")
+            raise exceptions.DataError("Incorrect stanza received from server")
         headers = []
         for header_elt in put_elt.elements(NS_HTTP_UPLOAD, "header"):
             try:
                 name = header_elt["name"]
-                value = unicode(header_elt)
+                value = str(header_elt)
             except KeyError:
-                log.warning(_(u"Invalid header element: {xml}").format(
+                log.warning(_("Invalid header element: {xml}").format(
                     iq_elt.toXml()))
                 continue
             name = name.replace('\n', '')
             value = value.replace('\n', '')
             if name.lower() not in ALLOWED_HEADERS:
-                log.warning(_(u'Ignoring unauthorised header "{name}": {xml}')
+                log.warning(_('Ignoring unauthorised header "{name}": {xml}')
                     .format(name=name, xml = iq_elt.toXml()))
                 continue
             headers.append((name, value))
@@ -351,14 +351,14 @@
             else:
                 if upload_jid is None:
                     raise failure.Failure(
-                        exceptions.NotFound(u"No HTTP upload entity found")
+                        exceptions.NotFound("No HTTP upload entity found")
                     )
 
         iq_elt = client.IQ("get")
         iq_elt["to"] = upload_jid.full()
         request_elt = iq_elt.addElement((NS_HTTP_UPLOAD, "request"))
         request_elt["filename"] = filename
-        request_elt["size"] = unicode(size)
+        request_elt["size"] = str(size)
         if content_type is not None:
             request_elt["content-type"] = content_type
 
@@ -368,8 +368,8 @@
         return d
 
 
+@implementer(iwokkel.IDisco)
 class XEP_0363_handler(XMPPHandler):
-    implements(iwokkel.IDisco)
 
     def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
         return [disco.DiscoFeature(NS_HTTP_UPLOAD)]