comparison sat/plugins/plugin_misc_file.py @ 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 849374e59178
children 4c3361e2bf55
comparison
equal deleted inserted replaced
3654:3910ad643e9d 3655:ca980569318c
235 uid=file_data[PROGRESS_ID_KEY], 235 uid=file_data[PROGRESS_ID_KEY],
236 size=file_data["size"], 236 size=file_data["size"],
237 data_cb=file_data.get("data_cb"), 237 data_cb=file_data.get("data_cb"),
238 ) 238 )
239 239
240 def _gotConfirmation( 240 async def _gotConfirmation(
241 self, data, client, peer_jid, transfer_data, file_data, stream_object 241 self, client, data, peer_jid, transfer_data, file_data, stream_object
242 ): 242 ):
243 """Called when the permission and dest path have been received 243 """Called when the permission and dest path have been received
244 244
245 @param peer_jid(jid.JID): jid of the file sender 245 @param peer_jid(jid.JID): jid of the file sender
246 @param transfer_data(dict): same as for [self.getDestDir] 246 @param transfer_data(dict): same as for [self.getDestDir]
256 file_data["file_path"] = file_path = os.path.join(path, file_data["name"]) 256 file_data["file_path"] = file_path = os.path.join(path, file_data["name"])
257 log.debug("destination file path set to {}".format(file_path)) 257 log.debug("destination file path set to {}".format(file_path))
258 258
259 # we manage case where file already exists 259 # we manage case where file already exists
260 if os.path.exists(file_path): 260 if os.path.exists(file_path):
261 261 overwrite = await xml_tools.deferConfirm(
262 def check_overwrite(overwrite):
263 if overwrite:
264 self.openFileWrite(
265 client, file_path, transfer_data, file_data, stream_object
266 )
267 return True
268 else:
269 return defer.ensureDeferred(
270 self.getDestDir(client, peer_jid, transfer_data, file_data)
271 )
272
273 exists_d = xml_tools.deferConfirm(
274 self.host, 262 self.host,
275 _(CONFIRM_OVERWRITE).format(file_path), 263 _(CONFIRM_OVERWRITE).format(file_path),
276 _(CONFIRM_OVERWRITE_TITLE), 264 _(CONFIRM_OVERWRITE_TITLE),
277 action_extra={ 265 action_extra={
278 "meta_from_jid": peer_jid.full(), 266 "meta_from_jid": peer_jid.full(),
280 "meta_progress_id": file_data[PROGRESS_ID_KEY], 268 "meta_progress_id": file_data[PROGRESS_ID_KEY],
281 }, 269 },
282 security_limit=SECURITY_LIMIT, 270 security_limit=SECURITY_LIMIT,
283 profile=client.profile, 271 profile=client.profile,
284 ) 272 )
285 exists_d.addCallback(check_overwrite) 273
286 return exists_d 274 if not overwrite:
275 return await self.getDestDir(client, peer_jid, transfer_data, file_data)
287 276
288 self.openFileWrite(client, file_path, transfer_data, file_data, stream_object) 277 self.openFileWrite(client, file_path, transfer_data, file_data, stream_object)
289 return True 278 return True
290 279
291 async def getDestDir( 280 async def getDestDir(
313 - data_cb (callable): method called on each data read/write 302 - data_cb (callable): method called on each data read/write
314 "file_path" will be added to this dict once destination selected 303 "file_path" will be added to this dict once destination selected
315 "size_human" will also be added with human readable file size 304 "size_human" will also be added with human readable file size
316 @param stream_object(bool): if True, a stream_object will be used instead of file_obj 305 @param stream_object(bool): if True, a stream_object will be used instead of file_obj
317 a stream.FileStreamObject will be used 306 a stream.FileStreamObject will be used
318 return (defer.Deferred): True if transfer is accepted 307 return: True if transfer is accepted
319 """ 308 """
320 cont, ret_value = await self.host.trigger.asyncReturnPoint( 309 cont, ret_value = await self.host.trigger.asyncReturnPoint(
321 "FILE_getDestDir", client, peer_jid, transfer_data, file_data, stream_object 310 "FILE_getDestDir", client, peer_jid, transfer_data, file_data, stream_object
322 ) 311 )
323 if not cont: 312 if not cont:
325 filename = file_data["name"] 314 filename = file_data["name"]
326 assert filename and not "/" in filename 315 assert filename and not "/" in filename
327 assert PROGRESS_ID_KEY in file_data 316 assert PROGRESS_ID_KEY in file_data
328 # human readable size 317 # human readable size
329 file_data["size_human"] = common_utils.getHumanSize(file_data["size"]) 318 file_data["size_human"] = common_utils.getHumanSize(file_data["size"])
330 d = xml_tools.deferDialog( 319 resp_data = await xml_tools.deferDialog(
331 self.host, 320 self.host,
332 _(CONFIRM).format(peer=peer_jid.full(), **file_data), 321 _(CONFIRM).format(peer=peer_jid.full(), **file_data),
333 _(CONFIRM_TITLE), 322 _(CONFIRM_TITLE),
334 type_=C.XMLUI_DIALOG_FILE, 323 type_=C.XMLUI_DIALOG_FILE,
335 options={C.XMLUI_DATA_FILETYPE: C.XMLUI_DATA_FILETYPE_DIR}, 324 options={C.XMLUI_DATA_FILETYPE: C.XMLUI_DATA_FILETYPE_DIR},
339 "meta_progress_id": file_data[PROGRESS_ID_KEY], 328 "meta_progress_id": file_data[PROGRESS_ID_KEY],
340 }, 329 },
341 security_limit=SECURITY_LIMIT, 330 security_limit=SECURITY_LIMIT,
342 profile=client.profile, 331 profile=client.profile,
343 ) 332 )
344 d.addCallback( 333
345 self._gotConfirmation, 334 accepted = await self._gotConfirmation(
346 client, 335 client,
336 resp_data,
347 peer_jid, 337 peer_jid,
348 transfer_data, 338 transfer_data,
349 file_data, 339 file_data,
350 stream_object, 340 stream_object,
351 ) 341 )
352 return d 342 return accepted