Mercurial > libervia-backend
diff libervia/backend/plugins/plugin_xep_0054.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 4b842c1fb686 |
children |
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_xep_0054.py Tue Jun 18 12:06:45 2024 +0200 +++ b/libervia/backend/plugins/plugin_xep_0054.py Wed Jun 19 18:44:57 2024 +0200 @@ -78,9 +78,9 @@ def __init__(self, host): log.info(_("Plugin XEP_0054 initialization")) self.host = host - self._i = host.plugins['IDENTITY'] - self._i.register(IMPORT_NAME, 'avatar', self.get_avatar, self.set_avatar) - self._i.register(IMPORT_NAME, 'nicknames', self.get_nicknames, self.set_nicknames) + self._i = host.plugins["IDENTITY"] + self._i.register(IMPORT_NAME, "avatar", self.get_avatar, self.set_avatar) + self._i.register(IMPORT_NAME, "nicknames", self.get_nicknames, self.set_nicknames) host.trigger.add("presence_available", self.presence_available_trigger) def get_handler(self, client): @@ -90,9 +90,7 @@ try: avatar_hash = client._xep_0054_avatar_hashes[client.jid.userhost()] except KeyError: - log.info( - _("No avatar in cache for {profile}") - .format(profile=client.profile)) + log.info(_("No avatar in cache for {profile}").format(profile=client.profile)) return True x_elt = domish.Element((NS_VCARD_UPDATE, "x")) x_elt.addElement("photo", content=avatar_hash) @@ -101,7 +99,8 @@ async def profile_connecting(self, client): client._xep_0054_avatar_hashes = persistent.PersistentDict( - NS_VCARD, client.profile) + NS_VCARD, client.profile + ) await client._xep_0054_avatar_hashes.load() def save_photo(self, client, photo_elt, entity): @@ -136,7 +135,8 @@ if mime_type is None: log.debug( f"no media type found specified for {entity}'s avatar, trying to " - f"guess") + f"guess" + ) try: mime_type = image.guess_type(io.BytesIO(decoded)) @@ -168,11 +168,7 @@ elif elem.name == "NICKNAME": nickname = vcard_dict["nickname"] = str(elem) await self._i.update( - client, - IMPORT_NAME, - "nicknames", - [nickname], - entity_jid + client, IMPORT_NAME, "nicknames", [nickname], entity_jid ) elif elem.name == "URL": vcard_dict["website"] = str(elem) @@ -196,7 +192,8 @@ vcard_dict["avatar"] = avatar_hash if avatar_hash is not None: await client._xep_0054_avatar_hashes.aset( - entity_jid.full(), avatar_hash) + entity_jid.full(), avatar_hash + ) if avatar_hash: avatar_cache = self.host.common_cache.get_metadata(avatar_hash) @@ -205,18 +202,21 @@ IMPORT_NAME, "avatar", { - 'path': avatar_cache['path'], - 'filename': avatar_cache['filename'], - 'media_type': avatar_cache['mime_type'], - 'cache_uid': avatar_hash + "path": avatar_cache["path"], + "filename": avatar_cache["filename"], + "media_type": avatar_cache["mime_type"], + "cache_uid": avatar_hash, }, - entity_jid + entity_jid, ) else: await self._i.update( - client, IMPORT_NAME, "avatar", None, entity_jid) + client, IMPORT_NAME, "avatar", None, entity_jid + ) else: - log.debug("FIXME: [{}] VCard_elt tag is not managed yet".format(elem.name)) + log.debug( + "FIXME: [{}] VCard_elt tag is not managed yet".format(elem.name) + ) return vcard_dict @@ -234,9 +234,11 @@ try: return next(iq_ret_elt.elements(NS_VCARD, "vCard")) except StopIteration: - log.warning(_( - "vCard element not found for {entity_jid}: {xml}" - ).format(entity_jid=entity_jid, xml=iq_ret_elt.toXml())) + log.warning( + _("vCard element not found for {entity_jid}: {xml}").format( + entity_jid=entity_jid, xml=iq_ret_elt.toXml() + ) + ) raise exceptions.DataError(f"no vCard element found for {entity_jid}") async def update_vcard_elt(self, client, entity_jid, to_replace): @@ -279,18 +281,18 @@ except exceptions.DataError: self._i.update(client, IMPORT_NAME, "avatar", None, entity_jid) except Exception as e: - log.warning(_( - "Can't get vCard for {entity_jid}: {e}" - ).format(entity_jid=entity_jid, e=e)) + log.warning( + _("Can't get vCard for {entity_jid}: {e}").format( + entity_jid=entity_jid, e=e + ) + ) else: log.debug(_("VCard found")) return await self.v_card_2_dict(client, vcard_elt, entity_jid) async def get_avatar( - self, - client: SatXMPPEntity, - entity_jid: jid.JID - ) -> Optional[dict]: + self, client: SatXMPPEntity, entity_jid: jid.JID + ) -> Optional[dict]: """Get avatar data @param entity: entity to get avatar from @@ -304,9 +306,10 @@ try: avatar_hash = hashes_cache[entity_jid.full()] except KeyError: - if 'avatar' in vcard: + if "avatar" in vcard: raise exceptions.InternalError( - "No avatar hash while avatar is found in vcard") + "No avatar hash while avatar is found in vcard" + ) return None if not avatar_hash: @@ -314,7 +317,8 @@ avatar_cache = self.host.common_cache.get_metadata(avatar_hash) return self._i.avatar_build_metadata( - avatar_cache['path'], avatar_cache['mime_type'], avatar_hash) + avatar_cache["path"], avatar_cache["mime_type"], avatar_hash + ) async def set_avatar(self, client, avatar_data, entity): """Set avatar of the profile @@ -323,7 +327,7 @@ IDENTITY plugin. @param entity(jid.JID): entity whose avatar must be changed """ - vcard_elt = await self.update_vcard_elt(client, entity, ['PHOTO']) + vcard_elt = await self.update_vcard_elt(client, entity, ["PHOTO"]) iq_elt = client.IQ() iq_elt.addChild(vcard_elt) @@ -345,7 +349,7 @@ """ vcard_data = await self.get_card(client, entity) try: - return [vcard_data['nickname']] + return [vcard_data["nickname"]] except (KeyError, TypeError): return [] @@ -357,7 +361,7 @@ """ nick = nicknames[0].strip() - vcard_elt = await self.update_vcard_elt(client, entity, ['NICKNAME']) + vcard_elt = await self.update_vcard_elt(client, entity, ["NICKNAME"]) if nick: vcard_elt.addElement((NS_VCARD, "NICKNAME"), content=nick) @@ -390,7 +394,8 @@ """ client = self.parent entity_jid = self.plugin_parent._i.get_identity_jid( - client, jid.JID(presence["from"])) + client, jid.JID(presence["from"]) + ) try: x_elt = next(presence.elements(NS_VCARD_UPDATE, "x")) @@ -434,7 +439,8 @@ if not given_hash: await self.plugin_parent._i.update( - client, IMPORT_NAME, "avatar", None, entity_jid) + client, IMPORT_NAME, "avatar", None, entity_jid + ) # the avatar has been removed, no need to go further return @@ -445,19 +451,18 @@ ) await self.plugin_parent._i.update( client, - IMPORT_NAME, "avatar", + IMPORT_NAME, + "avatar", { - 'path': avatar_cache['path'], - 'filename': avatar_cache['filename'], - 'media_type': avatar_cache['mime_type'], - 'cache_uid': given_hash, + "path": avatar_cache["path"], + "filename": avatar_cache["filename"], + "media_type": avatar_cache["mime_type"], + "cache_uid": given_hash, }, - entity_jid + entity_jid, ) else: - log.debug( - "New avatar found for [{entity_jid}], requesting vcard" - ) + log.debug("New avatar found for [{entity_jid}], requesting vcard") vcard = await self.plugin_parent.get_card(client, entity_jid) if vcard is None: log.warning(f"Unexpected empty vCard for {entity_jid}")