annotate src/plugins/plugin_exp_events.py @ 2240:4b66f070cfbb

jp (uri): new command to parse or generate XMPP URIs
author Goffi <goffi@goffi.org>
date Fri, 19 May 2017 12:34:33 +0200
parents 230fc5b609a8
children 5e12fc5ae52a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2231
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin to detect language (experimental)
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.log import getLogger
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 log = getLogger(__name__)
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import defer
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import jid
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.xish import domish
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from wokkel import pubsub
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 PLUGIN_INFO = {
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 C.PI_NAME: "Event plugin",
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 C.PI_IMPORT_NAME: "events",
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 C.PI_TYPE: "EXP",
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 C.PI_PROTOCOLS: [],
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_DEPENDENCIES: ["XEP-0060"],
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_MAIN: "Events",
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_HANDLER: "no",
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_DESCRIPTION: _("""Experimental implementation of XMPP events management""")
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 }
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 NS_EVENT = 'org.salut-a-toi.event:0'
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 class Events(object):
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 """Q&D module to handle event attendance answer, experimentation only"""
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def __init__(self, host):
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 log.info(_(u"Event plugin initialization"))
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.host = host
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self._p = self.host.plugins["XEP-0060"]
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 host.bridge.addMethod("eventGet", ".plugin",
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 in_sign='sss', out_sign='a{ss}',
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 method=self._eventGet,
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 async=True)
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 host.bridge.addMethod("eventSet", ".plugin",
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 in_sign='ssa{ss}s', out_sign='',
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 method=self._eventSet,
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 async=True)
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def _eventGet(self, service, node, profile_key):
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 service = jid.JID(service) if service else None
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 node = node if node else NS_EVENT
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 client = self.host.getClient(profile_key)
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 return self.eventGet(client, service, node)
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 @defer.inlineCallbacks
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def eventGet(self, client, service, node):
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """Retrieve attendance from event node
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 @param service(unicode, None): PubSub service
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 @param node(unicode): PubSub node of the event
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 @return (dict): a dict with current attendance status,
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 an empty dict is returned if nothing has been answered yed
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 """
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 items, metadata = yield self._p.getItems(service, node, item_ids=[client.jid.userhost()], profile_key=client.profile)
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 try:
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 event_elt = next(items[0].elements((NS_EVENT, u'event')))
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 except IndexError:
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 # no item found, event data are not set yet
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 defer.returnValue({})
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 data = {}
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 for key in (u'attend', u'guests'):
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 try:
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 data[key] = event_elt[key]
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 except KeyError:
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 continue
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 defer.returnValue(data)
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 def _eventSet(self, service, node, event_data, profile_key):
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 service = jid.JID(service) if service else None
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 node = node if node else NS_EVENT
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 client = self.host.getClient(profile_key)
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 return self.eventSet(client, service, node, event_data)
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 def eventSet(self, client, service, node, data):
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 """Set or update attendance data in event node
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 @param service(unicode, None): PubSub service
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 @param node(unicode): PubSub node of the event
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 @param data(dict[unicode, unicode]): data to update
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 key can be:
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 attend: one of "yes", "no", "maybe"
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 guests: an int
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 """
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 event_elt = domish.Element((NS_EVENT, 'event'))
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 for key in (u'attend', u'guests'):
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 try:
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 event_elt[key] = data.pop(key)
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 except KeyError:
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 pass
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 item_elt = pubsub.Item(id=client.jid.userhost(), payload=event_elt)
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 return self._p.publish(service, node, items=[item_elt], profile_key=client.profile)