diff sat/plugins/plugin_xep_0071.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 85d3240a400f
children 9d0df638c8b4
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0071.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_xep_0071.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 Publish-Subscribe (xep-0071)
@@ -27,14 +27,14 @@
 
 from twisted.internet import defer
 from wokkel import disco, iwokkel
-from zope.interface import implements
+from zope.interface import implementer
 
 # from lxml import etree
 try:
     from lxml import html
 except ImportError:
     raise exceptions.MissingModule(
-        u"Missing module lxml, please download/install it from http://lxml.de/"
+        "Missing module lxml, please download/install it from http://lxml.de/"
     )
 try:
     from twisted.words.protocols.xmlstream import XMPPHandler
@@ -176,14 +176,14 @@
 
         syntax = self._s.getCurrentSyntax(client.profile)
         defers = []
-        if u"xhtml" in data["extra"]:
+        if "xhtml" in data["extra"]:
             # we have directly XHTML
             for lang, xhtml in data_format.getSubDict("xhtml", data["extra"]):
                 self._check_body_text(data, lang, xhtml, self._s.SYNTAX_XHTML, defers)
                 d = self._s.convert(xhtml, self._s.SYNTAX_XHTML, self.SYNTAX_XHTML_IM)
                 d.addCallback(syntax_converted, lang)
                 defers.append(d)
-        elif u"rich" in data["extra"]:
+        elif "rich" in data["extra"]:
             # we have rich syntax to convert
             for lang, rich_data in data_format.getSubDict("rich", data["extra"]):
                 self._check_body_text(data, lang, rich_data, syntax, defers)
@@ -191,7 +191,7 @@
                 d.addCallback(syntax_converted, lang)
                 defers.append(d)
         else:
-            exceptions.InternalError(u"xhtml or rich should be present at this point")
+            exceptions.InternalError("xhtml or rich should be present at this point")
         d_list = defer.DeferredList(defers)
         d_list.addCallback(lambda __: data)
         return d_list
@@ -200,7 +200,7 @@
         """ Check presence of XHTML-IM in message
         """
         try:
-            html_elt = message.elements(NS_XHTML_IM, "html").next()
+            html_elt = next(message.elements(NS_XHTML_IM, "html"))
         except StopIteration:
             # No XHTML-IM
             pass
@@ -213,14 +213,14 @@
         """ Check presence of rich text in extra """
         rich = {}
         xhtml = {}
-        for key, value in data["extra"].iteritems():
+        for key, value in data["extra"].items():
             if key.startswith("rich"):
                 rich[key[5:]] = value
             elif key.startswith("xhtml"):
                 xhtml[key[6:]] = value
         if rich and xhtml:
             raise exceptions.DataError(
-                _(u"Can't have XHTML and rich content at the same time")
+                _("Can't have XHTML and rich content at the same time")
             )
         if rich or xhtml:
             if rich:
@@ -247,7 +247,7 @@
                 continue
             purged.append((name, value.strip()))
 
-        return u"; ".join([u"%s: %s" % data for data in purged])
+        return "; ".join(["%s: %s" % data for data in purged])
 
     def XHTML2XHTML_IM(self, xhtml):
         """ Convert XHTML document to XHTML_IM subset
@@ -265,7 +265,7 @@
         else:
             body_elt.attrib.clear()
 
-        allowed_tags = allowed.keys()
+        allowed_tags = list(allowed.keys())
         to_strip = []
         for elem in body_elt.iter():
             if elem.tag not in allowed_tags:
@@ -282,7 +282,7 @@
         for elem in to_strip:
             if elem.tag in blacklist:
                 # we need to remove the element and all descendants
-                log.debug(u"removing black listed tag: %s" % (elem.tag))
+                log.debug("removing black listed tag: %s" % (elem.tag))
                 elem.drop_tree()
             else:
                 elem.drop_tag()
@@ -295,8 +295,8 @@
         return html.tostring(root_elt, encoding="unicode", method="xml")
 
 
+@implementer(iwokkel.IDisco)
 class XEP_0071_handler(XMPPHandler):
-    implements(iwokkel.IDisco)
 
     def __init__(self, plugin_parent):
         self.plugin_parent = plugin_parent