Mercurial > libervia-backend
diff libervia/backend/plugins/plugin_misc_attach.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_misc_attach.py Tue Jun 18 12:06:45 2024 +0200 +++ b/libervia/backend/plugins/plugin_misc_attach.py Wed Jun 19 18:44:57 2024 +0200 @@ -49,7 +49,7 @@ } -AttachmentHandler = namedtuple('AttachmentHandler', ['can_handle', 'attach', 'priority']) +AttachmentHandler = namedtuple("AttachmentHandler", ["can_handle", "attach", "priority"]) class AttachPlugin: @@ -60,7 +60,7 @@ self._u = host.plugins["UPLOAD"] host.trigger.add("sendMessage", self._send_message_trigger) host.trigger.add("sendMessageComponent", self._send_message_trigger) - self._attachments_handlers = {'clear': [], 'encrypted': []} + self._attachments_handlers = {"clear": [], "encrypted": []} self.register(self.default_can_handle, self.default_attach, False, -1000) def register(self, can_handle, attach, encrypted=False, priority=0): @@ -82,12 +82,13 @@ """ handler = AttachmentHandler(can_handle, attach, priority) handlers = ( - self._attachments_handlers['encrypted'] - if encrypted else self._attachments_handlers['clear'] + self._attachments_handlers["encrypted"] + if encrypted + else self._attachments_handlers["clear"] ) if handler in handlers: raise exceptions.InternalError( - 'Attachment handler has been registered twice, this should never happen' + "Attachment handler has been registered twice, this should never happen" ) handlers.append(handler) @@ -112,33 +113,40 @@ media_type = mimetypes.guess_type(path, strict=False)[0] if media_type is None: log.warning( - _("Can't resize attachment of unknown type: {attachment}") - .format(attachment=attachment)) + _( + "Can't resize attachment of unknown type: {attachment}" + ).format(attachment=attachment) + ) continue attachment[C.KEY_ATTACHMENTS_MEDIA_TYPE] = media_type - main_type = media_type.split('/')[0] + main_type = media_type.split("/")[0] if main_type == "image": report = image.check(self.host, path) - if report['too_large']: + if report["too_large"]: tmp_dir = Path(tempfile.mkdtemp()) tmp_dirs_to_clean.append(tmp_dir) new_path = tmp_dir / path.name await image.resize( - path, report["recommended_size"], dest=new_path) + path, report["recommended_size"], dest=new_path + ) attachment["path"] = new_path log.info( - _("Attachment {path!r} has been resized at {new_path!r}") - .format(path=str(path), new_path=str(new_path))) + _( + "Attachment {path!r} has been resized at {new_path!r}" + ).format(path=str(path), new_path=str(new_path)) + ) else: log.warning( - _("Can't resize attachment of type {main_type!r}: {attachment}") - .format(main_type=main_type, attachment=attachment)) + _( + "Can't resize attachment of type {main_type!r}: {attachment}" + ).format(main_type=main_type, attachment=attachment) + ) if client.encryption.is_encryption_requested(data): - handlers = self._attachments_handlers['encrypted'] + handlers = self._attachments_handlers["encrypted"] else: - handlers = self._attachments_handlers['clear'] + handlers = self._attachments_handlers["clear"] for handler in handlers: can_handle = await utils.as_deferred(handler.can_handle, client, data) @@ -147,8 +155,9 @@ else: raise exceptions.NotFound( _("No plugin can handle attachment with {destinee}").format( - destinee = data['to'] - )) + destinee=data["to"] + ) + ) await utils.as_deferred(handler.attach, client, data) @@ -159,10 +168,7 @@ return data async def upload_files( - self, - client: SatXMPPEntity, - data: dict, - upload_cb: Optional[Callable] = None + self, client: SatXMPPEntity, data: dict, upload_cb: Optional[Callable] = None ): """Upload file, and update attachments @@ -202,7 +208,7 @@ continue if "url" in attachment: - url = attachment.pop('url') + url = attachment.pop("url") log.warning( f"unexpected URL in attachment: {url!r}\nattachment: {attachment}" ) @@ -214,18 +220,18 @@ attachment["size"] = path.stat().st_size - extra = { - "attachment": attachment - } + extra = {"attachment": attachment} progress_id = attachment.pop("progress_id", None) if progress_id: extra["progress_id"] = progress_id check_certificate = self.host.memory.param_get_a( - "check_certificate", "Connection", profile_key=client.profile) + "check_certificate", "Connection", profile_key=client.profile + ) if not check_certificate: - extra['ignore_tls_errors'] = True + extra["ignore_tls_errors"] = True log.warning( - _("certificate check disabled for upload, this is dangerous!")) + _("certificate check disabled for upload, this is dangerous!") + ) __, upload_d = await upload_cb( client=client, @@ -255,8 +261,9 @@ return defer.ensureDeferred(self.attach_files(client, data)) def _send_message_trigger( - self, client, mess_data, pre_xml_treatments, post_xml_treatments): - if mess_data['extra'].get(C.KEY_ATTACHMENTS): + self, client, mess_data, pre_xml_treatments, post_xml_treatments + ): + if mess_data["extra"].get(C.KEY_ATTACHMENTS): post_xml_treatments.addCallback(self._attach_files, client=client) return True @@ -271,8 +278,8 @@ body_elt = data["xml"].addElement("body") attachments = data["extra"][C.KEY_ATTACHMENTS] if attachments: - body_links = '\n'.join(a['url'] for a in attachments) + body_links = "\n".join(a["url"] for a in attachments) if str(body_elt).strip(): # if there is already a body, we add a line feed before the first link - body_elt.addContent('\n') + body_elt.addContent("\n") body_elt.addContent(body_links)