Mercurial > libervia-backend
diff libervia/backend/plugins/plugin_misc_identity.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 5f2d496c633f |
children | ff88a807852d |
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_misc_identity.py Tue Jun 18 12:06:45 2024 +0200 +++ b/libervia/backend/plugins/plugin_misc_identity.py Wed Jun 19 18:44:57 2024 +0200 @@ -43,7 +43,6 @@ ) - log = getLogger(__name__) @@ -86,7 +85,7 @@ # (it is stored only for roster entities) "store": True, "store_serialisation": self._avatar_ser, - "store_deserialisation": self._avatar_deser + "store_deserialisation": self._avatar_deser, }, "nicknames": { "type": list, @@ -103,7 +102,7 @@ "get_all": True, "get_post_treatment": self.description_get_post_treatment, "store": True, - } + }, } host.trigger.add("roster_update", self._roster_update_trigger) host.memory.set_signal_on_update("avatar") @@ -161,14 +160,15 @@ client._identity_update_lock = [] # we restore known identities from database client._identity_storage = persistent.LazyPersistentBinaryDict( - "identity", client.profile) + "identity", client.profile + ) stored_data = await client._identity_storage.all() to_delete = [] for key, value in stored_data.items(): - entity_s, name = key.split('\n') + entity_s, name = key.split("\n") try: metadata = self.metadata[name] except KeyError: @@ -181,31 +181,31 @@ value = deser_method(value) entity = jid.JID(entity_s) - if name == 'avatar': + if name == "avatar": if value is not None: try: - cache_uid = value['cache_uid'] + cache_uid = value["cache_uid"] if not cache_uid: raise ValueError - filename = value['filename'] + filename = value["filename"] if not filename: raise ValueError except (ValueError, KeyError): log.warning( f"invalid data for {entity} avatar, it will be deleted: " - f"{value}") + f"{value}" + ) to_delete.append(key) continue cache = self.host.common_cache.get_metadata(cache_uid) if cache is None: log.debug( - f"purging avatar for {entity}: it is not in cache anymore") + f"purging avatar for {entity}: it is not in cache anymore" + ) to_delete.append(key) continue - self.host.memory.update_entity_data( - client, entity, name, value, silent=True - ) + self.host.memory.update_entity_data(client, entity, name, value, silent=True) for key in to_delete: await client._identity_storage.adel(key) @@ -219,22 +219,19 @@ ) defer.ensureDeferred( self.update( - client, - IMPORT_NAME, - "nicknames", - [roster_item.name], - roster_item.jid + client, IMPORT_NAME, "nicknames", [roster_item.name], roster_item.jid ) ) return True def register( - self, - origin: str, - metadata_name: str, - cb_get: Union[Coroutine, defer.Deferred], - cb_set: Union[Coroutine, defer.Deferred], - priority: int=0): + self, + origin: str, + metadata_name: str, + cb_get: Union[Coroutine, defer.Deferred], + cb_set: Union[Coroutine, defer.Deferred], + priority: int = 0, + ): """Register callbacks to handle identity metadata @param origin: namespace of the plugin managing this metadata @@ -251,7 +248,7 @@ if not metadata_name in self.metadata.keys(): raise ValueError(f"Invalid metadata_name: {metadata_name!r}") callback = Callback(origin=origin, get=cb_get, set=cb_set, priority=priority) - cb_list = self.metadata[metadata_name].setdefault('callbacks', []) + cb_list = self.metadata[metadata_name].setdefault("callbacks", []) cb_list.append(callback) cb_list.sort(key=lambda c: c.priority, reverse=True) @@ -276,7 +273,8 @@ if not isinstance(value, value_type): raise ValueError( f"{value} has wrong type: it is {type(value)} while {value_type} was " - f"expected") + f"expected" + ) def get_field_type(self, metadata_name: str) -> str: """Return the type the requested field @@ -287,13 +285,13 @@ return self.metadata[metadata_name]["type"] async def get( - self, - client: SatXMPPEntity, - metadata_name: str, - entity: Optional[jid.JID], - use_cache: bool=True, - prefilled_values: Optional[Dict[str, Any]]=None - ): + self, + client: SatXMPPEntity, + metadata_name: str, + entity: Optional[jid.JID], + use_cache: bool = True, + prefilled_values: Optional[Dict[str, Any]] = None, + ): """Retrieve identity metadata of an entity if metadata is already in cache, it is returned. Otherwise, registered callbacks @@ -311,28 +309,31 @@ metadata = self.metadata[metadata_name] except KeyError: raise ValueError(f"Invalid metadata name: {metadata_name!r}") - get_all = metadata.get('get_all', False) + get_all = metadata.get("get_all", False) if use_cache: try: - data = self.host.memory.get_entity_datum( - client, entity, metadata_name) + data = self.host.memory.get_entity_datum(client, entity, metadata_name) except (KeyError, exceptions.UnknownEntityError): pass else: return data try: - callbacks = metadata['callbacks'] + callbacks = metadata["callbacks"] except KeyError: - log.warning(_("No callback registered for {metadata_name}") - .format(metadata_name=metadata_name)) + log.warning( + _("No callback registered for {metadata_name}").format( + metadata_name=metadata_name + ) + ) return [] if get_all else None if get_all: all_data = [] elif prefilled_values is not None: raise exceptions.InternalError( - "prefilled_values can only be used when `get_all` is set") + "prefilled_values can only be used when `get_all` is set" + ) for callback in callbacks: try: @@ -340,15 +341,18 @@ data = prefilled_values[callback.origin] log.debug( f"using prefilled values {data!r} for {metadata_name} with " - f"{callback.origin}") + f"{callback.origin}" + ) else: data = await defer.ensureDeferred(callback.get(client, entity)) except exceptions.CancelError: continue except Exception as e: log.warning( - _("Error while trying to get {metadata_name} with {callback}: {e}") - .format(callback=callback.get, metadata_name=metadata_name, e=e)) + _( + "Error while trying to get {metadata_name} with {callback}: {e}" + ).format(callback=callback.get, metadata_name=metadata_name, e=e) + ) else: if data: self.check_type(metadata_name, data) @@ -369,10 +373,9 @@ if post_treatment is not None: data = await utils.as_deferred(post_treatment, client, entity, data) - self.host.memory.update_entity_data( - client, entity, metadata_name, data) + self.host.memory.update_entity_data(client, entity, metadata_name, data) - if metadata.get('store', False): + if metadata.get("store", False): if data is not None: ser_method = metadata.get("store_serialisation") if ser_method is not None: @@ -401,10 +404,13 @@ self.check_type(metadata_name, data) try: - callbacks = metadata['callbacks'] + callbacks = metadata["callbacks"] except KeyError: - log.warning(_("No callback registered for {metadata_name}") - .format(metadata_name=metadata_name)) + log.warning( + _("No callback registered for {metadata_name}").format( + metadata_name=metadata_name + ) + ) return exceptions.FeatureNotFound(f"Can't set {metadata_name} for {entity}") for callback in callbacks: @@ -414,8 +420,10 @@ continue except Exception as e: log.warning( - _("Error while trying to set {metadata_name} with {callback}: {e}") - .format(callback=callback.set, metadata_name=metadata_name, e=e)) + _( + "Error while trying to set {metadata_name} with {callback}: {e}" + ).format(callback=callback.set, metadata_name=metadata_name, e=e) + ) else: break else: @@ -431,7 +439,7 @@ origin: str, metadata_name: str, data: Any, - entity: Optional[jid.JID] + entity: Optional[jid.JID], ): """Update a metadata in cache @@ -445,8 +453,7 @@ metadata = self.metadata[metadata_name] try: - cached_data = self.host.memory.get_entity_datum( - client, entity, metadata_name) + cached_data = self.host.memory.get_entity_datum(client, entity, metadata_name) except (KeyError, exceptions.UnknownEntityError): # metadata is not cached, we do the update pass @@ -461,21 +468,22 @@ if cached_data is None: log.debug( f"{metadata_name} for {entity} is already disabled, nothing to " - f"do") + f"do" + ) return elif cached_data is None: pass elif not update_is_new_data(client, entity, cached_data, data): log.debug( - f"{metadata_name} for {entity} is already in cache, nothing to " - f"do") + f"{metadata_name} for {entity} is already in cache, nothing to " f"do" + ) return # we can't use the cache, so we do the update log.debug(f"updating {metadata_name} for {entity}") - if metadata.get('get_all', False): + if metadata.get("get_all", False): # get_all is set, meaning that we have to check all plugins # so we first delete current cache try: @@ -491,14 +499,14 @@ return if data is not None: - data_filter = metadata['update_data_filter'] + data_filter = metadata["update_data_filter"] if data_filter is not None: data = await utils.as_deferred(data_filter, client, entity, data) self.check_type(metadata_name, data) self.host.memory.update_entity_data(client, entity, metadata_name, data) - if metadata.get('store', False): + if metadata.get("store", False): key = f"{entity}\n{metadata_name}" if data is not None: ser_method = metadata.get("store_serialisation") @@ -519,14 +527,9 @@ def _set_avatar(self, file_path, entity, profile_key=C.PROF_KEY_NONE): client = self.host.get_client(profile_key) entity = jid.JID(entity) if entity else None - return defer.ensureDeferred( - self.set(client, "avatar", file_path, entity)) + return defer.ensureDeferred(self.set(client, "avatar", file_path, entity)) - def _blocking_cache_avatar( - self, - source: str, - avatar_data: dict[str, Any] - ): + def _blocking_cache_avatar(self, source: str, avatar_data: dict[str, Any]): """This method is executed in a separated thread""" if avatar_data["media_type"] == "image/svg+xml": # for vector image, we save directly @@ -562,13 +565,11 @@ img_buf.seek(0) image_hash = hashlib.sha1(img_buf.read()).hexdigest() img_buf.seek(0) - with self.host.common_cache.cache_data( - source, image_hash, media_type - ) as f: + with self.host.common_cache.cache_data(source, image_hash, media_type) as f: f.write(img_buf.read()) - avatar_data['path'] = Path(f.name) - avatar_data['filename'] = avatar_data['path'].name - avatar_data['cache_uid'] = image_hash + avatar_data["path"] = Path(f.name) + avatar_data["filename"] = avatar_data["path"].name + avatar_data["cache_uid"] = image_hash async def cache_avatar(self, source: str, avatar_data: Dict[str, Any]) -> None: """Resize if necessary and cache avatar @@ -590,14 +591,14 @@ if not file_path.is_file(): raise ValueError(f"There is no file at {file_path} to use as avatar") avatar_data = { - 'path': file_path, - 'filename': file_path.name, - 'media_type': image.guess_type(file_path), + "path": file_path, + "filename": file_path.name, + "media_type": image.guess_type(file_path), } - media_type = avatar_data['media_type'] + media_type = avatar_data["media_type"] if media_type is None: raise ValueError(f"Can't identify type of image at {file_path}") - if not media_type.startswith('image/'): + if not media_type.startswith("image/"): raise ValueError(f"File at {file_path} doesn't appear to be an image") await self.cache_avatar(IMPORT_NAME, avatar_data) return avatar_data @@ -607,10 +608,10 @@ await self.update(client, IMPORT_NAME, "avatar", avatar_data, entity) def avatar_build_metadata( - self, - path: Path, - media_type: Optional[str] = None, - cache_uid: Optional[str] = None + self, + path: Path, + media_type: Optional[str] = None, + cache_uid: Optional[str] = None, ) -> Optional[Dict[str, Union[str, Path, None]]]: """Helper method to generate avatar metadata @@ -639,12 +640,12 @@ } def avatar_update_is_new_data(self, client, entity, cached_data, new_data): - return new_data['path'] != cached_data['path'] + return new_data["path"] != cached_data["path"] async def avatar_update_data_filter(self, client, entity, data): if not isinstance(data, dict): raise ValueError(f"Invalid data type ({type(data)}), a dict is expected") - mandatory_keys = {'path', 'filename', 'cache_uid'} + mandatory_keys = {"path", "filename", "cache_uid"} if not data.keys() >= mandatory_keys: raise ValueError(f"missing avatar data keys: {mandatory_keys - data.keys()}") return data @@ -702,19 +703,17 @@ return not set(new_nicknames).issubset(cached_data) async def description_get_post_treatment( - self, - client: SatXMPPEntity, - entity: jid.JID, - plugin_description: List[str] + self, client: SatXMPPEntity, entity: jid.JID, plugin_description: List[str] ) -> str: """Join all descriptions in a unique string""" - return '\n'.join(plugin_description) + return "\n".join(plugin_description) def _get_identity(self, entity_s, metadata_filter, use_cache, profile): entity = jid.JID(entity_s) client = self.host.get_client(profile) d = defer.ensureDeferred( - self.get_identity(client, entity, metadata_filter, use_cache)) + self.get_identity(client, entity, metadata_filter, use_cache) + ) d.addCallback(data_format.serialise) return d @@ -723,7 +722,7 @@ client: SatXMPPEntity, entity: Optional[jid.JID] = None, metadata_filter: Optional[List[str]] = None, - use_cache: bool = True + use_cache: bool = True, ) -> Dict[str, Any]: """Retrieve identity of an entity @@ -743,7 +742,8 @@ for metadata_name in metadata_names: id_data[metadata_name] = await self.get( - client, metadata_name, entity, use_cache) + client, metadata_name, entity, use_cache + ) return id_data @@ -751,7 +751,7 @@ entities = [jid.JID(e) for e in entities_s] client = self.host.get_client(profile) d = defer.ensureDeferred(self.get_identities(client, entities, metadata_filter)) - d.addCallback(lambda d: data_format.serialise({str(j):i for j, i in d.items()})) + d.addCallback(lambda d: data_format.serialise({str(j): i for j, i in d.items()})) return d async def get_identities( @@ -792,7 +792,7 @@ def _get_base_identities(self, profile_key): client = self.host.get_client(profile_key) d = defer.ensureDeferred(self.get_base_identities(client)) - d.addCallback(lambda d: data_format.serialise({str(j):i for j, i in d.items()})) + d.addCallback(lambda d: data_format.serialise({str(j): i for j, i in d.items()})) return d async def get_base_identities( @@ -809,11 +809,7 @@ else: entities = client.roster.get_jids() + [client.jid.userhostJID()] - return await self.get_identities( - client, - entities, - ['avatar', 'nicknames'] - ) + return await self.get_identities(client, entities, ["avatar", "nicknames"]) def _set_identity(self, id_data_s, profile): client = self.host.get_client(profile) @@ -827,11 +823,14 @@ """ if not id_data.keys() <= self.metadata.keys(): raise ValueError( - f"Invalid metadata names: {id_data.keys() - self.metadata.keys()}") + f"Invalid metadata names: {id_data.keys() - self.metadata.keys()}" + ) for metadata_name, data in id_data.items(): try: await self.set(client, metadata_name, data) except Exception as e: log.warning( - _("Can't set metadata {metadata_name!r}: {reason}") - .format(metadata_name=metadata_name, reason=e)) + _("Can't set metadata {metadata_name!r}: {reason}").format( + metadata_name=metadata_name, reason=e + ) + )