comparison sat_frontends/jp/cmd_file.py @ 3568:04283582966f

core, frontends: fix invalid translatable strings. Some f-strings where used in translatable text, this has been fixed by using explicit `format()` call (using a script based on `tokenize`). As tokenize messes with spaces, a reformating tool (`black`) has been applied to some files afterwards.
author Goffi <goffi@goffi.org>
date Mon, 14 Jun 2021 18:35:12 +0200
parents 584379473925
children 1877c5c477ec
comparison
equal deleted inserted replaced
3567:a240748ed686 3568:04283582966f
52 52
53 def add_parser_options(self): 53 def add_parser_options(self):
54 self.parser.add_argument( 54 self.parser.add_argument(
55 "files", type=str, nargs="+", metavar="file", help=_("a list of file") 55 "files", type=str, nargs="+", metavar="file", help=_("a list of file")
56 ) 56 )
57 self.parser.add_argument( 57 self.parser.add_argument("jid", help=_("the destination jid"))
58 "jid", help=_("the destination jid")
59 )
60 self.parser.add_argument( 58 self.parser.add_argument(
61 "-b", "--bz2", action="store_true", help=_("make a bzip2 tarball") 59 "-b", "--bz2", action="store_true", help=_("make a bzip2 tarball")
62 ) 60 )
63 self.parser.add_argument( 61 self.parser.add_argument(
64 "-d", 62 "-d",
99 self.disp(_("File request sent to {jid}".format(jid=self.args.jid)), 1) 97 self.disp(_("File request sent to {jid}".format(jid=self.args.jid)), 1)
100 try: 98 try:
101 await self.set_progress_id(data["progress"]) 99 await self.set_progress_id(data["progress"])
102 except KeyError: 100 except KeyError:
103 # TODO: if 'xmlui' key is present, manage xmlui message display 101 # TODO: if 'xmlui' key is present, manage xmlui message display
104 self.disp( 102 self.disp(_("Can't send file to {jid}".format(jid=self.args.jid)), error=True)
105 _("Can't send file to {jid}".format(jid=self.args.jid)), error=True
106 )
107 self.host.quit(2) 103 self.host.quit(2)
108 104
109 async def start(self): 105 async def start(self):
110 for file_ in self.args.files: 106 for file_ in self.args.files:
111 if not os.path.exists(file_): 107 if not os.path.exists(file_):
112 self.disp(_(f"file {file_!r} doesn't exist!"), error=True) 108 self.disp(
109 _("file {file_} doesn't exist!").format(file_=repr(file_)), error=True
110 )
113 self.host.quit(C.EXIT_BAD_ARG) 111 self.host.quit(C.EXIT_BAD_ARG)
114 if not self.args.bz2 and os.path.isdir(file_): 112 if not self.args.bz2 and os.path.isdir(file_):
115 self.disp( 113 self.disp(
116 _(f"{file_!r} is a dir! Please send files inside or use compression") 114 _(
115 "{file_} is a dir! Please send files inside or use compression"
116 ).format(file_=repr(file_))
117 ) 117 )
118 self.host.quit(C.EXIT_BAD_ARG) 118 self.host.quit(C.EXIT_BAD_ARG)
119 119
120 extra = {} 120 extra = {}
121 if self.args.path: 121 if self.args.path:
186 @property 186 @property
187 def filename(self): 187 def filename(self):
188 return self.args.name or self.args.hash or "output" 188 return self.args.name or self.args.hash or "output"
189 189
190 def add_parser_options(self): 190 def add_parser_options(self):
191 self.parser.add_argument( 191 self.parser.add_argument("jid", help=_("the destination jid"))
192 "jid", help=_("the destination jid")
193 )
194 self.parser.add_argument( 192 self.parser.add_argument(
195 "-D", 193 "-D",
196 "--dest", 194 "--dest",
197 help=_( 195 help=_(
198 "destination path where the file will be saved (default: " 196 "destination path where the file will be saved (default: "
256 else: 254 else:
257 path = os.path.abspath(self.filename) 255 path = os.path.abspath(self.filename)
258 256
259 if os.path.exists(path) and not self.args.force: 257 if os.path.exists(path) and not self.args.force:
260 message = _("File {path} already exists! Do you want to overwrite?").format( 258 message = _("File {path} already exists! Do you want to overwrite?").format(
261 path = path) 259 path=path
260 )
262 await self.host.confirmOrQuit(message, _("file request cancelled")) 261 await self.host.confirmOrQuit(message, _("file request cancelled"))
263 262
264 self.full_dest_jid = await self.host.get_full_jid(self.args.jid) 263 self.full_dest_jid = await self.host.get_full_jid(self.args.jid)
265 extra = {} 264 extra = {}
266 if self.args.path: 265 if self.args.path:
276 self.args.hash_algo if self.args.hash else "", 275 self.args.hash_algo if self.args.hash else "",
277 extra, 276 extra,
278 self.profile, 277 self.profile,
279 ) 278 )
280 except Exception as e: 279 except Exception as e:
281 self.disp(msg=_(f"can't request file: {e}"), error=True) 280 self.disp(msg=_("can't request file: {e}").format(e=e), error=True)
282 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 281 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
283 else: 282 else:
284 await self.set_progress_id(progress_id) 283 await self.set_progress_id(progress_id)
285 284
286 285
332 331
333 async def onProgressFinished(self, metadata): 332 async def onProgressFinished(self, metadata):
334 self.disp(_("File received successfully"), 2) 333 self.disp(_("File received successfully"), 2)
335 if metadata.get("hash_verified", False): 334 if metadata.get("hash_verified", False):
336 try: 335 try:
337 self.disp(_( 336 self.disp(
338 f"hash checked: {metadata['hash_algo']}:{metadata['hash']}"), 1) 337 _("hash checked: {metadata['hash_algo']}:{metadata['hash']}"), 1
338 )
339 except KeyError: 339 except KeyError:
340 self.disp(_("hash is checked but hash value is missing", 1), error=True) 340 self.disp(_("hash is checked but hash value is missing", 1), error=True)
341 else: 341 else:
342 self.disp(_("hash can't be verified"), 1) 342 self.disp(_("hash can't be verified"), 1)
343 343
344 async def onProgressError(self, e): 344 async def onProgressError(self, e):
345 self.disp(_(f"Error while receiving file: {e}"), error=True) 345 self.disp(_("Error while receiving file: {e}").format(e=e), error=True)
346 346
347 def getXmluiId(self, action_data): 347 def getXmluiId(self, action_data):
348 # FIXME: we temporarily use ElementTree, but a real XMLUI managing module 348 # FIXME: we temporarily use ElementTree, but a real XMLUI managing module
349 # should be available in the futur 349 # should be available in the futur
350 # TODO: XMLUI module 350 # TODO: XMLUI module
411 if xmlui_id is None: 411 if xmlui_id is None:
412 return self.host.quitFromSignal(1) 412 return self.host.quitFromSignal(1)
413 try: 413 try:
414 from_jid = jid.JID(action_data["meta_from_jid"]) 414 from_jid = jid.JID(action_data["meta_from_jid"])
415 except ValueError: 415 except ValueError:
416 self.disp(_('invalid "from_jid" value received, ignoring: {value}').format( 416 self.disp(
417 value=from_jid), error=True) 417 _('invalid "from_jid" value received, ignoring: {value}').format(
418 value=from_jid
419 ),
420 error=True,
421 )
418 return 422 return
419 except KeyError: 423 except KeyError:
420 self.disp(_('ignoring action without "from_jid" value'), error=True) 424 self.disp(_('ignoring action without "from_jid" value'), error=True)
421 return 425 return
422 self.disp(_("Confirmation needed for request from an entity not in roster"), 1) 426 self.disp(_("Confirmation needed for request from an entity not in roster"), 1)
446 self.disp(_("waiting for incoming file request"), 2) 450 self.disp(_("waiting for incoming file request"), 2)
447 await self.start_answering() 451 await self.start_answering()
448 452
449 453
450 class Get(base.CommandBase): 454 class Get(base.CommandBase):
451
452 def __init__(self, host): 455 def __init__(self, host):
453 super(Get, self).__init__( 456 super(Get, self).__init__(
454 host, "get", use_progress=True, use_verbose=True, 457 host,
455 help=_("download a file from URI") 458 "get",
456 ) 459 use_progress=True,
457 460 use_verbose=True,
458 def add_parser_options(self): 461 help=_("download a file from URI"),
459 self.parser.add_argument( 462 )
460 '-o', '--dest-file', type=str, default='', 463
461 help=_("destination file (DEFAULT: filename from URL)") 464 def add_parser_options(self):
462 ) 465 self.parser.add_argument(
466 "-o",
467 "--dest-file",
468 type=str,
469 default="",
470 help=_("destination file (DEFAULT: filename from URL)"),
471 )
463 self.parser.add_argument( 472 self.parser.add_argument(
464 "-f", 473 "-f",
465 "--force", 474 "--force",
466 action="store_true", 475 action="store_true",
467 help=_("overwrite existing file without confirmation"), 476 help=_("overwrite existing file without confirmation"),
480 async def gotId(self, data): 489 async def gotId(self, data):
481 """Called when a progress id has been received""" 490 """Called when a progress id has been received"""
482 try: 491 try:
483 await self.set_progress_id(data["progress"]) 492 await self.set_progress_id(data["progress"])
484 except KeyError: 493 except KeyError:
485 if 'xmlui' in data: 494 if "xmlui" in data:
486 ui = xmlui_manager.create(self.host, data['xmlui']) 495 ui = xmlui_manager.create(self.host, data["xmlui"])
487 await ui.show() 496 await ui.show()
488 else: 497 else:
489 self.disp(_("Can't download file"), error=True) 498 self.disp(_("Can't download file"), error=True)
490 self.host.quit(C.EXIT_ERROR) 499 self.host.quit(C.EXIT_ERROR)
491 500
497 dest_file = Path(parsed_uri.path).name.strip() or "downloaded_file" 506 dest_file = Path(parsed_uri.path).name.strip() or "downloaded_file"
498 507
499 dest_file = Path(dest_file).expanduser().resolve() 508 dest_file = Path(dest_file).expanduser().resolve()
500 if dest_file.exists() and not self.args.force: 509 if dest_file.exists() and not self.args.force:
501 message = _("File {path} already exists! Do you want to overwrite?").format( 510 message = _("File {path} already exists! Do you want to overwrite?").format(
502 path = dest_file) 511 path=dest_file
512 )
503 await self.host.confirmOrQuit(message, _("file download cancelled")) 513 await self.host.confirmOrQuit(message, _("file download cancelled"))
504 514
505 options = {} 515 options = {}
506 516
507 try: 517 try:
567 @param file_(str): file path 577 @param file_(str): file path
568 """ 578 """
569 try: 579 try:
570 await self.set_progress_id(data["progress"]) 580 await self.set_progress_id(data["progress"])
571 except KeyError: 581 except KeyError:
572 if 'xmlui' in data: 582 if "xmlui" in data:
573 ui = xmlui_manager.create(self.host, data['xmlui']) 583 ui = xmlui_manager.create(self.host, data["xmlui"])
574 await ui.show() 584 await ui.show()
575 else: 585 else:
576 self.disp(_("Can't upload file"), error=True) 586 self.disp(_("Can't upload file"), error=True)
577 self.host.quit(C.EXIT_ERROR) 587 self.host.quit(C.EXIT_ERROR)
578 588
579 async def start(self): 589 async def start(self):
580 file_ = self.args.file 590 file_ = self.args.file
581 if not os.path.exists(file_): 591 if not os.path.exists(file_):
582 self.disp(_(f"file {file_!r} doesn't exist !"), error=True) 592 self.disp(
593 _("file {file_} doesn't exist !").format(file_=repr(file_)), error=True
594 )
583 self.host.quit(C.EXIT_BAD_ARG) 595 self.host.quit(C.EXIT_BAD_ARG)
584 if os.path.isdir(file_): 596 if os.path.isdir(file_):
585 self.disp(_(f"{file_!r} is a dir! Can't upload a dir")) 597 self.disp(_("{file_} is a dir! Can't upload a dir").format(file_=repr(file_)))
586 self.host.quit(C.EXIT_BAD_ARG) 598 self.host.quit(C.EXIT_BAD_ARG)
587 599
588 if self.args.jid is None: 600 if self.args.jid is None:
589 self.full_dest_jid = "" 601 self.full_dest_jid = ""
590 else: 602 else:
611 else: 623 else:
612 await self.gotId(upload_data, file_) 624 await self.gotId(upload_data, file_)
613 625
614 626
615 class ShareAffiliationsSet(base.CommandBase): 627 class ShareAffiliationsSet(base.CommandBase):
616
617 def __init__(self, host): 628 def __init__(self, host):
618 super(ShareAffiliationsSet, self).__init__( 629 super(ShareAffiliationsSet, self).__init__(
619 host, 630 host,
620 "set", 631 "set",
621 use_output=C.OUTPUT_DICT, 632 use_output=C.OUTPUT_DICT,
718 host, "affiliations", use_profile=False, help=_("affiliations management") 729 host, "affiliations", use_profile=False, help=_("affiliations management")
719 ) 730 )
720 731
721 732
722 class ShareConfigurationSet(base.CommandBase): 733 class ShareConfigurationSet(base.CommandBase):
723
724 def __init__(self, host): 734 def __init__(self, host):
725 super(ShareConfigurationSet, self).__init__( 735 super(ShareConfigurationSet, self).__init__(
726 host, 736 host,
727 "set", 737 "set",
728 use_output=C.OUTPUT_DICT, 738 use_output=C.OUTPUT_DICT,
820 class ShareConfiguration(base.CommandBase): 830 class ShareConfiguration(base.CommandBase):
821 subcommands = (ShareConfigurationGet, ShareConfigurationSet) 831 subcommands = (ShareConfigurationGet, ShareConfigurationSet)
822 832
823 def __init__(self, host): 833 def __init__(self, host):
824 super(ShareConfiguration, self).__init__( 834 super(ShareConfiguration, self).__init__(
825 host, "configuration", use_profile=False, 835 host,
826 help=_("file sharing node configuration") 836 "configuration",
837 use_profile=False,
838 help=_("file sharing node configuration"),
827 ) 839 )
828 840
829 841
830 class ShareList(base.CommandBase): 842 class ShareList(base.CommandBase):
831 def __init__(self, host): 843 def __init__(self, host):
960 ) 972 )
961 except Exception as e: 973 except Exception as e:
962 self.disp(f"can't share path: {e}", error=True) 974 self.disp(f"can't share path: {e}", error=True)
963 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 975 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
964 else: 976 else:
965 self.disp(_(f'{self.path} shared under the name "{name}"')) 977 self.disp(
978 _('{path} shared under the name "{name}"').format(
979 path=self.path, name=name
980 )
981 )
966 self.host.quit() 982 self.host.quit()
967 983
968 984
969 class ShareInvite(base.CommandBase): 985 class ShareInvite(base.CommandBase):
970 def __init__(self, host): 986 def __init__(self, host):
1013 1029
1014 async def start(self): 1030 async def start(self):
1015 self.path = os.path.normpath(self.args.path) if self.args.path else "" 1031 self.path = os.path.normpath(self.args.path) if self.args.path else ""
1016 extra = {} 1032 extra = {}
1017 if self.args.thumbnail is not None: 1033 if self.args.thumbnail is not None:
1018 if not self.args.thumbnail.startswith('http'): 1034 if not self.args.thumbnail.startswith("http"):
1019 self.parser.error(_("only http(s) links are allowed with --thumbnail")) 1035 self.parser.error(_("only http(s) links are allowed with --thumbnail"))
1020 else: 1036 else:
1021 extra['thumb_url'] = self.args.thumbnail 1037 extra["thumb_url"] = self.args.thumbnail
1022 try: 1038 try:
1023 await self.host.bridge.FISInvite( 1039 await self.host.bridge.FISInvite(
1024 self.args.jid, 1040 self.args.jid,
1025 self.args.service, 1041 self.args.service,
1026 self.args.type, 1042 self.args.type,
1032 ) 1048 )
1033 except Exception as e: 1049 except Exception as e:
1034 self.disp(f"can't send invitation: {e}", error=True) 1050 self.disp(f"can't send invitation: {e}", error=True)
1035 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 1051 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
1036 else: 1052 else:
1037 self.disp( 1053 self.disp(_("invitation sent to {jid}").format(jid=self.args.jid))
1038 _(f'invitation sent to {self.args.jid}')
1039 )
1040 self.host.quit() 1054 self.host.quit()
1041 1055
1042 1056
1043 class Share(base.CommandBase): 1057 class Share(base.CommandBase):
1044 subcommands = ( 1058 subcommands = (
1045 ShareList, SharePath, ShareInvite, ShareAffiliations, ShareConfiguration) 1059 ShareList,
1060 SharePath,
1061 ShareInvite,
1062 ShareAffiliations,
1063 ShareConfiguration,
1064 )
1046 1065
1047 def __init__(self, host): 1066 def __init__(self, host):
1048 super(Share, self).__init__( 1067 super(Share, self).__init__(
1049 host, "share", use_profile=False, help=_("files sharing management") 1068 host, "share", use_profile=False, help=_("files sharing management")
1050 ) 1069 )