diff sat/plugins/plugin_exp_events.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 83cbd4545274
children fee60f17ebac
line wrap: on
line diff
--- a/sat/plugins/plugin_exp_events.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_exp_events.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)
@@ -29,7 +29,7 @@
 from twisted.words.protocols.jabber import jid, error
 from twisted.words.xish import domish
 from wokkel import disco, iwokkel
-from zope.interface import implements
+from zope.interface import implementer
 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
 from wokkel import pubsub
 
@@ -41,11 +41,11 @@
     C.PI_IMPORT_NAME: "EVENTS",
     C.PI_TYPE: "EXP",
     C.PI_PROTOCOLS: [],
-    C.PI_DEPENDENCIES: [u"XEP-0060", u"INVITATION", u"LIST_INTEREST"],
+    C.PI_DEPENDENCIES: ["XEP-0060", "INVITATION", "LIST_INTEREST"],
     C.PI_RECOMMENDATIONS: ["XEP-0277", "EMAIL_INVITATION"],
     C.PI_MAIN: "Events",
     C.PI_HANDLER: "yes",
-    C.PI_DESCRIPTION: _(u"""Experimental implementation of XMPP events management"""),
+    C.PI_DESCRIPTION: _("""Experimental implementation of XMPP events management"""),
 }
 
 NS_EVENT = "org.salut-a-toi.event:0"
@@ -55,13 +55,13 @@
     """Q&D module to handle event attendance answer, experimentation only"""
 
     def __init__(self, host):
-        log.info(_(u"Event plugin initialization"))
+        log.info(_("Event plugin initialization"))
         self.host = host
         self._p = self.host.plugins["XEP-0060"]
         self._i = self.host.plugins.get("EMAIL_INVITATION")
         self._b = self.host.plugins.get("XEP-0277")
-        self.host.registerNamespace(u"event", NS_EVENT)
-        self.host.plugins[u"INVITATION"].registerNamespace(NS_EVENT,
+        self.host.registerNamespace("event", NS_EVENT)
+        self.host.plugins["INVITATION"].registerNamespace(NS_EVENT,
                                                            self.register)
         host.bridge.addMethod(
             "eventGet",
@@ -69,7 +69,7 @@
             in_sign="ssss",
             out_sign="(ia{ss})",
             method=self._eventGet,
-            async=True,
+            async_=True,
         )
         host.bridge.addMethod(
             "eventCreate",
@@ -77,7 +77,7 @@
             in_sign="ia{ss}ssss",
             out_sign="s",
             method=self._eventCreate,
-            async=True,
+            async_=True,
         )
         host.bridge.addMethod(
             "eventModify",
@@ -85,7 +85,7 @@
             in_sign="sssia{ss}s",
             out_sign="",
             method=self._eventModify,
-            async=True,
+            async_=True,
         )
         host.bridge.addMethod(
             "eventsList",
@@ -93,7 +93,7 @@
             in_sign="sss",
             out_sign="aa{ss}",
             method=self._eventsList,
-            async=True,
+            async_=True,
         )
         host.bridge.addMethod(
             "eventInviteeGet",
@@ -101,7 +101,7 @@
             in_sign="sss",
             out_sign="a{ss}",
             method=self._eventInviteeGet,
-            async=True,
+            async_=True,
         )
         host.bridge.addMethod(
             "eventInviteeSet",
@@ -109,7 +109,7 @@
             in_sign="ssa{ss}s",
             out_sign="",
             method=self._eventInviteeSet,
-            async=True,
+            async_=True,
         )
         host.bridge.addMethod(
             "eventInviteesList",
@@ -117,7 +117,7 @@
             in_sign="sss",
             out_sign="a{sa{ss}}",
             method=self._eventInviteesList,
-            async=True,
+            async_=True,
         ),
         host.bridge.addMethod(
             "eventInvite",
@@ -125,7 +125,7 @@
             in_sign="sssss",
             out_sign="",
             method=self._invite,
-            async=True,
+            async_=True,
         )
         host.bridge.addMethod(
             "eventInviteByEmail",
@@ -133,7 +133,7 @@
             in_sign="ssssassssssss",
             out_sign="",
             method=self._inviteByEmail,
-            async=True,
+            async_=True,
         )
 
     def getHandler(self, client):
@@ -152,56 +152,56 @@
 
         data = {}
 
-        for key in (u"name",):
+        for key in ("name",):
             try:
                 data[key] = event_elt[key]
             except KeyError:
                 continue
 
-        for elt_name in (u"description",):
+        for elt_name in ("description",):
             try:
                 elt = next(event_elt.elements(NS_EVENT, elt_name))
             except StopIteration:
                 continue
             else:
-                data[elt_name] = unicode(elt)
+                data[elt_name] = str(elt)
 
-        for elt_name in (u"image", "background-image"):
+        for elt_name in ("image", "background-image"):
             try:
                 image_elt = next(event_elt.elements(NS_EVENT, elt_name))
                 data[elt_name] = image_elt["src"]
             except StopIteration:
                 continue
             except KeyError:
-                log.warning(_(u"no src found for image"))
+                log.warning(_("no src found for image"))
 
-        for uri_type in (u"invitees", u"blog"):
+        for uri_type in ("invitees", "blog"):
             try:
                 elt = next(event_elt.elements(NS_EVENT, uri_type))
-                uri = data[uri_type + u"_uri"] = elt["uri"]
+                uri = data[uri_type + "_uri"] = elt["uri"]
                 uri_data = xmpp_uri.parseXMPPUri(uri)
-                if uri_data[u"type"] != u"pubsub":
+                if uri_data["type"] != "pubsub":
                     raise ValueError
             except StopIteration:
-                log.warning(_(u"no {uri_type} element found!").format(uri_type=uri_type))
+                log.warning(_("no {uri_type} element found!").format(uri_type=uri_type))
             except KeyError:
-                log.warning(_(u"incomplete {uri_type} element").format(uri_type=uri_type))
+                log.warning(_("incomplete {uri_type} element").format(uri_type=uri_type))
             except ValueError:
-                log.warning(_(u"bad {uri_type} element").format(uri_type=uri_type))
+                log.warning(_("bad {uri_type} element").format(uri_type=uri_type))
             else:
-                data[uri_type + u"_service"] = uri_data[u"path"]
-                data[uri_type + u"_node"] = uri_data[u"node"]
+                data[uri_type + "_service"] = uri_data["path"]
+                data[uri_type + "_node"] = uri_data["node"]
 
         for meta_elt in event_elt.elements(NS_EVENT, "meta"):
-            key = meta_elt[u"name"]
+            key = meta_elt["name"]
             if key in data:
                 log.warning(
-                    u"Ignoring conflicting meta element: {xml}".format(
+                    "Ignoring conflicting meta element: {xml}".format(
                         xml=meta_elt.toXml()
                     )
                 )
                 continue
-            data[key] = unicode(meta_elt)
+            data[key] = str(meta_elt)
         if event_elt.link:
             link_elt = event_elt.link
             data["service"] = link_elt["service"]
@@ -225,11 +225,11 @@
             id_ = NS_EVENT
         items, metadata = yield self._p.getItems(client, service, node, item_ids=[id_])
         try:
-            event_elt = next(items[0].elements(NS_EVENT, u"event"))
+            event_elt = next(items[0].elements(NS_EVENT, "event"))
         except StopIteration:
-            raise exceptions.NotFound(_(u"No event element has been found"))
+            raise exceptions.NotFound(_("No event element has been found"))
         except IndexError:
-            raise exceptions.NotFound(_(u"No event with this id has been found"))
+            raise exceptions.NotFound(_("No event with this id has been found"))
         defer.returnValue(event_elt)
 
     def register(self, client, name, extra, service, node, event_id, item_elt,
@@ -249,16 +249,16 @@
         link_elt["node"] = node
         link_elt["item"] = event_id
         __, event_data = self._parseEventElt(event_elt)
-        name = event_data.get(u'name')
-        if u'image' in event_data:
-            extra = {u'thumb_url': event_data[u'image']}
+        name = event_data.get('name')
+        if 'image' in event_data:
+            extra = {'thumb_url': event_data['image']}
         else:
             extra = None
-        return self.host.plugins[u'LIST_INTEREST'].registerPubsub(
+        return self.host.plugins['LIST_INTEREST'].registerPubsub(
             client, NS_EVENT, service, node, event_id, creator,
             name=name, element=event_elt, extra=extra)
 
-    def _eventGet(self, service, node, id_=u"", profile_key=C.PROF_KEY_NONE):
+    def _eventGet(self, service, node, id_="", profile_key=C.PROF_KEY_NONE):
         service = jid.JID(service) if service else None
         node = node if node else NS_EVENT
         client = self.host.getClient(profile_key)
@@ -283,12 +283,12 @@
         defer.returnValue(self._parseEventElt(event_elt))
 
     def _eventCreate(
-        self, timestamp, data, service, node, id_=u"", profile_key=C.PROF_KEY_NONE
+        self, timestamp, data, service, node, id_="", profile_key=C.PROF_KEY_NONE
     ):
         service = jid.JID(service) if service else None
         node = node or None
         client = self.host.getClient(profile_key)
-        data[u"register"] = C.bool(data.get(u"register", C.BOOL_FALSE))
+        data["register"] = C.bool(data.get("register", C.BOOL_FALSE))
         return self.eventCreate(client, timestamp, data, service, node, id_ or NS_EVENT)
 
     @defer.inlineCallbacks
@@ -311,32 +311,32 @@
         @return (unicode): created node
         """
         if not event_id:
-            raise ValueError(_(u"event_id must be set"))
+            raise ValueError(_("event_id must be set"))
         if not service:
             service = client.jid.userhostJID()
         if not node:
-            node = NS_EVENT + u"__" + shortuuid.uuid()
+            node = NS_EVENT + "__" + shortuuid.uuid()
         event_elt = domish.Element((NS_EVENT, "event"))
         if timestamp is not None and timestamp != -1:
             formatted_date = utils.xmpp_date(timestamp)
             event_elt.addElement((NS_EVENT, "date"), content=formatted_date)
         register = data.pop("register", False)
-        for key in (u"name",):
+        for key in ("name",):
             if key in data:
                 event_elt[key] = data.pop(key)
-        for key in (u"description",):
+        for key in ("description",):
             if key in data:
                 event_elt.addElement((NS_EVENT, key), content=data.pop(key))
-        for key in (u"image", u"background-image"):
+        for key in ("image", "background-image"):
             if key in data:
                 elt = event_elt.addElement((NS_EVENT, key))
                 elt["src"] = data.pop(key)
 
         # we first create the invitees and blog nodes (if not specified in data)
-        for uri_type in (u"invitees", u"blog"):
-            key = uri_type + u"_uri"
-            for to_delete in (u"service", u"node"):
-                k = uri_type + u"_" + to_delete
+        for uri_type in ("invitees", "blog"):
+            key = uri_type + "_uri"
+            for to_delete in ("service", "node"):
+                k = uri_type + "_" + to_delete
                 if k in data:
                     del data[k]
             if key not in data:
@@ -352,12 +352,12 @@
             else:
                 uri = data.pop(key)
                 uri_data = xmpp_uri.parseXMPPUri(uri)
-                if uri_data[u"type"] != u"pubsub":
+                if uri_data["type"] != "pubsub":
                     raise ValueError(
-                        _(u"The given URI is not valid: {uri}").format(uri=uri)
+                        _("The given URI is not valid: {uri}").format(uri=uri)
                     )
-                uri_service = jid.JID(uri_data[u"path"])
-                uri_node = uri_data[u"node"]
+                uri_service = jid.JID(uri_data["path"])
+                uri_node = uri_data["node"]
 
             elt = event_elt.addElement((NS_EVENT, uri_type))
             elt["uri"] = xmpp_uri.buildXMPPUri(
@@ -365,7 +365,7 @@
             )
 
         # remaining data are put in <meta> elements
-        for key in data.keys():
+        for key in list(data.keys()):
             elt = event_elt.addElement((NS_EVENT, "meta"), content=data.pop(key))
             elt["name"] = key
 
@@ -374,8 +374,8 @@
             # TODO: check auto-create, no need to create node first if available
             node = yield self._p.createNode(client, service, nodeIdentifier=node)
         except 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"))
 
         yield self._p.publish(client, service, node, items=[item_elt])
 
@@ -388,7 +388,7 @@
                      profile_key=C.PROF_KEY_NONE):
         service = jid.JID(service) if service else None
         if not node:
-            raise ValueError(_(u"missing node"))
+            raise ValueError(_("missing node"))
         client = self.host.getClient(profile_key)
         return self.eventModify(
             client, service, node, id_ or NS_EVENT, timestamp_update or None, data_update
@@ -407,13 +407,13 @@
         new_timestamp = event_timestamp if timestamp_update is None else timestamp_update
         new_data = event_metadata
         if data_update:
-            for k, v in data_update.iteritems():
+            for k, v in data_update.items():
                 new_data[k] = v
         yield self.eventCreate(client, new_timestamp, new_data, service, node, id_)
 
     def _eventsListSerialise(self, events):
         for timestamp, data in events:
-            data["date"] = unicode(timestamp)
+            data["date"] = str(timestamp)
             data["creator"] = C.boolConst(data.get("creator", False))
         return [e[1] for e in events]
 
@@ -431,15 +431,15 @@
 
         @return list(tuple(int, dict)): list of events (timestamp + metadata)
         """
-        items, metadata = yield self.host.plugins[u'LIST_INTEREST'].listInterests(
+        items, metadata = yield self.host.plugins['LIST_INTEREST'].listInterests(
             client, service, node, namespace=NS_EVENT)
         events = []
         for item in items:
             try:
-                event_elt = next(item.interest.pubsub.elements(NS_EVENT, u"event"))
+                event_elt = next(item.interest.pubsub.elements(NS_EVENT, "event"))
             except StopIteration:
                 log.warning(
-                    _(u"No event found in item {item_id}, ignoring").format(
+                    _("No event found in item {item_id}, ignoring").format(
                         item_id=item["id"])
                 )
             else:
@@ -466,12 +466,12 @@
             items, metadata = yield self._p.getItems(
                 client, service, node, item_ids=[client.jid.userhost()]
             )
-            event_elt = next(items[0].elements(NS_EVENT, u"invitee"))
+            event_elt = next(items[0].elements(NS_EVENT, "invitee"))
         except (exceptions.NotFound, IndexError):
             # no item found, event data are not set yet
             defer.returnValue({})
         data = {}
-        for key in (u"attend", u"guests"):
+        for key in ("attend", "guests"):
             try:
                 data[key] = event_elt[key]
             except KeyError:
@@ -495,7 +495,7 @@
                 guests: an int
         """
         event_elt = domish.Element((NS_EVENT, "invitee"))
-        for key in (u"attend", u"guests"):
+        for key in ("attend", "guests"):
             try:
                 event_elt[key] = data.pop(key)
             except KeyError:
@@ -522,15 +522,15 @@
         invitees = {}
         for item in items:
             try:
-                event_elt = next(item.elements(NS_EVENT, u"invitee"))
+                event_elt = next(item.elements(NS_EVENT, "invitee"))
             except StopIteration:
                 # no item found, event data are not set yet
                 log.warning(_(
-                    u"no data found for {item_id} (service: {service}, node: {node})"
+                    "no data found for {item_id} (service: {service}, node: {node})"
                     .format(item_id=item["id"], service=service, node=node)))
             else:
                 data = {}
-                for key in (u"attend", u"guests"):
+                for key in ("attend", "guests"):
                     try:
                         data[key] = event_elt[key]
                     except KeyError:
@@ -558,30 +558,30 @@
         @param item_id(unicode): event id
         """
         # FIXME: handle name and extra
-        name = u''
+        name = ''
         extra = {}
         if self._b is None:
             raise exceptions.FeatureNotFound(
-                _(u'"XEP-0277" (blog) plugin is needed for this feature')
+                _('"XEP-0277" (blog) plugin is needed for this feature')
             )
         if item_id is None:
             item_id = NS_EVENT
 
         # first we authorize our invitee to see the nodes of interest
-        yield self._p.setNodeAffiliations(client, service, node, {invitee_jid: u"member"})
-        log.debug(_(u"affiliation set on event node"))
+        yield self._p.setNodeAffiliations(client, service, node, {invitee_jid: "member"})
+        log.debug(_("affiliation set on event node"))
         __, event_data = yield self.eventGet(client, service, node, item_id)
-        log.debug(_(u"got event data"))
+        log.debug(_("got event data"))
         invitees_service = jid.JID(event_data["invitees_service"])
         invitees_node = event_data["invitees_node"]
         blog_service = jid.JID(event_data["blog_service"])
         blog_node = event_data["blog_node"]
         yield self._p.setNodeAffiliations(
-            client, invitees_service, invitees_node, {invitee_jid: u"publisher"}
+            client, invitees_service, invitees_node, {invitee_jid: "publisher"}
         )
-        log.debug(_(u"affiliation set on invitee node"))
+        log.debug(_("affiliation set on invitee node"))
         yield self._p.setNodeAffiliations(
-            client, blog_service, blog_node, {invitee_jid: u"member"}
+            client, blog_service, blog_node, {invitee_jid: "member"}
         )
         blog_items, __ = yield self._b.mbGet(client, blog_service, blog_node, None)
 
@@ -591,29 +591,29 @@
                 comments_node = item["comments_node"]
             except KeyError:
                 log.debug(
-                    u"no comment service set for item {item_id}".format(
+                    "no comment service set for item {item_id}".format(
                         item_id=item["id"]
                     )
                 )
             else:
                 yield self._p.setNodeAffiliations(
-                    client, comments_service, comments_node, {invitee_jid: u"publisher"}
+                    client, comments_service, comments_node, {invitee_jid: "publisher"}
                 )
-        log.debug(_(u"affiliation set on blog and comments nodes"))
+        log.debug(_("affiliation set on blog and comments nodes"))
 
         # now we send the invitation
-        pubsub_invitation = self.host.plugins[u'INVITATION']
+        pubsub_invitation = self.host.plugins['INVITATION']
         pubsub_invitation.sendPubsubInvitation(client, invitee_jid, service, node,
                                                item_id, name, extra)
 
-    def _inviteByEmail(self, service, node, id_=NS_EVENT, email=u"", emails_extra=None,
-                       name=u"", host_name=u"", language=u"", url_template=u"",
-                       message_subject=u"", message_body=u"",
+    def _inviteByEmail(self, service, node, id_=NS_EVENT, email="", emails_extra=None,
+                       name="", host_name="", language="", url_template="",
+                       message_subject="", message_body="",
                        profile_key=C.PROF_KEY_NONE):
         client = self.host.getClient(profile_key)
         kwargs = {
-            u"profile": client.profile,
-            u"emails_extra": [unicode(e) for e in emails_extra],
+            "profile": client.profile,
+            "emails_extra": [str(e) for e in emails_extra],
         }
         for key in (
             "email",
@@ -625,7 +625,7 @@
             "message_body",
         ):
             value = locals()[key]
-            kwargs[key] = unicode(value)
+            kwargs[key] = str(value)
         return self.inviteByEmail(
             client, jid.JID(service) if service else None, node, id_ or NS_EVENT, **kwargs
         )
@@ -640,26 +640,26 @@
         """
         if self._i is None:
             raise exceptions.FeatureNotFound(
-                _(u'"Invitations" plugin is needed for this feature')
+                _('"Invitations" plugin is needed for this feature')
             )
         if self._b is None:
             raise exceptions.FeatureNotFound(
-                _(u'"XEP-0277" (blog) plugin is needed for this feature')
+                _('"XEP-0277" (blog) plugin is needed for this feature')
             )
         service = service or client.jid.userhostJID()
         event_uri = xmpp_uri.buildXMPPUri(
             "pubsub", path=service.full(), node=node, item=id_
         )
-        kwargs["extra"] = {u"event_uri": event_uri}
+        kwargs["extra"] = {"event_uri": event_uri}
         invitation_data = yield self._i.create(**kwargs)
-        invitee_jid = invitation_data[u"jid"]
-        log.debug(_(u"invitation created"))
+        invitee_jid = invitation_data["jid"]
+        log.debug(_("invitation created"))
         # now that we have a jid, we can send normal invitation
         yield self.invite(client, invitee_jid, service, node, id_)
 
 
+@implementer(iwokkel.IDisco)
 class EventsHandler(XMPPHandler):
-    implements(iwokkel.IDisco)
 
     def __init__(self, plugin_parent):
         self.plugin_parent = plugin_parent