Mercurial > libervia-backend
diff sat/plugins/plugin_xep_0095.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 | 003b8b4b56a7 |
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0095.py Wed Jun 27 07:51:29 2018 +0200 +++ b/sat/plugins/plugin_xep_0095.py Wed Jun 27 20:14:46 2018 +0200 @@ -20,6 +20,7 @@ from sat.core.i18n import _ from sat.core.constants import Const as C from sat.core.log import getLogger + log = getLogger(__name__) from sat.core import exceptions from twisted.words.protocols.jabber import xmlstream @@ -37,19 +38,18 @@ C.PI_PROTOCOLS: ["XEP-0095"], C.PI_MAIN: "XEP_0095", C.PI_HANDLER: "yes", - C.PI_DESCRIPTION: _("""Implementation of Stream Initiation""") + C.PI_DESCRIPTION: _("""Implementation of Stream Initiation"""), } IQ_SET = '/iq[@type="set"]' -NS_SI = 'http://jabber.org/protocol/si' +NS_SI = "http://jabber.org/protocol/si" SI_REQUEST = IQ_SET + '/si[@xmlns="' + NS_SI + '"]' SI_PROFILE_HEADER = "http://jabber.org/protocol/si/profile/" -SI_ERROR_CONDITIONS = ('bad-profile', 'no-valid-streams') +SI_ERROR_CONDITIONS = ("bad-profile", "no-valid-streams") class XEP_0095(object): - def __init__(self, host): log.info(_("Plugin XEP_0095 initialization")) self.host = host @@ -70,7 +70,11 @@ try: del self.si_profiles[si_profile] except KeyError: - log.error(u"Trying to unregister SI profile [{}] which was not registered".format(si_profile)) + log.error( + u"Trying to unregister SI profile [{}] which was not registered".format( + si_profile + ) + ) def streamInit(self, iq_elt, client): """This method is called on stream initiation (XEP-0095 #3.2) @@ -79,17 +83,21 @@ """ log.info(_("XEP-0095 Stream initiation")) iq_elt.handled = True - si_elt = iq_elt.elements(NS_SI, 'si').next() - si_id = si_elt['id'] - si_mime_type = iq_elt.getAttribute('mime-type', 'application/octet-stream') - si_profile = si_elt['profile'] - si_profile_key = si_profile[len(SI_PROFILE_HEADER):] if si_profile.startswith(SI_PROFILE_HEADER) else si_profile + si_elt = iq_elt.elements(NS_SI, "si").next() + si_id = si_elt["id"] + si_mime_type = iq_elt.getAttribute("mime-type", "application/octet-stream") + si_profile = si_elt["profile"] + si_profile_key = ( + si_profile[len(SI_PROFILE_HEADER) :] + if si_profile.startswith(SI_PROFILE_HEADER) + else si_profile + ) if si_profile_key in self.si_profiles: - #We know this SI profile, we call the callback + # We know this SI profile, we call the callback self.si_profiles[si_profile_key](client, iq_elt, si_id, si_mime_type, si_elt) else: - #We don't know this profile, we send an error - self.sendError(client, iq_elt, 'bad-profile') + # We don't know this profile, we send an error + self.sendError(client, iq_elt, "bad-profile") def sendError(self, client, request, condition): """Send IQ error as a result @@ -99,7 +107,7 @@ """ if condition in SI_ERROR_CONDITIONS: si_condition = condition - condition = 'bad-request' + condition = "bad-request" else: si_condition = None @@ -119,8 +127,8 @@ log.info(_("sending stream initiation accept answer")) if misc_elts is None: misc_elts = [] - result_elt = xmlstream.toResponse(iq_elt, 'result') - si_elt = result_elt.addElement((NS_SI, 'si')) + result_elt = xmlstream.toResponse(iq_elt, "result") + si_elt = result_elt.addElement((NS_SI, "si")) si_elt.addChild(feature_elt) for elt in misc_elts: si_elt.addChild(elt) @@ -134,8 +142,15 @@ raise exceptions.DataError return (iq_elt, si_elt) - - def proposeStream(self, client, to_jid, si_profile, feature_elt, misc_elts, mime_type='application/octet-stream'): + def proposeStream( + self, + client, + to_jid, + si_profile, + feature_elt, + misc_elts, + mime_type="application/octet-stream", + ): """Propose a stream initiation @param to_jid(jid.JID): recipient @@ -154,8 +169,8 @@ offer["from"] = client.jid.full() offer["to"] = to_jid.full() - si = offer.addElement('si', NS_SI) - si['id'] = sid + si = offer.addElement("si", NS_SI) + si["id"] = sid si["mime-type"] = mime_type si["profile"] = si_profile for elt in misc_elts: @@ -175,10 +190,17 @@ self.host = plugin_parent.host def connectionInitialized(self): - self.xmlstream.addObserver(SI_REQUEST, self.plugin_parent.streamInit, client=self.parent) + self.xmlstream.addObserver( + SI_REQUEST, self.plugin_parent.streamInit, client=self.parent + ) - def getDiscoInfo(self, requestor, target, nodeIdentifier=''): - return [disco.DiscoFeature(NS_SI)] + [disco.DiscoFeature(u"http://jabber.org/protocol/si/profile/{}".format(profile_name)) for profile_name in self.plugin_parent.si_profiles] + def getDiscoInfo(self, requestor, target, nodeIdentifier=""): + return [disco.DiscoFeature(NS_SI)] + [ + disco.DiscoFeature( + u"http://jabber.org/protocol/si/profile/{}".format(profile_name) + ) + for profile_name in self.plugin_parent.si_profiles + ] - def getDiscoItems(self, requestor, target, nodeIdentifier=''): + def getDiscoItems(self, requestor, target, nodeIdentifier=""): return []