comparison libervia/cli/cmd_file.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 79c8a70e1813
children
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
78 ) 78 )
79 self.parser.add_argument( 79 self.parser.add_argument(
80 "-e", 80 "-e",
81 "--encrypt", 81 "--encrypt",
82 action="store_true", 82 action="store_true",
83 help=_("end-to-end encrypt the file transfer") 83 help=_("end-to-end encrypt the file transfer"),
84 ) 84 )
85 self.parser.add_argument( 85 self.parser.add_argument(
86 "--webrtc", 86 "--webrtc", action="store_true", help=_("Use WebRTC Data Channel transport.")
87 action="store_true",
88 help=_("Use WebRTC Data Channel transport.")
89 ) 87 )
90 88
91 async def on_progress_started(self, metadata): 89 async def on_progress_started(self, metadata):
92 self.disp(_("File copy started"), 2) 90 self.disp(_("File copy started"), 2)
93 91
108 await self.set_progress_id(data["progress"]) 106 await self.set_progress_id(data["progress"])
109 except KeyError: 107 except KeyError:
110 # TODO: if 'xmlui' key is present, manage xmlui message display 108 # TODO: if 'xmlui' key is present, manage xmlui message display
111 self.disp(_("Can't send file to {jid}".format(jid=self.args.jid)), error=True) 109 self.disp(_("Can't send file to {jid}".format(jid=self.args.jid)), error=True)
112 self.host.quit(2) 110 self.host.quit(2)
113
114 111
115 async def start(self): 112 async def start(self):
116 file_ = None 113 file_ = None
117 for file_ in self.args.files: 114 for file_ in self.args.files:
118 if not os.path.exists(file_): 115 if not os.path.exists(file_):
168 if self.verbosity >= 1: 165 if self.verbosity >= 1:
169 root_logger.setLevel(logging.WARNING) 166 root_logger.setLevel(logging.WARNING)
170 if self.verbosity >= 2: 167 if self.verbosity >= 2:
171 root_logger.setLevel(logging.DEBUG) 168 root_logger.setLevel(logging.DEBUG)
172 from libervia.frontends.tools.webrtc_file import WebRTCFileSender 169 from libervia.frontends.tools.webrtc_file import WebRTCFileSender
170
173 aio.install_glib_asyncio_iteration() 171 aio.install_glib_asyncio_iteration()
174 file_sender = WebRTCFileSender( 172 file_sender = WebRTCFileSender(
175 self.host.bridge, 173 self.host.bridge,
176 self.profile, 174 self.profile,
177 on_call_start_cb=self.got_id, 175 on_call_start_cb=self.got_id,
178 end_call_cb=self.host.a_quit 176 end_call_cb=self.host.a_quit,
179 ) 177 )
180 await file_sender.send_file_webrtc( 178 await file_sender.send_file_webrtc(
181 file_path, 179 file_path, self.args.jid, self.args.name
182 self.args.jid,
183 self.args.name
184 ) 180 )
185 else: 181 else:
186 try: 182 try:
187 send_data_raw = await self.host.bridge.file_send( 183 send_data_raw = await self.host.bridge.file_send(
188 self.args.jid, 184 self.args.jid,
317 "receive", 313 "receive",
318 use_progress=True, 314 use_progress=True,
319 use_verbose=True, 315 use_verbose=True,
320 help=_("wait for a file to be sent by a contact"), 316 help=_("wait for a file to be sent by a contact"),
321 ) 317 )
322 self._overwrite_refused = False # True when one overwrite has already been refused 318 self._overwrite_refused = (
319 False # True when one overwrite has already been refused
320 )
323 self.action_callbacks = { 321 self.action_callbacks = {
324 C.META_TYPE_CONFIRM: self.on_confirm_action, 322 C.META_TYPE_CONFIRM: self.on_confirm_action,
325 C.META_TYPE_FILE: self.on_file_action, 323 C.META_TYPE_FILE: self.on_file_action,
326 C.META_TYPE_OVERWRITE: self.on_overwrite_action, 324 C.META_TYPE_OVERWRITE: self.on_overwrite_action,
327 C.META_TYPE_NOT_IN_ROSTER_LEAK: self.on_not_in_roster_action, 325 C.META_TYPE_NOT_IN_ROSTER_LEAK: self.on_not_in_roster_action,
375 async def _on_webrtc_close(self) -> None: 373 async def _on_webrtc_close(self) -> None:
376 if not self.args.multiple: 374 if not self.args.multiple:
377 await self.host.a_quit() 375 await self.host.a_quit()
378 376
379 async def on_webrtc_file( 377 async def on_webrtc_file(
380 self, 378 self, from_jid: jid.JID, session_id: str, file_data: dict
381 from_jid: jid.JID,
382 session_id: str,
383 file_data: dict
384 ) -> None: 379 ) -> None:
385 from libervia.frontends.tools.webrtc_file import WebRTCFileReceiver 380 from libervia.frontends.tools.webrtc_file import WebRTCFileReceiver
381
386 aio.install_glib_asyncio_iteration() 382 aio.install_glib_asyncio_iteration()
387 root_logger = logging.getLogger() 383 root_logger = logging.getLogger()
388 # we don't want any formatting for messages from webrtc 384 # we don't want any formatting for messages from webrtc
389 for handler in root_logger.handlers: 385 for handler in root_logger.handlers:
390 handler.setFormatter(None) 386 handler.setFormatter(None)
399 395
400 if dest_path.is_dir(): 396 if dest_path.is_dir():
401 filename = file_data.get("name", "unammed_file") 397 filename = file_data.get("name", "unammed_file")
402 dest_path /= filename 398 dest_path /= filename
403 if dest_path.exists() and not self.args.force: 399 if dest_path.exists() and not self.args.force:
404 self.host.disp( 400 self.host.disp("Destination file already exists", error=True)
405 "Destination file already exists",
406 error=True
407 )
408 aio.run_from_thread( 401 aio.run_from_thread(
409 self.host.a_quit, C.EXIT_ERROR, loop=self.host.loop.loop 402 self.host.a_quit, C.EXIT_ERROR, loop=self.host.loop.loop
410 ) 403 )
411 return 404 return
412 405
413 file_receiver = WebRTCFileReceiver( 406 file_receiver = WebRTCFileReceiver(
414 self.host.bridge, 407 self.host.bridge, self.profile, on_close_cb=self._on_webrtc_close
415 self.profile,
416 on_close_cb=self._on_webrtc_close
417 ) 408 )
418 409
419 await file_receiver.receive_file_webrtc( 410 await file_receiver.receive_file_webrtc(
420 from_jid, 411 from_jid, session_id, dest_path, file_data
421 session_id, 412 )
422 dest_path,
423 file_data
424 )
425
426 413
427 def get_xmlui_id(self, action_data): 414 def get_xmlui_id(self, action_data):
428 # FIXME: we temporarily use ElementTree, but a real XMLUI managing module 415 # FIXME: we temporarily use ElementTree, but a real XMLUI managing module
429 # should be available in the futur 416 # should be available in the futur
430 # TODO: XMLUI module 417 # TODO: XMLUI module
480 467
481 if pre_accepted or not self.bare_jids or from_jid.bare in self.bare_jids: 468 if pre_accepted or not self.bare_jids or from_jid.bare in self.bare_jids:
482 if self._overwrite_refused: 469 if self._overwrite_refused:
483 self.disp(_("File refused because overwrite is needed"), error=True) 470 self.disp(_("File refused because overwrite is needed"), error=True)
484 await self.host.bridge.action_launch( 471 await self.host.bridge.action_launch(
485 xmlui_id, data_format.serialise({"cancelled": C.BOOL_TRUE}), 472 xmlui_id,
486 profile_key=profile 473 data_format.serialise({"cancelled": C.BOOL_TRUE}),
474 profile_key=profile,
487 ) 475 )
488 return self.host.quit_from_signal(2) 476 return self.host.quit_from_signal(2)
489 await self.set_progress_id(progress_id) 477 await self.set_progress_id(progress_id)
490 if webrtc: 478 if webrtc:
491 xmlui_data = {"answer": C.BOOL_TRUE} 479 xmlui_data = {"answer": C.BOOL_TRUE}
493 try: 481 try:
494 session_id = action_data["session_id"] 482 session_id = action_data["session_id"]
495 except KeyError: 483 except KeyError:
496 self.disp(_("ignoring action without session id"), 1) 484 self.disp(_("ignoring action without session id"), 1)
497 return 485 return
498 await self.on_webrtc_file( 486 await self.on_webrtc_file(from_jid, session_id, file_data)
499 from_jid,
500 session_id,
501 file_data
502 )
503 487
504 else: 488 else:
505 xmlui_data = {"path": self.path} 489 xmlui_data = {"path": self.path}
506 await self.host.bridge.action_launch( 490 await self.host.bridge.action_launch(
507 xmlui_id, data_format.serialise(xmlui_data), profile_key=profile 491 xmlui_id, data_format.serialise(xmlui_data), profile_key=profile
602 "--force", 586 "--force",
603 action="store_true", 587 action="store_true",
604 help=_("overwrite existing file without confirmation"), 588 help=_("overwrite existing file without confirmation"),
605 ) 589 )
606 self.parser.add_argument( 590 self.parser.add_argument(
607 "attachment", type=str, 591 "attachment",
608 help=_("URI of the file to retrieve or JSON of the whole attachment") 592 type=str,
593 help=_("URI of the file to retrieve or JSON of the whole attachment"),
609 ) 594 )
610 595
611 async def on_progress_started(self, metadata): 596 async def on_progress_started(self, metadata):
612 self.disp(_("File download started"), 2) 597 self.disp(_("File download started"), 2)
613 598