Mercurial > libervia-backend
comparison sat/plugins/plugin_misc_attach.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | 78b5f356900c |
children |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
56 | 56 |
57 def __init__(self, host): | 57 def __init__(self, host): |
58 log.info(_("plugin Attach initialization")) | 58 log.info(_("plugin Attach initialization")) |
59 self.host = host | 59 self.host = host |
60 self._u = host.plugins["UPLOAD"] | 60 self._u = host.plugins["UPLOAD"] |
61 host.trigger.add("sendMessage", self._sendMessageTrigger) | 61 host.trigger.add("sendMessage", self._send_message_trigger) |
62 host.trigger.add("sendMessageComponent", self._sendMessageTrigger) | 62 host.trigger.add("sendMessageComponent", self._send_message_trigger) |
63 self._attachments_handlers = {'clear': [], 'encrypted': []} | 63 self._attachments_handlers = {'clear': [], 'encrypted': []} |
64 self.register(self.defaultCanHandle, self.defaultAttach, False, -1000) | 64 self.register(self.default_can_handle, self.default_attach, False, -1000) |
65 | 65 |
66 def register(self, can_handle, attach, encrypted=False, priority=0): | 66 def register(self, can_handle, attach, encrypted=False, priority=0): |
67 """Register an attachments handler | 67 """Register an attachments handler |
68 | 68 |
69 @param can_handle(callable, coroutine, Deferred): a method which must return True | 69 @param can_handle(callable, coroutine, Deferred): a method which must return True |
92 | 92 |
93 handlers.append(handler) | 93 handlers.append(handler) |
94 handlers.sort(key=lambda h: h.priority, reverse=True) | 94 handlers.sort(key=lambda h: h.priority, reverse=True) |
95 log.debug(f"new attachments handler: {handler}") | 95 log.debug(f"new attachments handler: {handler}") |
96 | 96 |
97 async def attachFiles(self, client, data): | 97 async def attach_files(self, client, data): |
98 """Main method to attach file | 98 """Main method to attach file |
99 | 99 |
100 It will do generic pre-treatment, and call the suitable attachments handler | 100 It will do generic pre-treatment, and call the suitable attachments handler |
101 """ | 101 """ |
102 # we check attachment for pre-treatment like large image resizing | 102 # we check attachment for pre-treatment like large image resizing |
133 else: | 133 else: |
134 log.warning( | 134 log.warning( |
135 _("Can't resize attachment of type {main_type!r}: {attachment}") | 135 _("Can't resize attachment of type {main_type!r}: {attachment}") |
136 .format(main_type=main_type, attachment=attachment)) | 136 .format(main_type=main_type, attachment=attachment)) |
137 | 137 |
138 if client.encryption.isEncryptionRequested(data): | 138 if client.encryption.is_encryption_requested(data): |
139 handlers = self._attachments_handlers['encrypted'] | 139 handlers = self._attachments_handlers['encrypted'] |
140 else: | 140 else: |
141 handlers = self._attachments_handlers['clear'] | 141 handlers = self._attachments_handlers['clear'] |
142 | 142 |
143 for handler in handlers: | 143 for handler in handlers: |
144 can_handle = await utils.asDeferred(handler.can_handle, client, data) | 144 can_handle = await utils.as_deferred(handler.can_handle, client, data) |
145 if can_handle: | 145 if can_handle: |
146 break | 146 break |
147 else: | 147 else: |
148 raise exceptions.NotFound( | 148 raise exceptions.NotFound( |
149 _("No plugin can handle attachment with {destinee}").format( | 149 _("No plugin can handle attachment with {destinee}").format( |
150 destinee = data['to'] | 150 destinee = data['to'] |
151 )) | 151 )) |
152 | 152 |
153 await utils.asDeferred(handler.attach, client, data) | 153 await utils.as_deferred(handler.attach, client, data) |
154 | 154 |
155 for dir_path in tmp_dirs_to_clean: | 155 for dir_path in tmp_dirs_to_clean: |
156 log.debug(f"Cleaning temporary directory at {dir_path}") | 156 log.debug(f"Cleaning temporary directory at {dir_path}") |
157 shutil.rmtree(dir_path) | 157 shutil.rmtree(dir_path) |
158 | 158 |
218 "attachment": attachment | 218 "attachment": attachment |
219 } | 219 } |
220 progress_id = attachment.pop("progress_id", None) | 220 progress_id = attachment.pop("progress_id", None) |
221 if progress_id: | 221 if progress_id: |
222 extra["progress_id"] = progress_id | 222 extra["progress_id"] = progress_id |
223 check_certificate = self.host.memory.getParamA( | 223 check_certificate = self.host.memory.param_get_a( |
224 "check_certificate", "Connection", profile_key=client.profile) | 224 "check_certificate", "Connection", profile_key=client.profile) |
225 if not check_certificate: | 225 if not check_certificate: |
226 extra['ignore_tls_errors'] = True | 226 extra['ignore_tls_errors'] = True |
227 log.warning( | 227 log.warning( |
228 _("certificate check disabled for upload, this is dangerous!")) | 228 _("certificate check disabled for upload, this is dangerous!")) |
249 | 249 |
250 attachment["url"] = ret | 250 attachment["url"] = ret |
251 | 251 |
252 return data | 252 return data |
253 | 253 |
254 def _attachFiles(self, data, client): | 254 def _attach_files(self, data, client): |
255 return defer.ensureDeferred(self.attachFiles(client, data)) | 255 return defer.ensureDeferred(self.attach_files(client, data)) |
256 | 256 |
257 def _sendMessageTrigger( | 257 def _send_message_trigger( |
258 self, client, mess_data, pre_xml_treatments, post_xml_treatments): | 258 self, client, mess_data, pre_xml_treatments, post_xml_treatments): |
259 if mess_data['extra'].get(C.KEY_ATTACHMENTS): | 259 if mess_data['extra'].get(C.KEY_ATTACHMENTS): |
260 post_xml_treatments.addCallback(self._attachFiles, client=client) | 260 post_xml_treatments.addCallback(self._attach_files, client=client) |
261 return True | 261 return True |
262 | 262 |
263 async def defaultCanHandle(self, client, data): | 263 async def default_can_handle(self, client, data): |
264 return True | 264 return True |
265 | 265 |
266 async def defaultAttach(self, client, data): | 266 async def default_attach(self, client, data): |
267 await self.upload_files(client, data) | 267 await self.upload_files(client, data) |
268 # TODO: handle xhtml-im | 268 # TODO: handle xhtml-im |
269 body_elt = data["xml"].body | 269 body_elt = data["xml"].body |
270 if body_elt is None: | 270 if body_elt is None: |
271 body_elt = data["xml"].addElement("body") | 271 body_elt = data["xml"].addElement("body") |