code style reformatting using black

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

View file

@ -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 twisted.words.xish import domish
from twisted.words.protocols.jabber import jid
@ -32,9 +33,9 @@ try:
except ImportError:
from wokkel.subprotocols import XMPPHandler
PRESENCE = '/presence'
NS_ENTITY_CAPABILITY = 'http://jabber.org/protocol/caps'
NS_CAPS_OPTIMIZE = 'http://jabber.org/protocol/caps#optimize'
PRESENCE = "/presence"
NS_ENTITY_CAPABILITY = "http://jabber.org/protocol/caps"
NS_CAPS_OPTIMIZE = "http://jabber.org/protocol/caps#optimize"
CAPABILITY_UPDATE = PRESENCE + '/c[@xmlns="' + NS_ENTITY_CAPABILITY + '"]'
PLUGIN_INFO = {
@ -46,7 +47,7 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: [],
C.PI_MAIN: "XEP_0115",
C.PI_HANDLER: "yes",
C.PI_DESCRIPTION: _("""Implementation of entity capabilities""")
C.PI_DESCRIPTION: _("""Implementation of entity capabilities"""),
}
@ -75,24 +76,29 @@ class XEP_0115(object):
log.warning(_(u"Caps optimisation not available"))
# hash generation
_infos = yield client.discoHandler.info(client.jid, client.jid, '')
_infos = yield client.discoHandler.info(client.jid, client.jid, "")
disco_infos = disco.DiscoInfo()
for item in _infos:
disco_infos.append(item)
cap_hash = client._caps_hash = self.host.memory.disco.generateHash(disco_infos)
log.info(u"Our capability hash has been generated: [{cap_hash}]".format(
cap_hash = cap_hash))
log.info(
u"Our capability hash has been generated: [{cap_hash}]".format(
cap_hash=cap_hash
)
)
log.debug(u"Generating capability domish.Element")
c_elt = domish.Element((NS_ENTITY_CAPABILITY, 'c'))
c_elt['hash'] = 'sha-1'
c_elt['node'] = C.APP_URL
c_elt['ver'] = cap_hash
c_elt = domish.Element((NS_ENTITY_CAPABILITY, "c"))
c_elt["hash"] = "sha-1"
c_elt["node"] = C.APP_URL
c_elt["ver"] = cap_hash
client._caps_elt = c_elt
if client._caps_optimize:
client._caps_sent = False
if cap_hash not in self.host.memory.disco.hashes:
self.host.memory.disco.hashes[cap_hash] = disco_infos
self.host.memory.updateEntityData(client.jid, C.ENTITY_CAP_HASH, cap_hash, profile_key=client.profile)
self.host.memory.updateEntityData(
client.jid, C.ENTITY_CAP_HASH, cap_hash, profile_key=client.profile
)
def _presenceAddElt(self, client, obj):
if client._caps_optimize:
@ -120,10 +126,13 @@ class XEP_0115_handler(XMPPHandler):
def connectionInitialized(self):
self.xmlstream.addObserver(CAPABILITY_UPDATE, self.update)
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
return [disco.DiscoFeature(NS_ENTITY_CAPABILITY), disco.DiscoFeature(NS_CAPS_OPTIMIZE)]
def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
return [
disco.DiscoFeature(NS_ENTITY_CAPABILITY),
disco.DiscoFeature(NS_CAPS_OPTIMIZE),
]
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
def getDiscoItems(self, requestor, target, nodeIdentifier=""):
return []
def update(self, presence):
@ -132,35 +141,65 @@ class XEP_0115_handler(XMPPHandler):
Check if we know the version of this capabilities and get the capabilities if necessary
"""
from_jid = jid.JID(presence['from'])
c_elem = presence.elements(NS_ENTITY_CAPABILITY, 'c').next()
from_jid = jid.JID(presence["from"])
c_elem = presence.elements(NS_ENTITY_CAPABILITY, "c").next()
try:
c_ver = c_elem['ver']
c_hash = c_elem['hash']
c_node = c_elem['node']
c_ver = c_elem["ver"]
c_hash = c_elem["hash"]
c_node = c_elem["node"]
except KeyError:
log.warning(_(u'Received invalid capabilities tag: %s') % c_elem.toXml())
log.warning(_(u"Received invalid capabilities tag: %s") % c_elem.toXml())
return
if c_ver in self.host.memory.disco.hashes:
# we already know the hash, we update the jid entity
log.debug(u"hash [%(hash)s] already in cache, updating entity [%(jid)s]" % {'hash': c_ver, 'jid': from_jid.full()})
self.host.memory.updateEntityData(from_jid, C.ENTITY_CAP_HASH, c_ver, profile_key=self.profile)
log.debug(
u"hash [%(hash)s] already in cache, updating entity [%(jid)s]"
% {"hash": c_ver, "jid": from_jid.full()}
)
self.host.memory.updateEntityData(
from_jid, C.ENTITY_CAP_HASH, c_ver, profile_key=self.profile
)
return
if c_hash != 'sha-1': # unknown hash method
log.warning(_(u'Unknown hash method for entity capabilities: [%(hash_method)s] (entity: %(jid)s, node: %(node)s)') % {'hash_method':c_hash, 'jid': from_jid, 'node': c_node})
if c_hash != "sha-1": # unknown hash method
log.warning(
_(
u"Unknown hash method for entity capabilities: [%(hash_method)s] (entity: %(jid)s, node: %(node)s)"
)
% {"hash_method": c_hash, "jid": from_jid, "node": c_node}
)
def cb(dummy):
computed_hash = self.host.memory.getEntityDatum(from_jid, C.ENTITY_CAP_HASH, self.profile)
computed_hash = self.host.memory.getEntityDatum(
from_jid, C.ENTITY_CAP_HASH, self.profile
)
if computed_hash != c_ver:
log.warning(_(u'Computed hash differ from given hash:\ngiven: [%(given_hash)s]\ncomputed: [%(computed_hash)s]\n(entity: %(jid)s, node: %(node)s)') % {'given_hash':c_ver, 'computed_hash': computed_hash, 'jid': from_jid, 'node': c_node})
log.warning(
_(
u"Computed hash differ from given hash:\ngiven: [%(given_hash)s]\ncomputed: [%(computed_hash)s]\n(entity: %(jid)s, node: %(node)s)"
)
% {
"given_hash": c_ver,
"computed_hash": computed_hash,
"jid": from_jid,
"node": c_node,
}
)
def eb(failure):
if isinstance(failure.value, error.ConnectionDone):
return
msg = failure.value.condition if hasattr(failure.value, 'condition') else failure.getErrorMessage()
log.error(_(u"Couldn't retrieve disco info for {jid}: {error}").format(jid=from_jid.full(), error=msg))
msg = (
failure.value.condition
if hasattr(failure.value, "condition")
else failure.getErrorMessage()
)
log.error(
_(u"Couldn't retrieve disco info for {jid}: {error}").format(
jid=from_jid.full(), error=msg
)
)
d = self.host.getDiscoInfos(self.parent, from_jid)
d.addCallbacks(cb, eb)