code style reformatting using black

This commit is contained in:
Goffi 2018-06-27 20:14:46 +02:00
commit 744cb0c503

View file

@ -21,13 +21,14 @@ from sat.core.i18n import _
from sat.core import exceptions
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from twisted.words.xish import domish
from wokkel import disco, pubsub
from wokkel.formats import Mood
NS_USER_MOOD = 'http://jabber.org/protocol/mood'
NS_USER_MOOD = "http://jabber.org/protocol/mood"
PLUGIN_INFO = {
C.PI_NAME: "Personal Eventing Protocol Plugin",
@ -37,19 +38,25 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: ["XEP-0060"],
C.PI_MAIN: "XEP_0163",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Implementation of Personal Eventing Protocol""")
C.PI_DESCRIPTION: _("""Implementation of Personal Eventing Protocol"""),
}
class XEP_0163(object):
def __init__(self, host):
log.info(_("PEP plugin initialization"))
self.host = host
self.pep_events = set()
self.pep_out_cb = {}
host.trigger.add("PubSub Disco Info", self.disoInfoTrigger)
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;
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;
self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB, self.sendMood)
def disoInfoTrigger(self, disco_info, profile):
@ -78,13 +85,18 @@ class XEP_0163(object):
self.pep_events.add(node)
if notify:
self.pep_events.add(node + "+notify")
def filterPEPEvent(client, itemsEvent):
"""Ignore messages which are not coming from PEP (i.e. main server)
@param itemsEvent(pubsub.ItemsEvent): pubsub event
"""
if itemsEvent.sender.user or itemsEvent.sender.resource:
log.debug("ignoring non PEP event from {} (profile={})".format(itemsEvent.sender.full(), client.profile))
log.debug(
"ignoring non PEP event from {} (profile={})".format(
itemsEvent.sender.full(), client.profile
)
)
return
in_callback(itemsEvent, client.profile)
@ -110,11 +122,14 @@ class XEP_0163(object):
"""
profile = self.host.memory.getProfileName(profile_key)
if not profile:
log.error(_(u'Trying to send personal event with an unknown profile key [%s]') % profile_key)
log.error(
_(u"Trying to send personal event with an unknown profile key [%s]")
% profile_key
)
raise exceptions.ProfileUnknownError
if not event_type in self.pep_out_cb.keys():
log.error(_('Trying to send personal event for an unknown type'))
raise exceptions.DataError('Type unknown')
log.error(_("Trying to send personal event for an unknown type"))
raise exceptions.DataError("Type unknown")
return self.pep_out_cb[event_type](data, profile)
def userMoodCB(self, itemsEvent, profile):
@ -122,7 +137,9 @@ class XEP_0163(object):
log.debug(_("No item found"))
return
try:
mood_elt = [child for child in itemsEvent.items[0].elements() if child.name == "mood"][0]
mood_elt = [
child for child in itemsEvent.items[0].elements() if child.name == "mood"
][0]
except IndexError:
log.error(_("Can't find mood element in mood event"))
return
@ -130,8 +147,14 @@ class XEP_0163(object):
if not mood:
log.debug(_("No mood found"))
return
self.host.bridge.psEvent(C.PS_PEP, itemsEvent.sender.full(), itemsEvent.nodeIdentifier,
"MOOD", {"mood": mood.value or "", "text": mood.text or ""}, profile)
self.host.bridge.psEvent(
C.PS_PEP,
itemsEvent.sender.full(),
itemsEvent.nodeIdentifier,
"MOOD",
{"mood": mood.value or "", "text": mood.text or ""},
profile,
)
def sendMood(self, data, profile):
"""Send XEP-0107's User Mood
@ -139,8 +162,8 @@ class XEP_0163(object):
@param data: must include mood and text
@param profile: profile which send the mood"""
try:
value = data['mood'].lower()
text = data['text'] if 'text' in data else ''
value = data["mood"].lower()
text = data["text"] if "text" in data else ""
except KeyError:
raise exceptions.DataError("Mood data must contain at least 'mood' key")
mood = UserMood(value, text)
@ -152,7 +175,7 @@ class UserMood(Mood, domish.Element):
def __init__(self, value, text=None):
Mood.__init__(self, value, text)
domish.Element.__init__(self, (NS_USER_MOOD, 'mood'))
domish.Element.__init__(self, (NS_USER_MOOD, "mood"))
self.addElement(value)
if text:
self.addElement('text', content=text)
self.addElement("text", content=text)