diff sat_pubsub/pubsub_admin.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 412d26a9b2c2
line wrap: on
line diff
--- a/sat_pubsub/pubsub_admin.py	Fri Aug 16 12:48:34 2019 +0200
+++ b/sat_pubsub/pubsub_admin.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) 2019 Jérôme Poisson
@@ -21,7 +21,7 @@
 
 """
 
-from zope.interface import implements
+from zope.interface import implementer
 from twisted.python import log
 from twisted.internet import defer
 from twisted.words.protocols.jabber import jid, error as jabber_error, xmlstream
@@ -29,12 +29,12 @@
 from wokkel.subprotocols import XMPPHandler
 from wokkel import disco, iwokkel, pubsub
 
-NS_PUBSUB_ADMIN = u"https://salut-a-toi.org/spec/pubsub_admin:0"
+NS_PUBSUB_ADMIN = "https://salut-a-toi.org/spec/pubsub_admin:0"
 ADMIN_REQUEST = '/iq[@type="set"]/admin[@xmlns="{}"]'.format(NS_PUBSUB_ADMIN)
 
 
+@implementer(iwokkel.IDisco)
 class PubsubAdminHandler(XMPPHandler):
-    implements(iwokkel.IDisco)
 
     def __init__(self, backend):
         super(PubsubAdminHandler, self).__init__()
@@ -43,7 +43,7 @@
     def connectionInitialized(self):
         self.xmlstream.addObserver(ADMIN_REQUEST, self.onAdminRequest)
 
-    def sendError(self, iq_elt, condition=u'bad-request'):
+    def sendError(self, iq_elt, condition='bad-request'):
         stanza_error = jabber_error.StanzaError(condition)
         iq_error = stanza_error.toResponse(iq_elt)
         self.parent.xmlstream.send(iq_error)
@@ -58,42 +58,42 @@
             pep = False
 
         # is the sender really an admin?
-        admins = self.backend.config[u'admins_jids_list']
-        from_jid = jid.JID(iq_elt[u'from'])
+        admins = self.backend.config['admins_jids_list']
+        from_jid = jid.JID(iq_elt['from'])
         if from_jid.userhostJID() not in admins:
             log.msg("WARNING: admin request done by non admin entity {from_jid}"
                 .format(from_jid=from_jid.full()))
-            self.sendError(iq_elt, u'forbidden')
+            self.sendError(iq_elt, 'forbidden')
             return
 
         # alright, we can proceed
-        recipient = jid.JID(iq_elt[u'to'])
+        recipient = jid.JID(iq_elt['to'])
         admin_elt = iq_elt.admin
         try:
-            pubsub_elt = next(admin_elt.elements(pubsub.NS_PUBSUB, u'pubsub'))
-            publish_elt = next(pubsub_elt.elements(pubsub.NS_PUBSUB, u'publish'))
+            pubsub_elt = next(admin_elt.elements(pubsub.NS_PUBSUB, 'pubsub'))
+            publish_elt = next(pubsub_elt.elements(pubsub.NS_PUBSUB, 'publish'))
         except StopIteration:
             self.sendError(iq_elt)
             return
         try:
-            node = publish_elt[u'node']
+            node = publish_elt['node']
         except KeyError:
             self.sendError(iq_elt)
             return
 
         # we prepare the result IQ request, we will fill it with item ids
-        iq_result_elt = xmlstream.toResponse(iq_elt, u'result')
-        result_admin_elt = iq_result_elt.addElement((NS_PUBSUB_ADMIN, u'admin'))
-        result_pubsub_elt = result_admin_elt.addElement((pubsub.NS_PUBSUB, u'pubsub'))
-        result_publish_elt = result_pubsub_elt.addElement(u'publish')
-        result_publish_elt[u'node'] = node
+        iq_result_elt = xmlstream.toResponse(iq_elt, 'result')
+        result_admin_elt = iq_result_elt.addElement((NS_PUBSUB_ADMIN, 'admin'))
+        result_pubsub_elt = result_admin_elt.addElement((pubsub.NS_PUBSUB, 'pubsub'))
+        result_publish_elt = result_pubsub_elt.addElement('publish')
+        result_publish_elt['node'] = node
 
         # now we can send the items
-        for item in publish_elt.elements(pubsub.NS_PUBSUB, u'item'):
+        for item in publish_elt.elements(pubsub.NS_PUBSUB, 'item'):
             try:
-                requestor = jid.JID(item.attributes.pop(u'publisher'))
+                requestor = jid.JID(item.attributes.pop('publisher'))
             except Exception as e:
-                log.msg(u"WARNING: invalid jid in publisher ({requestor}): {msg}"
+                log.msg("WARNING: invalid jid in publisher ({requestor}): {msg}"
                     .format(requestor=requestor, msg=e))
                 self.sendError(iq_elt)
                 return
@@ -111,20 +111,20 @@
                     recipient=recipient)
             except (error.Forbidden, error.ItemForbidden):
                 __import__('pudb').set_trace()
-                self.sendError(iq_elt, u"forbidden")
+                self.sendError(iq_elt, "forbidden")
                 return
             except Exception as e:
-                self.sendError(iq_elt, u"internal-server-error")
-                log.msg(u"INTERNAL ERROR: {msg}".format(msg=e))
+                self.sendError(iq_elt, "internal-server-error")
+                log.msg("INTERNAL ERROR: {msg}".format(msg=e))
                 return
 
-            result_item_elt = result_publish_elt.addElement(u'item')
+            result_item_elt = result_publish_elt.addElement('item')
             # either the id was given and it is available in item
             # either it's a new item, and we can retrieve it from return payload
             try:
-                result_item_elt[u'id'] = item[u'id']
+                result_item_elt['id'] = item['id']
             except KeyError:
-                result_item_elt = payload.publish.item[u'id']
+                result_item_elt = payload.publish.item['id']
 
         self.xmlstream.send(iq_result_elt)