comparison sat/plugins/plugin_xep_0163.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 189e38fb11ff
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core import exceptions 21 from sat.core import exceptions
22 from sat.core.constants import Const as C 22 from sat.core.constants import Const as C
23 from sat.core.log import getLogger 23 from sat.core.log import getLogger
24
24 log = getLogger(__name__) 25 log = getLogger(__name__)
25 from twisted.words.xish import domish 26 from twisted.words.xish import domish
26 27
27 from wokkel import disco, pubsub 28 from wokkel import disco, pubsub
28 from wokkel.formats import Mood 29 from wokkel.formats import Mood
29 30
30 NS_USER_MOOD = 'http://jabber.org/protocol/mood' 31 NS_USER_MOOD = "http://jabber.org/protocol/mood"
31 32
32 PLUGIN_INFO = { 33 PLUGIN_INFO = {
33 C.PI_NAME: "Personal Eventing Protocol Plugin", 34 C.PI_NAME: "Personal Eventing Protocol Plugin",
34 C.PI_IMPORT_NAME: "XEP-0163", 35 C.PI_IMPORT_NAME: "XEP-0163",
35 C.PI_TYPE: "XEP", 36 C.PI_TYPE: "XEP",
36 C.PI_PROTOCOLS: ["XEP-0163", "XEP-0107"], 37 C.PI_PROTOCOLS: ["XEP-0163", "XEP-0107"],
37 C.PI_DEPENDENCIES: ["XEP-0060"], 38 C.PI_DEPENDENCIES: ["XEP-0060"],
38 C.PI_MAIN: "XEP_0163", 39 C.PI_MAIN: "XEP_0163",
39 C.PI_HANDLER: "no", 40 C.PI_HANDLER: "no",
40 C.PI_DESCRIPTION: _("""Implementation of Personal Eventing Protocol""") 41 C.PI_DESCRIPTION: _("""Implementation of Personal Eventing Protocol"""),
41 } 42 }
42 43
43 44
44 class XEP_0163(object): 45 class XEP_0163(object):
45
46 def __init__(self, host): 46 def __init__(self, host):
47 log.info(_("PEP plugin initialization")) 47 log.info(_("PEP plugin initialization"))
48 self.host = host 48 self.host = host
49 self.pep_events = set() 49 self.pep_events = set()
50 self.pep_out_cb = {} 50 self.pep_out_cb = {}
51 host.trigger.add("PubSub Disco Info", self.disoInfoTrigger) 51 host.trigger.add("PubSub Disco Info", self.disoInfoTrigger)
52 host.bridge.addMethod("PEPSend", ".plugin", in_sign='sa{ss}s', out_sign='', method=self.PEPSend, async=True) # args: type(MOOD, TUNE, etc), data, profile_key; 52 host.bridge.addMethod(
53 "PEPSend",
54 ".plugin",
55 in_sign="sa{ss}s",
56 out_sign="",
57 method=self.PEPSend,
58 async=True,
59 ) # args: type(MOOD, TUNE, etc), data, profile_key;
53 self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB, self.sendMood) 60 self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB, self.sendMood)
54 61
55 def disoInfoTrigger(self, disco_info, profile): 62 def disoInfoTrigger(self, disco_info, profile):
56 """Add info from managed PEP 63 """Add info from managed PEP
57 64
76 if out_callback: 83 if out_callback:
77 self.pep_out_cb[event_type] = out_callback 84 self.pep_out_cb[event_type] = out_callback
78 self.pep_events.add(node) 85 self.pep_events.add(node)
79 if notify: 86 if notify:
80 self.pep_events.add(node + "+notify") 87 self.pep_events.add(node + "+notify")
88
81 def filterPEPEvent(client, itemsEvent): 89 def filterPEPEvent(client, itemsEvent):
82 """Ignore messages which are not coming from PEP (i.e. main server) 90 """Ignore messages which are not coming from PEP (i.e. main server)
83 91
84 @param itemsEvent(pubsub.ItemsEvent): pubsub event 92 @param itemsEvent(pubsub.ItemsEvent): pubsub event
85 """ 93 """
86 if itemsEvent.sender.user or itemsEvent.sender.resource: 94 if itemsEvent.sender.user or itemsEvent.sender.resource:
87 log.debug("ignoring non PEP event from {} (profile={})".format(itemsEvent.sender.full(), client.profile)) 95 log.debug(
96 "ignoring non PEP event from {} (profile={})".format(
97 itemsEvent.sender.full(), client.profile
98 )
99 )
88 return 100 return
89 in_callback(itemsEvent, client.profile) 101 in_callback(itemsEvent, client.profile)
90 102
91 self.host.plugins["XEP-0060"].addManagedNode(node, items_cb=filterPEPEvent) 103 self.host.plugins["XEP-0060"].addManagedNode(node, items_cb=filterPEPEvent)
92 104
108 @param data: dict of {string:string} of event_type dependant data 120 @param data: dict of {string:string} of event_type dependant data
109 @param profile_key: profile who send the event 121 @param profile_key: profile who send the event
110 """ 122 """
111 profile = self.host.memory.getProfileName(profile_key) 123 profile = self.host.memory.getProfileName(profile_key)
112 if not profile: 124 if not profile:
113 log.error(_(u'Trying to send personal event with an unknown profile key [%s]') % profile_key) 125 log.error(
126 _(u"Trying to send personal event with an unknown profile key [%s]")
127 % profile_key
128 )
114 raise exceptions.ProfileUnknownError 129 raise exceptions.ProfileUnknownError
115 if not event_type in self.pep_out_cb.keys(): 130 if not event_type in self.pep_out_cb.keys():
116 log.error(_('Trying to send personal event for an unknown type')) 131 log.error(_("Trying to send personal event for an unknown type"))
117 raise exceptions.DataError('Type unknown') 132 raise exceptions.DataError("Type unknown")
118 return self.pep_out_cb[event_type](data, profile) 133 return self.pep_out_cb[event_type](data, profile)
119 134
120 def userMoodCB(self, itemsEvent, profile): 135 def userMoodCB(self, itemsEvent, profile):
121 if not itemsEvent.items: 136 if not itemsEvent.items:
122 log.debug(_("No item found")) 137 log.debug(_("No item found"))
123 return 138 return
124 try: 139 try:
125 mood_elt = [child for child in itemsEvent.items[0].elements() if child.name == "mood"][0] 140 mood_elt = [
141 child for child in itemsEvent.items[0].elements() if child.name == "mood"
142 ][0]
126 except IndexError: 143 except IndexError:
127 log.error(_("Can't find mood element in mood event")) 144 log.error(_("Can't find mood element in mood event"))
128 return 145 return
129 mood = Mood.fromXml(mood_elt) 146 mood = Mood.fromXml(mood_elt)
130 if not mood: 147 if not mood:
131 log.debug(_("No mood found")) 148 log.debug(_("No mood found"))
132 return 149 return
133 self.host.bridge.psEvent(C.PS_PEP, itemsEvent.sender.full(), itemsEvent.nodeIdentifier, 150 self.host.bridge.psEvent(
134 "MOOD", {"mood": mood.value or "", "text": mood.text or ""}, profile) 151 C.PS_PEP,
152 itemsEvent.sender.full(),
153 itemsEvent.nodeIdentifier,
154 "MOOD",
155 {"mood": mood.value or "", "text": mood.text or ""},
156 profile,
157 )
135 158
136 def sendMood(self, data, profile): 159 def sendMood(self, data, profile):
137 """Send XEP-0107's User Mood 160 """Send XEP-0107's User Mood
138 161
139 @param data: must include mood and text 162 @param data: must include mood and text
140 @param profile: profile which send the mood""" 163 @param profile: profile which send the mood"""
141 try: 164 try:
142 value = data['mood'].lower() 165 value = data["mood"].lower()
143 text = data['text'] if 'text' in data else '' 166 text = data["text"] if "text" in data else ""
144 except KeyError: 167 except KeyError:
145 raise exceptions.DataError("Mood data must contain at least 'mood' key") 168 raise exceptions.DataError("Mood data must contain at least 'mood' key")
146 mood = UserMood(value, text) 169 mood = UserMood(value, text)
147 return self.sendPEPEvent(NS_USER_MOOD, mood, profile) 170 return self.sendPEPEvent(NS_USER_MOOD, mood, profile)
148 171
150 class UserMood(Mood, domish.Element): 173 class UserMood(Mood, domish.Element):
151 """Improved wokkel Mood which is also a domish.Element""" 174 """Improved wokkel Mood which is also a domish.Element"""
152 175
153 def __init__(self, value, text=None): 176 def __init__(self, value, text=None):
154 Mood.__init__(self, value, text) 177 Mood.__init__(self, value, text)
155 domish.Element.__init__(self, (NS_USER_MOOD, 'mood')) 178 domish.Element.__init__(self, (NS_USER_MOOD, "mood"))
156 self.addElement(value) 179 self.addElement(value)
157 if text: 180 if text:
158 self.addElement('text', content=text) 181 self.addElement("text", content=text)