diff sat/plugins/plugin_exp_list_of_interest.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 ef554aca3eb1
children 9d0df638c8b4
line wrap: on
line diff
--- a/sat/plugins/plugin_exp_list_of_interest.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_exp_list_of_interest.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 to detect language (experimental)
@@ -21,7 +21,7 @@
 from sat.core.constants import Const as C
 from sat.core.log import getLogger
 from wokkel import disco, iwokkel, pubsub
-from zope.interface import implements
+from zope.interface import implementer
 from twisted.internet import defer
 from twisted.words.protocols.jabber import error as jabber_error, jid
 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
@@ -35,11 +35,11 @@
     C.PI_IMPORT_NAME: "LIST_INTEREST",
     C.PI_TYPE: "EXP",
     C.PI_PROTOCOLS: [],
-    C.PI_DEPENDENCIES: [u"XEP-0060", u"XEP-0329"],
+    C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0329"],
     C.PI_RECOMMENDATIONS: [],
     C.PI_MAIN: "ListInterest",
     C.PI_HANDLER: "yes",
-    C.PI_DESCRIPTION: _(u"Experimental handling of interesting XMPP locations"),
+    C.PI_DESCRIPTION: _("Experimental handling of interesting XMPP locations"),
 }
 
 NS_LIST_INTEREST = "https://salut-a-toi/protocol/list-interest:0"
@@ -49,7 +49,7 @@
     namespace = NS_LIST_INTEREST
 
     def __init__(self, host):
-        log.info(_(u"List of Interest plugin initialization"))
+        log.info(_("List of Interest plugin initialization"))
         self.host = host
         self._p = self.host.plugins["XEP-0060"]
         host.bridge.addMethod(
@@ -58,7 +58,7 @@
             in_sign="ssss",
             out_sign="aa{ss}",
             method=self._listInterests,
-            async=True,
+            async_=True,
         )
 
     def getHandler(self, client):
@@ -76,8 +76,8 @@
                 options=options,
             )
         except jabber_error.StanzaError as e:
-            if e.condition == u"conflict":
-                log.debug(_(u"requested node already exists"))
+            if e.condition == "conflict":
+                log.debug(_("requested node already exists"))
 
     @defer.inlineCallbacks
     def registerPubsub(self, client, namespace, service, node, item_id=None,
@@ -101,20 +101,20 @@
         if extra is None:
             extra = {}
         yield self.createNode(client)
-        interest_elt = domish.Element((NS_LIST_INTEREST, u"interest"))
-        interest_elt[u"namespace"] = namespace
+        interest_elt = domish.Element((NS_LIST_INTEREST, "interest"))
+        interest_elt["namespace"] = namespace
         if name is not None:
-            interest_elt[u'name'] = name
-        thumb_url = extra.get(u'thumb_url')
+            interest_elt['name'] = name
+        thumb_url = extra.get('thumb_url')
         if thumb_url:
-            interest_elt[u'thumb_url'] = thumb_url
-        pubsub_elt = interest_elt.addElement(u"pubsub")
-        pubsub_elt[u"service"] = service.full()
-        pubsub_elt[u"node"] = node
+            interest_elt['thumb_url'] = thumb_url
+        pubsub_elt = interest_elt.addElement("pubsub")
+        pubsub_elt["service"] = service.full()
+        pubsub_elt["node"] = node
         if item_id is not None:
-            pubsub_elt[u"item"] = item_id
+            pubsub_elt["item"] = item_id
         if creator:
-            pubsub_elt[u"creator"] = C.BOOL_TRUE
+            pubsub_elt["creator"] = C.BOOL_TRUE
         if element is not None:
             pubsub_elt.addChild(element)
         item_elt = pubsub.Item(payload=interest_elt)
@@ -138,21 +138,21 @@
         if extra is None:
             extra = {}
         yield self.createNode(client)
-        interest_elt = domish.Element((NS_LIST_INTEREST, u"interest"))
-        interest_elt[u"namespace"] = self.host.getNamespace(u"fis")
+        interest_elt = domish.Element((NS_LIST_INTEREST, "interest"))
+        interest_elt["namespace"] = self.host.getNamespace("fis")
         if name is not None:
-            interest_elt[u'name'] = name
-        thumb_url = extra.get(u'thumb_url')
+            interest_elt['name'] = name
+        thumb_url = extra.get('thumb_url')
         if thumb_url:
-            interest_elt[u'thumb_url'] = thumb_url
-        file_sharing_elt = interest_elt.addElement(u"file_sharing")
-        file_sharing_elt[u"service"] = service.full()
+            interest_elt['thumb_url'] = thumb_url
+        file_sharing_elt = interest_elt.addElement("file_sharing")
+        file_sharing_elt["service"] = service.full()
         if repos_type is not None:
-            file_sharing_elt[u"type"] = repos_type
+            file_sharing_elt["type"] = repos_type
         if namespace is not None:
-            file_sharing_elt[u"namespace"] = namespace
+            file_sharing_elt["namespace"] = namespace
         if path is not None:
-            file_sharing_elt[u"path"] = path
+            file_sharing_elt["path"] = path
         item_elt = pubsub.Item(payload=interest_elt)
         yield self._p.publish(
             client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt]
@@ -163,38 +163,38 @@
         for item_elt in interests_data[0]:
             interest_data = {}
             interest_elt = item_elt.interest
-            if interest_elt.hasAttribute(u'namespace'):
-                interest_data[u'namespace'] = interest_elt.getAttribute(u'namespace')
-            if interest_elt.hasAttribute(u'name'):
-                interest_data[u'name'] = interest_elt.getAttribute(u'name')
-            if interest_elt.hasAttribute(u'thumb_url'):
-                interest_data[u'thumb_url'] = interest_elt.getAttribute(u'thumb_url')
+            if interest_elt.hasAttribute('namespace'):
+                interest_data['namespace'] = interest_elt.getAttribute('namespace')
+            if interest_elt.hasAttribute('name'):
+                interest_data['name'] = interest_elt.getAttribute('name')
+            if interest_elt.hasAttribute('thumb_url'):
+                interest_data['thumb_url'] = interest_elt.getAttribute('thumb_url')
             elt = interest_elt.firstChildElement()
             if elt.uri != NS_LIST_INTEREST:
-                log.warning(u"unexpected child element, ignoring: {xml}".format(
+                log.warning("unexpected child element, ignoring: {xml}".format(
                     xml = elt.toXml()))
                 continue
-            if elt.name == u'pubsub':
+            if elt.name == 'pubsub':
                 interest_data.update({
-                    u"type": u"pubsub",
-                    u"service": elt[u'service'],
-                    u"node": elt[u'node'],
+                    "type": "pubsub",
+                    "service": elt['service'],
+                    "node": elt['node'],
                 })
-                for attr in (u'item', u'creator'):
+                for attr in ('item', 'creator'):
                     if elt.hasAttribute(attr):
                         interest_data[attr] = elt[attr]
-            elif elt.name == u'file_sharing':
+            elif elt.name == 'file_sharing':
                 interest_data.update({
-                    u"type": u"file_sharing",
-                    u"service": elt[u'service'],
+                    "type": "file_sharing",
+                    "service": elt['service'],
                 })
-                if elt.hasAttribute(u'type'):
-                    interest_data[u'subtype'] = elt[u'type']
-                for attr in (u'namespace', u'path'):
+                if elt.hasAttribute('type'):
+                    interest_data['subtype'] = elt['type']
+                for attr in ('namespace', 'path'):
                     if elt.hasAttribute(attr):
                         interest_data[attr] = elt[attr]
             else:
-                log.warning(u"unknown element, ignoring: {xml}".format(xml=elt.toXml()))
+                log.warning("unknown element, ignoring: {xml}".format(xml=elt.toXml()))
                 continue
             interests.append(interest_data)
 
@@ -229,20 +229,20 @@
             filtered_items = []
             for item in items:
                 try:
-                    interest_elt = next(item.elements(NS_LIST_INTEREST, u"interest"))
+                    interest_elt = next(item.elements(NS_LIST_INTEREST, "interest"))
                 except StopIteration:
-                    log.warning(_(u"Missing interest element: {xml}").format(
+                    log.warning(_("Missing interest element: {xml}").format(
                         xml=interest_elt.toXml()))
                     continue
-                if interest_elt.getAttribute(u"namespace") == namespace:
+                if interest_elt.getAttribute("namespace") == namespace:
                     filtered_items.append(item)
             items = filtered_items
 
         defer.returnValue((items, metadata))
 
 
+@implementer(iwokkel.IDisco)
 class ListInterestHandler(XMPPHandler):
-    implements(iwokkel.IDisco)
 
     def __init__(self, plugin_parent):
         self.plugin_parent = plugin_parent