diff sat_pubsub/delegation.py @ 414:ccb2a22ea0fc

Python 3 port: /!\ Python 3.6+ is now needed to use SàT Pubsub /!\ instability may occur and features may not be working anymore, this will improve with time The same procedure as in backend has been applied (check backend commit ab2696e34d29 logs for details). Python minimal version has been updated in setup.py
author Goffi <goffi@goffi.org>
date Fri, 16 Aug 2019 12:53:33 +0200
parents c56a728412f1
children cebcb7f56889
line wrap: on
line diff
--- a/sat_pubsub/delegation.py	Fri Aug 16 12:48:34 2019 +0200
+++ b/sat_pubsub/delegation.py	Fri Aug 16 12:53:33 2019 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #-*- coding: utf-8 -*-
 #
 # Copyright (c) 2015 Jérôme Poisson
@@ -31,7 +31,7 @@
 from twisted.words.protocols.jabber import jid, error
 from twisted.words.protocols.jabber.xmlstream import toResponse
 from twisted.words.xish import domish
-from zope.interface import implements
+from zope.interface import implementer
 
 DELEGATION_NS = 'urn:xmpp:delegation:1'
 FORWARDED_NS = 'urn:xmpp:forward:0'
@@ -49,8 +49,8 @@
     pass
 
 
+@implementer(iwokkel.IDisco)
 class DelegationsHandler(XMPPHandler):
-    implements(iwokkel.IDisco)
     _service_hacked = False
 
     def __init__(self):
@@ -144,32 +144,32 @@
 
     def onAdvertise(self, message):
         """Manage the <message/> advertising delegations"""
-        delegation_elt = message.elements(DELEGATION_NS, 'delegation').next()
+        delegation_elt = next(message.elements(DELEGATION_NS, 'delegation'))
         delegated = {}
         for delegated_elt in delegation_elt.elements(DELEGATION_NS):
             try:
                 if delegated_elt.name != 'delegated':
-                    raise InvalidStanza(u'unexpected element {}'.format(delegated_elt.name))
+                    raise InvalidStanza('unexpected element {}'.format(delegated_elt.name))
                 try:
                     namespace = delegated_elt['namespace']
                 except KeyError:
-                    raise InvalidStanza(u'was expecting a "namespace" attribute in delegated element')
+                    raise InvalidStanza('was expecting a "namespace" attribute in delegated element')
                 delegated[namespace] = []
                 for attribute_elt in delegated_elt.elements(DELEGATION_NS, 'attribute'):
                     try:
                         delegated[namespace].append(attribute_elt["name"])
                     except KeyError:
-                        raise InvalidStanza(u'was expecting a "name" attribute in attribute element')
+                        raise InvalidStanza('was expecting a "name" attribute in attribute element')
             except InvalidStanza as e:
                 log.msg("Invalid stanza received ({})".format(e))
 
-        log.msg(u'delegations updated:\n{}'.format(
-            u'\n'.join([u"    - namespace {}{}".format(ns,
-            u"" if not attributes else u" with filtering on {} attribute(s)".format(
-            u", ".join(attributes))) for ns, attributes in delegated.items()])))
+        log.msg('delegations updated:\n{}'.format(
+            '\n'.join(["    - namespace {}{}".format(ns,
+            "" if not attributes else " with filtering on {} attribute(s)".format(
+            ", ".join(attributes))) for ns, attributes in list(delegated.items())])))
 
         if not pubsub.NS_PUBSUB in delegated:
-            log.msg(u"Didn't got pubsub delegation from server, can't act as a PEP service")
+            log.msg("Didn't got pubsub delegation from server, can't act as a PEP service")
 
     def onForward(self, iq):
         """Manage forwarded iq
@@ -183,14 +183,14 @@
         # TODO: do proper origin security check
         _, allowed = iq['to'].split('.', 1)
         if jid.JID(iq['from']) != jid.JID(allowed):
-            log.msg((u"SECURITY WARNING: forwarded stanza doesn't come from our server: {}"
+            log.msg(("SECURITY WARNING: forwarded stanza doesn't come from our server: {}"
                      .format(iq.toXml())).encode('utf-8'))
             raise error.StanzaError('not-allowed')
 
         try:
-            fwd_iq = (iq.elements(DELEGATION_NS, 'delegation').next()
-                      .elements(FORWARDED_NS, 'forwarded').next()
-                      .elements('jabber:client', 'iq').next())
+            delegation_elt = next(iq.elements(DELEGATION_NS, 'delegation'))
+            forwarded_elt = next(delegation_elt.elements(FORWARDED_NS, 'forwarded'))
+            fwd_iq = next(forwarded_elt.elements('jabber:client', 'iq'))
         except StopIteration:
             raise error.StanzaError('not-acceptable')