changeset 3655:ca980569318c

plugin misc file: fix `getDestDir` returning a Deferred in a coroutine
author Goffi <goffi@goffi.org>
date Wed, 08 Sep 2021 11:17:19 +0200
parents 3910ad643e9d
children 015e13f88960
files sat/plugins/plugin_misc_file.py
diffstat 1 files changed, 12 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_file.py	Wed Sep 08 11:17:13 2021 +0200
+++ b/sat/plugins/plugin_misc_file.py	Wed Sep 08 11:17:19 2021 +0200
@@ -237,8 +237,8 @@
                 data_cb=file_data.get("data_cb"),
             )
 
-    def _gotConfirmation(
-        self, data, client, peer_jid, transfer_data, file_data, stream_object
+    async def _gotConfirmation(
+        self, client, data, peer_jid, transfer_data, file_data, stream_object
     ):
         """Called when the permission and dest path have been received
 
@@ -258,19 +258,7 @@
 
         # we manage case where file already exists
         if os.path.exists(file_path):
-
-            def check_overwrite(overwrite):
-                if overwrite:
-                    self.openFileWrite(
-                        client, file_path, transfer_data, file_data, stream_object
-                    )
-                    return True
-                else:
-                    return defer.ensureDeferred(
-                        self.getDestDir(client, peer_jid, transfer_data, file_data)
-                    )
-
-            exists_d = xml_tools.deferConfirm(
+            overwrite = await xml_tools.deferConfirm(
                 self.host,
                 _(CONFIRM_OVERWRITE).format(file_path),
                 _(CONFIRM_OVERWRITE_TITLE),
@@ -282,8 +270,9 @@
                 security_limit=SECURITY_LIMIT,
                 profile=client.profile,
             )
-            exists_d.addCallback(check_overwrite)
-            return exists_d
+
+            if not overwrite:
+                return await self.getDestDir(client, peer_jid, transfer_data, file_data)
 
         self.openFileWrite(client, file_path, transfer_data, file_data, stream_object)
         return True
@@ -315,7 +304,7 @@
             "size_human" will also be added with human readable file size
         @param stream_object(bool): if True, a stream_object will be used instead of file_obj
             a stream.FileStreamObject will be used
-        return (defer.Deferred): True if transfer is accepted
+        return: True if transfer is accepted
         """
         cont, ret_value = await self.host.trigger.asyncReturnPoint(
             "FILE_getDestDir", client, peer_jid, transfer_data, file_data, stream_object
@@ -327,7 +316,7 @@
         assert PROGRESS_ID_KEY in file_data
         # human readable size
         file_data["size_human"] = common_utils.getHumanSize(file_data["size"])
-        d = xml_tools.deferDialog(
+        resp_data = await xml_tools.deferDialog(
             self.host,
             _(CONFIRM).format(peer=peer_jid.full(), **file_data),
             _(CONFIRM_TITLE),
@@ -341,12 +330,13 @@
             security_limit=SECURITY_LIMIT,
             profile=client.profile,
         )
-        d.addCallback(
-            self._gotConfirmation,
+
+        accepted = await self._gotConfirmation(
             client,
+            resp_data,
             peer_jid,
             transfer_data,
             file_data,
             stream_object,
         )
-        return d
+        return accepted