diff sat_pubsub/backend.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 4179ed660a85
line wrap: on
line diff
--- a/sat_pubsub/backend.py	Fri Aug 16 12:48:34 2019 +0200
+++ b/sat_pubsub/backend.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) 2012-2019 Jérôme Poisson
@@ -64,7 +64,7 @@
 import copy
 import uuid
 
-from zope.interface import implements
+from zope.interface import implementer
 
 from twisted.application import service
 from twisted.python import components, log
@@ -123,6 +123,7 @@
                               + [copy.deepcopy(d) for d in item_data[1:]])
 
 
+@implementer(iidavoll.IBackendService)
 class BackendService(service.Service, utility.EventDispatcher):
     """
     Generic publish-subscribe backend service.
@@ -134,7 +135,6 @@
     @cvar defaultConfig: The default node configuration.
     """
 
-    implements(iidavoll.IBackendService)
 
     nodeOptions = {
             const.OPT_PERSIST_ITEMS:
@@ -201,7 +201,7 @@
         self.storage = storage
         self._callbackList = []
         self.config = config
-        self.admins = config[u'admins_jids_list']
+        self.admins = config['admins_jids_list']
 
     def isAdmin(self, entity_jid):
         """Return True if an entity is an administrator"""
@@ -255,7 +255,7 @@
 
     def _makeMetaData(self, metaData):
         options = []
-        for key, value in metaData.iteritems():
+        for key, value in metaData.items():
             if key in self.nodeOptions:
                 option = {"var": key}
                 option.update(self.nodeOptions[key])
@@ -324,7 +324,7 @@
         """
         categories = []
         try:
-            entry_elt = item_elt.elements(const.NS_ATOM, "entry").next()
+            entry_elt = next(item_elt.elements(const.NS_ATOM, "entry"))
         except StopIteration:
             return categories
 
@@ -373,7 +373,7 @@
 
         # we now remove every field which is not in data schema
         to_remove = set()
-        for item_var, item_field in item_form.fields.iteritems():
+        for item_var, item_field in item_form.fields.items():
             if item_var not in schema_form.fields:
                 to_remove.add(item_field)
 
@@ -387,7 +387,7 @@
         current publisher must correspond to each item publisher
         """
         def doCheck(item_pub_map):
-            for item_publisher in item_pub_map.itervalues():
+            for item_publisher in item_pub_map.values():
                 if item_publisher.userhost() != publisher.userhost():
                     raise error.ItemForbidden()
 
@@ -431,13 +431,13 @@
                     item["id"] = yield node.getNextId()
                     new_item = True
                     if ret_payload is None:
-                        ret_pubsub_elt = domish.Element((pubsub.NS_PUBSUB, u'pubsub'))
-                        ret_publish_elt = ret_pubsub_elt.addElement(u'publish')
-                        ret_publish_elt[u'node'] = node.nodeIdentifier
+                        ret_pubsub_elt = domish.Element((pubsub.NS_PUBSUB, 'pubsub'))
+                        ret_publish_elt = ret_pubsub_elt.addElement('publish')
+                        ret_publish_elt['node'] = node.nodeIdentifier
                         ret_payload = ret_pubsub_elt
                     ret_publish_elt = ret_payload.publish
-                    ret_item_elt = ret_publish_elt.addElement(u'item')
-                    ret_item_elt["id"] = item[u"id"]
+                    ret_item_elt = ret_publish_elt.addElement('item')
+                    ret_item_elt["id"] = item["id"]
                 else:
                     check_overwrite = True
                     new_item = False
@@ -462,9 +462,9 @@
                             # TODO: handle multiple items publishing (from several
                             #       publishers)
                             raise error.NoPublishing(
-                                u"consistent_publisher is currently only possible when "
-                                u"publishing items from a single publisher. Try to "
-                                u"publish one item at a time")
+                                "consistent_publisher is currently only possible when "
+                                "publishing items from a single publisher. Try to "
+                                "publish one item at a time")
                         # we replace requestor and new payload's publisher by original
                         # item publisher to keep publisher consistent
                         requestor = publishers.pop()
@@ -506,7 +506,7 @@
 
             notifications = [(subscriber, subscriptions_, items_data)
                              for subscriber, subscriptions_
-                             in subsBySubscriber.iteritems()]
+                             in subsBySubscriber.items()]
 
             return notifications
 
@@ -762,7 +762,7 @@
             #        if at least one other entity is owner for this node
             raise error.Forbidden("You can't change your own affiliation")
 
-        to_delete = [jid_ for jid_, affiliation in affiliations.iteritems() if affiliation == 'none']
+        to_delete = [jid_ for jid_, affiliation in affiliations.items() if affiliation == 'none']
         for jid_ in to_delete:
             del affiliations[jid_]
 
@@ -960,7 +960,7 @@
         elif access_model == const.VAL_AMODEL_WHITELIST:
             yield self.checkNodeAffiliations(node, requestor)
         else:
-            raise Exception(u"Unknown access_model")
+            raise Exception("Unknown access_model")
 
         defer.returnValue((affiliation, owner, roster, access_model))
 
@@ -1154,7 +1154,7 @@
             """
             # TODO: the behaviour should be configurable (per node ?)
             if (any((requestor.userhostJID() != publisher.userhostJID()
-                    for publisher in publishers_map.itervalues()))
+                    for publisher in publishers_map.values()))
                 and not self.isAdmin(requestor)
                ):
                 raise error.Forbidden()
@@ -1278,7 +1278,7 @@
 
     discoIdentity = disco.DiscoIdentity('pubsub',
                                         'service',
-                                        u'Salut à Toi pubsub service')
+                                        'Salut à Toi pubsub service')
 
     pubsubService = None
 
@@ -1523,7 +1523,7 @@
         return d
 
     def _mapErrors(self, failure):
-        e = failure.trap(*self._errorMap.keys())
+        e = failure.trap(*list(self._errorMap.keys()))
 
         condition, pubsubCondition, feature = self._errorMap[e]
         msg = failure.value.msg
@@ -1601,7 +1601,7 @@
 
     def _publish_errb(self, failure, request):
         if failure.type == error.NodeNotFound and self.backend.supportsAutoCreate():
-            print "Auto-creating node %s" % (request.nodeIdentifier,)
+            print("Auto-creating node %s" % (request.nodeIdentifier,))
             d = self.backend.createNode(request.nodeIdentifier,
                                         request.sender,
                                         pep=self._isPep(request),
@@ -1780,8 +1780,8 @@
 
 
 
+@implementer(iwokkel.IDisco)
 class ExtraDiscoHandler(XMPPHandler):
-    implements(iwokkel.IDisco)
     # see comment in twisted/plugins/pubsub.py
     # FIXME: upstream must be fixed so we can use custom (non pubsub#) disco features