Mercurial > libervia-backend
diff src/plugins/plugin_xep_0115.py @ 594:e629371a28d3
Fix pep8 support in src/plugins.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 18 Jan 2013 17:55:35 +0100 |
parents | beaf6bec2fcd |
children | 84a6e83157c2 |
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0115.py Mon Jan 21 00:59:50 2013 +0100 +++ b/src/plugins/plugin_xep_0115.py Fri Jan 18 17:55:35 2013 +0100 @@ -42,22 +42,24 @@ PRESENCE = '/presence' NS_ENTITY_CAPABILITY = 'http://jabber.org/protocol/caps' -CAPABILITY_UPDATE = PRESENCE + '/c[@xmlns="' + NS_ENTITY_CAPABILITY + '"]' +CAPABILITY_UPDATE = PRESENCE + '/c[@xmlns="' + NS_ENTITY_CAPABILITY + '"]' PLUGIN_INFO = { -"name": "XEP 0115 Plugin", -"import_name": "XEP-0115", -"type": "XEP", -"protocols": ["XEP-0115"], -"dependencies": [], -"main": "XEP_0115", -"handler": "yes", -"description": _("""Implementation of entity capabilities""") + "name": "XEP 0115 Plugin", + "import_name": "XEP-0115", + "type": "XEP", + "protocols": ["XEP-0115"], + "dependencies": [], + "main": "XEP_0115", + "handler": "yes", + "description": _("""Implementation of entity capabilities""") } + class HashGenerationError(Exception): pass + class ByteIdentity(object): """This class manage identity as bytes (needed for i;octet sort), it is used for the hash generation""" @@ -74,16 +76,16 @@ class XEP_0115(object): - cap_hash = None #capabilities hash is class variable as it is common to all profiles + cap_hash = None # capabilities hash is class variable as it is common to all profiles #TODO: this code is really dirty, need to clean it and try to move it to Wokkel def __init__(self, host): info(_("Plugin XEP_0115 initialization")) self.host = host host.trigger.add("Disco Handled", self.checkHash) - self.hash_cache = PersistentBinaryDict(NS_ENTITY_CAPABILITY) #key = hash or jid, value = features + self.hash_cache = PersistentBinaryDict(NS_ENTITY_CAPABILITY) # key = hash or jid, value = features self.hash_cache.load() - self.jid_hash = {} #jid to hash mapping, map to a discoInfo features if the hash method is unknown + self.jid_hash = {} # jid to hash mapping, map to a discoInfo features if the hash method is unknown def checkHash(self, profile): if not XEP_0115.cap_hash: @@ -97,16 +99,17 @@ def presenceHack(self, profile): """modify SatPresenceProtocol to add capabilities data""" - client=self.host.getClient(profile) + client = self.host.getClient(profile) presenceInst = client.presence - c_elt = domish.Element((NS_ENTITY_CAPABILITY,'c')) - c_elt['hash']='sha-1' - c_elt['node']='http://wiki.goffi.org/wiki/Salut_%C3%A0_Toi' - c_elt['ver']=XEP_0115.cap_hash + c_elt = domish.Element((NS_ENTITY_CAPABILITY, 'c')) + c_elt['hash'] = 'sha-1' + c_elt['node'] = 'http://wiki.goffi.org/wiki/Salut_%C3%A0_Toi' + c_elt['ver'] = XEP_0115.cap_hash presenceInst._c_elt = c_elt if "_legacy_send" in dir(presenceInst): debug('capabilities already added to presence instance') return + def hacked_send(self, obj): obj.addChild(self._c_elt) self._legacy_send(obj) @@ -114,31 +117,30 @@ presenceInst._legacy_send = presenceInst.send presenceInst.send = new_send - def generateHash(self, profile_key="@DEFAULT@"): """This method generate a sha1 hash as explained in xep-0115 #5.1 it then store it in XEP_0115.cap_hash""" profile = self.host.memory.getProfileName(profile_key) if not profile: - error ('Requesting hash for an inexistant profile') + error('Requesting hash for an inexistant profile') raise HashGenerationError client = self.host.getClient(profile_key) if not client: - error ('Requesting hash for an inexistant client') + error('Requesting hash for an inexistant client') raise HashGenerationError def generateHash_2(services, profile): - _s=[] - byte_identities = [ByteIdentity(identity) for identity in filter(lambda x:isinstance(x,disco.DiscoIdentity),services)] #FIXME: lang must be managed here - byte_identities.sort(key=lambda i:i.lang) - byte_identities.sort(key=lambda i:i.idType) - byte_identities.sort(key=lambda i:i.category) + _s = [] + byte_identities = [ByteIdentity(identity) for identity in filter(lambda x: isinstance(x, disco.DiscoIdentity), services)] # FIXME: lang must be managed here + byte_identities.sort(key=lambda i: i.lang) + byte_identities.sort(key=lambda i: i.idType) + byte_identities.sort(key=lambda i: i.category) for identity in byte_identities: _s.append(str(identity)) _s.append('<') - byte_features = [feature.encode('utf-8') for feature in filter(lambda x:isinstance(x,disco.DiscoFeature),services)] - byte_features.sort() #XXX: the default sort has the same behaviour as the requested RFC 4790 i;octet sort + byte_features = [feature.encode('utf-8') for feature in filter(lambda x: isinstance(x, disco.DiscoFeature), services)] + byte_features.sort() # XXX: the default sort has the same behaviour as the requested RFC 4790 i;octet sort for feature in byte_features: _s.append(feature) _s.append('<') @@ -149,6 +151,7 @@ services = client.discoHandler.info(client.jid, client.jid, '').addCallback(generateHash_2, profile) + class XEP_0115_handler(XMPPHandler): implements(iwokkel.IDisco) @@ -176,7 +179,6 @@ #No key, that means unknown hash method self.plugin_parent.jid_hash[from_jid] = discoResult.features - def update(self, presence): """ Manage the capabilities of the entity @@ -184,11 +186,11 @@ and get the capibilities if necessary """ from_jid = jid.JID(presence['from']) - c_elem = filter (lambda x:x.name == "c", presence.elements())[0] #We only want the "c" element + c_elem = filter(lambda x: x.name == "c", presence.elements())[0] # We only want the "c" element try: - ver=c_elem['ver'] - hash=c_elem['hash'] - node=c_elem['node'] + ver = c_elem['ver'] + hash = c_elem['hash'] + node = c_elem['node'] except KeyError: warning('Received invalid capabilities tag') return @@ -197,9 +199,8 @@ #we know that hash, we just link it with the jid self.plugin_parent.jid_hash[from_jid] = ver else: - if hash!='sha-1': + if hash != 'sha-1': #unknown hash method warning('Unknown hash for entity capabilities: [%s]' % hash) - self.parent.disco.requestInfo(from_jid).addCallback(self._updateCache, from_jid, ver if hash=='sha-1' else None ) + self.parent.disco.requestInfo(from_jid).addCallback(self._updateCache, from_jid, ver if hash == 'sha-1' else None) #TODO: me must manage the full algorithm described at XEP-0115 #5.4 part 3 -