Mercurial > libervia-backend
diff sat/plugins/plugin_misc_file.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 | 4c3361e2bf55 |
children | 877145b4ba01 |
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_file.py Fri Apr 07 15:18:39 2023 +0200 +++ b/sat/plugins/plugin_misc_file.py Sat Apr 08 13:54:42 2023 +0200 @@ -69,24 +69,24 @@ def __init__(self, host): log.info(_("plugin File initialization")) self.host = host - host.bridge.addMethod( - "fileSend", + host.bridge.add_method( + "file_send", ".plugin", in_sign="ssssss", out_sign="a{ss}", - method=self._fileSend, + method=self._file_send, async_=True, ) self._file_managers = [] - host.importMenu( + host.import_menu( (D_("Action"), D_("send file")), - self._fileSendMenu, + self._file_send_menu, security_limit=10, help_string=D_("Send a file"), type_=C.MENU_SINGLE, ) - def _fileSend( + def _file_send( self, peer_jid_s: str, filepath: str, @@ -95,13 +95,13 @@ extra_s: str, profile: str = C.PROF_KEY_NONE ) -> defer.Deferred: - client = self.host.getClient(profile) - return defer.ensureDeferred(self.fileSend( + client = self.host.get_client(profile) + return defer.ensureDeferred(self.file_send( client, jid.JID(peer_jid_s), filepath, name or None, file_desc or None, data_format.deserialise(extra_s) )) - async def fileSend( + async def file_send( self, client, peer_jid, filepath, filename=None, file_desc=None, extra=None ): """Send a file using best available method @@ -119,7 +119,7 @@ if not filename: filename = os.path.basename(filepath) or "_" for manager, priority in self._file_managers: - if await utils.asDeferred(manager.canHandleFileSend, + if await utils.as_deferred(manager.can_handle_file_send, client, peer_jid, filepath): try: method_name = manager.name @@ -131,8 +131,8 @@ ) ) try: - progress_id = await utils.asDeferred( - manager.fileSend, client, peer_jid, filepath, filename, file_desc, + progress_id = await utils.as_deferred( + manager.file_send, client, peer_jid, filepath, filename, file_desc, extra ) except Exception as e: @@ -155,15 +155,15 @@ ).toXml() } - def _onFileChoosed(self, peer_jid, data, profile): - client = self.host.getClient(profile) + def _on_file_choosed(self, peer_jid, data, profile): + client = self.host.get_client(profile) cancelled = C.bool(data.get("cancelled", C.BOOL_FALSE)) if cancelled: return path = data["path"] - return self.fileSend(client, peer_jid, path) + return self.file_send(client, peer_jid, path) - def _fileSendMenu(self, data, profile): + def _file_send_menu(self, data, profile): """ XMLUI activated by menu: return file sending UI @param profile: %(doc_profile)s @@ -173,8 +173,8 @@ except RuntimeError: raise exceptions.DataError(_("Invalid JID")) - file_choosed_id = self.host.registerCallback( - partial(self._onFileChoosed, jid_), + file_choosed_id = self.host.register_callback( + partial(self._on_file_choosed, jid_), with_data=True, one_shot=True, ) @@ -193,7 +193,7 @@ def register(self, manager, priority: int = 0) -> None: """Register a fileSending manager - @param manager: object implementing canHandleFileSend, and fileSend methods + @param manager: object implementing can_handle_file_send, and file_send methods @param priority: pririoty of this manager, the higher available will be used """ m_data = (manager, priority) @@ -201,9 +201,9 @@ raise exceptions.ConflictError( f"Manager {manager} is already registered" ) - if not hasattr(manager, "canHandleFileSend") or not hasattr(manager, "fileSend"): + if not hasattr(manager, "can_handle_file_send") or not hasattr(manager, "file_send"): raise ValueError( - f'{manager} must have both "canHandleFileSend" and "fileSend" methods to ' + f'{manager} must have both "can_handle_file_send" and "file_send" methods to ' 'be registered') self._file_managers.append(m_data) self._file_managers.sort(key=lambda m: m[1], reverse=True) @@ -219,7 +219,7 @@ # Dialogs with user # the overwrite check is done here - def openFileWrite(self, client, file_path, transfer_data, file_data, stream_object): + def open_file_write(self, client, file_path, transfer_data, file_data, stream_object): """create SatFile or FileStremaObject for the requested file and fill suitable data """ if stream_object: @@ -245,15 +245,15 @@ data_cb=file_data.get("data_cb"), ) - async def _gotConfirmation( + async def _got_confirmation( self, client, data, peer_jid, transfer_data, file_data, stream_object ): """Called when the permission and dest path have been received @param peer_jid(jid.JID): jid of the file sender - @param transfer_data(dict): same as for [self.getDestDir] - @param file_data(dict): same as for [self.getDestDir] - @param stream_object(bool): same as for [self.getDestDir] + @param transfer_data(dict): same as for [self.get_dest_dir] + @param file_data(dict): same as for [self.get_dest_dir] + @param stream_object(bool): same as for [self.get_dest_dir] return (bool): True if copy is wanted and OK False if user wants to cancel if file exists ask confirmation and call again self._getDestDir if needed @@ -266,7 +266,7 @@ # we manage case where file already exists if os.path.exists(file_path): - overwrite = await xml_tools.deferConfirm( + overwrite = await xml_tools.defer_confirm( self.host, _(CONFIRM_OVERWRITE).format(file_path), _(CONFIRM_OVERWRITE_TITLE), @@ -280,12 +280,12 @@ ) if not overwrite: - return await self.getDestDir(client, peer_jid, transfer_data, file_data) + return await self.get_dest_dir(client, peer_jid, transfer_data, file_data) - self.openFileWrite(client, file_path, transfer_data, file_data, stream_object) + self.open_file_write(client, file_path, transfer_data, file_data, stream_object) return True - async def getDestDir( + async def get_dest_dir( self, client, peer_jid, transfer_data, file_data, stream_object=False ): """Request confirmation and destination dir to user @@ -296,7 +296,7 @@ @param filename(unicode): name of the file @param transfer_data(dict): data of the transfer session, it will be only used to store the file_obj. - "file_obj" (or "stream_object") key *MUST NOT* exist before using getDestDir + "file_obj" (or "stream_object") key *MUST NOT* exist before using get_dest_dir @param file_data(dict): information about the file to be transfered It MUST contain the following keys: - peer_jid (jid.JID): other peer jid @@ -314,7 +314,7 @@ a stream.FileStreamObject will be used return: True if transfer is accepted """ - cont, ret_value = await self.host.trigger.asyncReturnPoint( + cont, ret_value = await self.host.trigger.async_return_point( "FILE_getDestDir", client, peer_jid, transfer_data, file_data, stream_object ) if not cont: @@ -323,8 +323,8 @@ assert filename and not "/" in filename assert PROGRESS_ID_KEY in file_data # human readable size - file_data["size_human"] = common_utils.getHumanSize(file_data["size"]) - resp_data = await xml_tools.deferDialog( + file_data["size_human"] = common_utils.get_human_size(file_data["size"]) + resp_data = await xml_tools.defer_dialog( self.host, _(CONFIRM).format(peer=peer_jid.full(), **file_data), _(CONFIRM_TITLE), @@ -339,7 +339,7 @@ profile=client.profile, ) - accepted = await self._gotConfirmation( + accepted = await self._got_confirmation( client, resp_data, peer_jid,