diff sat/plugins/plugin_xep_0231.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 003b8b4b56a7
children 9d0df638c8b4
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0231.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_xep_0231.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 Bit of Binary handling (XEP-0231)
@@ -25,7 +25,7 @@
 log = getLogger(__name__)
 from sat.tools import xml_tools
 from wokkel import disco, iwokkel
-from zope.interface import implements
+from zope.interface import implementer
 from twisted.python import failure
 from twisted.words.protocols.jabber import xmlstream
 from twisted.words.protocols.jabber import jid
@@ -49,13 +49,13 @@
     ),
 }
 
-NS_BOB = u"urn:xmpp:bob"
+NS_BOB = "urn:xmpp:bob"
 IQ_BOB_REQUEST = C.IQ_GET + '/data[@xmlns="' + NS_BOB + '"]'
 
 
 class XEP_0231(object):
     def __init__(self, host):
-        log.info(_(u"plugin Bits of Binary initialization"))
+        log.info(_("plugin Bits of Binary initialization"))
         self.host = host
         host.registerNamespace("bob", NS_BOB)
         host.trigger.add("xhtml_post_treat", self.XHTMLTrigger)
@@ -65,7 +65,7 @@
             in_sign="sss",
             out_sign="s",
             method=self._getFile,
-            async=True,
+            async_=True,
         )
 
     def dumpData(self, cache, data_elt, cid):
@@ -83,7 +83,7 @@
             if max_age < 0:
                 raise ValueError
         except (KeyError, ValueError):
-            log.warning(u"invalid max-age found")
+            log.warning("invalid max-age found")
             max_age = None
 
         with cache.cacheData(
@@ -99,13 +99,13 @@
         return XEP_0231_handler(self)
 
     def _requestCb(self, iq_elt, cache, cid):
-        for data_elt in iq_elt.elements(NS_BOB, u"data"):
+        for data_elt in iq_elt.elements(NS_BOB, "data"):
             if data_elt.getAttribute("cid") == cid:
                 file_path = self.dumpData(cache, data_elt, cid)
                 return file_path
 
         log.warning(
-            u"invalid data stanza received, requested cid was not found:\n{iq_elt}\nrequested cid: {cid}".format(
+            "invalid data stanza received, requested cid was not found:\n{iq_elt}\nrequested cid: {cid}".format(
                 iq_elt=iq_elt, cid=cid
             )
         )
@@ -113,7 +113,7 @@
 
     def _requestEb(self, failure_):
         """Log the error and continue errback chain"""
-        log.warning(u"Can't get requested data:\n{reason}".format(reason=failure_))
+        log.warning("Can't get requested data:\n{reason}".format(reason=failure_))
         return failure_
 
     def requestData(self, client, to_jid, cid, cache=None):
@@ -137,24 +137,24 @@
         return d
 
     def _setImgEltSrc(self, path, img_elt):
-        img_elt[u"src"] = u"file://{}".format(path)
+        img_elt["src"] = "file://{}".format(path)
 
     def XHTMLTrigger(self, client, message_elt, body_elt, lang, treat_d):
-        for img_elt in xml_tools.findAll(body_elt, C.NS_XHTML, u"img"):
-            source = img_elt.getAttribute(u"src", "")
-            if source.startswith(u"cid:"):
+        for img_elt in xml_tools.findAll(body_elt, C.NS_XHTML, "img"):
+            source = img_elt.getAttribute("src", "")
+            if source.startswith("cid:"):
                 cid = source[4:]
                 file_path = client.cache.getFilePath(cid)
                 if file_path is not None:
                     #  image is in cache, we change the url
-                    img_elt[u"src"] = u"file://{}".format(file_path)
+                    img_elt["src"] = "file://{}".format(file_path)
                     continue
                 else:
                     # image is not in cache, is it given locally?
-                    for data_elt in message_elt.elements(NS_BOB, u"data"):
+                    for data_elt in message_elt.elements(NS_BOB, "data"):
                         if data_elt.getAttribute("cid") == cid:
                             file_path = self.dumpData(client.cache, data_elt, cid)
-                            img_elt[u"src"] = u"file://{}".format(file_path)
+                            img_elt["src"] = "file://{}".format(file_path)
                             break
                     else:
                         # cid not found locally, we need to request it
@@ -172,7 +172,7 @@
         iq_elt.handled = True
         data_elt = next(iq_elt.elements(NS_BOB, "data"))
         try:
-            cid = data_elt[u"cid"]
+            cid = data_elt["cid"]
         except KeyError:
             error_elt = jabber_error.StanzaError("not-acceptable").toResponse(iq_elt)
             client.send(error_elt)
@@ -189,9 +189,9 @@
 
         result_elt = xmlstream.toResponse(iq_elt, "result")
         data_elt = result_elt.addElement((NS_BOB, "data"), content=data.encode("base64"))
-        data_elt[u"cid"] = cid
-        data_elt[u"type"] = metadata[u"mime_type"]
-        data_elt[u"max-age"] = unicode(int(max(0, metadata["eol"] - time.time())))
+        data_elt["cid"] = cid
+        data_elt["type"] = metadata["mime_type"]
+        data_elt["max-age"] = str(int(max(0, metadata["eol"] - time.time())))
         client.send(result_elt)
 
     def _getFile(self, peer_jid_s, cid, profile):
@@ -217,7 +217,7 @@
         else:
             # file not in cache, is it given locally?
             if parent_elt is not None:
-                for data_elt in parent_elt.elements(NS_BOB, u"data"):
+                for data_elt in parent_elt.elements(NS_BOB, "data"):
                     if data_elt.getAttribute("cid") == cid:
                         return defer.succeed(self.dumpData(client.cache, data_elt, cid))
 
@@ -226,8 +226,8 @@
             return self.requestData(client, peer_jid, cid)
 
 
+@implementer(iwokkel.IDisco)
 class XEP_0231_handler(xmlstream.XMPPHandler):
-    implements(iwokkel.IDisco)
 
     def __init__(self, plugin_parent):
         self.plugin_parent = plugin_parent