comparison sat_frontends/jp/cmd_bookmarks.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 be6d91572633
children 524856bd7b19
comparison
equal deleted inserted replaced
3567:a240748ed686 3568:04283582966f
21 from sat.core.i18n import _ 21 from sat.core.i18n import _
22 from sat_frontends.jp.constants import Const as C 22 from sat_frontends.jp.constants import Const as C
23 23
24 __commands__ = ["Bookmarks"] 24 __commands__ = ["Bookmarks"]
25 25
26 STORAGE_LOCATIONS = ('local', 'private', 'pubsub') 26 STORAGE_LOCATIONS = ("local", "private", "pubsub")
27 TYPES = ('muc', 'url') 27 TYPES = ("muc", "url")
28
28 29
29 class BookmarksCommon(base.CommandBase): 30 class BookmarksCommon(base.CommandBase):
30 """Class used to group common options of bookmarks subcommands""" 31 """Class used to group common options of bookmarks subcommands"""
31 32
32 def add_parser_options(self, location_default='all'): 33 def add_parser_options(self, location_default="all"):
33 self.parser.add_argument('-l', '--location', type=str, choices=(location_default,) + STORAGE_LOCATIONS, default=location_default, help=_("storage location (default: %(default)s)")) 34 self.parser.add_argument(
34 self.parser.add_argument('-t', '--type', type=str, choices=TYPES, default=TYPES[0], help=_("bookmarks type (default: %(default)s)")) 35 "-l",
36 "--location",
37 type=str,
38 choices=(location_default,) + STORAGE_LOCATIONS,
39 default=location_default,
40 help=_("storage location (default: %(default)s)"),
41 )
42 self.parser.add_argument(
43 "-t",
44 "--type",
45 type=str,
46 choices=TYPES,
47 default=TYPES[0],
48 help=_("bookmarks type (default: %(default)s)"),
49 )
50
35 51
36 class BookmarksList(BookmarksCommon): 52 class BookmarksList(BookmarksCommon):
37
38 def __init__(self, host): 53 def __init__(self, host):
39 super(BookmarksList, self).__init__(host, 'list', help=_('list bookmarks')) 54 super(BookmarksList, self).__init__(host, "list", help=_("list bookmarks"))
40 55
41 async def start(self): 56 async def start(self):
42 try: 57 try:
43 data = await self.host.bridge.bookmarksList( 58 data = await self.host.bridge.bookmarksList(
44 self.args.type, self.args.location, self.host.profile) 59 self.args.type, self.args.location, self.host.profile
60 )
45 except Exception as e: 61 except Exception as e:
46 self.disp(f"can't get bookmarks list: {e}", error=True) 62 self.disp(f"can't get bookmarks list: {e}", error=True)
47 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 63 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
48 64
49 mess = [] 65 mess = []
52 continue 68 continue
53 loc_mess = [] 69 loc_mess = []
54 loc_mess.append(f"{location}:") 70 loc_mess.append(f"{location}:")
55 book_mess = [] 71 book_mess = []
56 for book_link, book_data in list(data[location].items()): 72 for book_link, book_data in list(data[location].items()):
57 name = book_data.get('name') 73 name = book_data.get("name")
58 autojoin = book_data.get('autojoin', 'false') == 'true' 74 autojoin = book_data.get("autojoin", "false") == "true"
59 nick = book_data.get('nick') 75 nick = book_data.get("nick")
60 book_mess.append("\t%s[%s%s]%s" % ((name+' ') if name else '', 76 book_mess.append(
61 book_link, 77 "\t%s[%s%s]%s"
62 ' (%s)' % nick if nick else '', 78 % (
63 ' (*)' if autojoin else '')) 79 (name + " ") if name else "",
64 loc_mess.append('\n'.join(book_mess)) 80 book_link,
65 mess.append('\n'.join(loc_mess)) 81 " (%s)" % nick if nick else "",
82 " (*)" if autojoin else "",
83 )
84 )
85 loc_mess.append("\n".join(book_mess))
86 mess.append("\n".join(loc_mess))
66 87
67 print('\n\n'.join(mess)) 88 print("\n\n".join(mess))
68 self.host.quit() 89 self.host.quit()
69 90
70 91
71 class BookmarksRemove(BookmarksCommon): 92 class BookmarksRemove(BookmarksCommon):
72
73 def __init__(self, host): 93 def __init__(self, host):
74 super(BookmarksRemove, self).__init__(host, 'remove', help=_('remove a bookmark')) 94 super(BookmarksRemove, self).__init__(host, "remove", help=_("remove a bookmark"))
75 95
76 def add_parser_options(self): 96 def add_parser_options(self):
77 super(BookmarksRemove, self).add_parser_options() 97 super(BookmarksRemove, self).add_parser_options()
78 self.parser.add_argument( 98 self.parser.add_argument(
79 'bookmark', help=_('jid (for muc bookmark) or url of to remove')) 99 "bookmark", help=_("jid (for muc bookmark) or url of to remove")
100 )
80 self.parser.add_argument( 101 self.parser.add_argument(
81 "-f", "--force", action="store_true", 102 "-f",
82 help=_("delete bookmark without confirmation"),) 103 "--force",
104 action="store_true",
105 help=_("delete bookmark without confirmation"),
106 )
83 107
84 async def start(self): 108 async def start(self):
85 if not self.args.force: 109 if not self.args.force:
86 await self.host.confirmOrQuit(_("Are you sure to delete this bookmark?")) 110 await self.host.confirmOrQuit(_("Are you sure to delete this bookmark?"))
87 111
88 try: 112 try:
89 await self.host.bridge.bookmarksRemove( 113 await self.host.bridge.bookmarksRemove(
90 self.args.type, self.args.bookmark, self.args.location, self.host.profile) 114 self.args.type, self.args.bookmark, self.args.location, self.host.profile
115 )
91 except Exception as e: 116 except Exception as e:
92 self.disp(_(f"can't delete bookmark: {e}"), error=True) 117 self.disp(_("can't delete bookmark: {e}").format(e=e), error=True)
93 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 118 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
94 else: 119 else:
95 self.disp(_('bookmark deleted')) 120 self.disp(_("bookmark deleted"))
96 self.host.quit() 121 self.host.quit()
97 122
98 123
99 class BookmarksAdd(BookmarksCommon): 124 class BookmarksAdd(BookmarksCommon):
100
101 def __init__(self, host): 125 def __init__(self, host):
102 super(BookmarksAdd, self).__init__(host, 'add', help=_('add a bookmark')) 126 super(BookmarksAdd, self).__init__(host, "add", help=_("add a bookmark"))
103 127
104 def add_parser_options(self): 128 def add_parser_options(self):
105 super(BookmarksAdd, self).add_parser_options(location_default='auto') 129 super(BookmarksAdd, self).add_parser_options(location_default="auto")
106 self.parser.add_argument( 130 self.parser.add_argument(
107 'bookmark', help=_('jid (for muc bookmark) or url of to remove')) 131 "bookmark", help=_("jid (for muc bookmark) or url of to remove")
108 self.parser.add_argument('-n', '--name', help=_("bookmark name")) 132 )
109 muc_group = self.parser.add_argument_group(_('MUC specific options')) 133 self.parser.add_argument("-n", "--name", help=_("bookmark name"))
110 muc_group.add_argument('-N', '--nick', help=_('nickname')) 134 muc_group = self.parser.add_argument_group(_("MUC specific options"))
135 muc_group.add_argument("-N", "--nick", help=_("nickname"))
111 muc_group.add_argument( 136 muc_group.add_argument(
112 '-a', '--autojoin', action='store_true', 137 "-a",
113 help=_('join room on profile connection')) 138 "--autojoin",
139 action="store_true",
140 help=_("join room on profile connection"),
141 )
114 142
115 async def start(self): 143 async def start(self):
116 if self.args.type == 'url' and (self.args.autojoin or self.args.nick is not None): 144 if self.args.type == "url" and (self.args.autojoin or self.args.nick is not None):
117 self.parser.error(_("You can't use --autojoin or --nick with --type url")) 145 self.parser.error(_("You can't use --autojoin or --nick with --type url"))
118 data = {} 146 data = {}
119 if self.args.autojoin: 147 if self.args.autojoin:
120 data['autojoin'] = 'true' 148 data["autojoin"] = "true"
121 if self.args.nick is not None: 149 if self.args.nick is not None:
122 data['nick'] = self.args.nick 150 data["nick"] = self.args.nick
123 if self.args.name is not None: 151 if self.args.name is not None:
124 data['name'] = self.args.name 152 data["name"] = self.args.name
125 try: 153 try:
126 await self.host.bridge.bookmarksAdd( 154 await self.host.bridge.bookmarksAdd(
127 self.args.type, self.args.bookmark, data, self.args.location, 155 self.args.type,
128 self.host.profile) 156 self.args.bookmark,
157 data,
158 self.args.location,
159 self.host.profile,
160 )
129 except Exception as e: 161 except Exception as e:
130 self.disp(f"can't add bookmark: {e}", error=True) 162 self.disp(f"can't add bookmark: {e}", error=True)
131 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 163 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
132 else: 164 else:
133 self.disp(_('bookmark successfully added')) 165 self.disp(_("bookmark successfully added"))
134 self.host.quit() 166 self.host.quit()
135 167
136 168
137 class Bookmarks(base.CommandBase): 169 class Bookmarks(base.CommandBase):
138 subcommands = (BookmarksList, BookmarksRemove, BookmarksAdd) 170 subcommands = (BookmarksList, BookmarksRemove, BookmarksAdd)
139 171
140 def __init__(self, host): 172 def __init__(self, host):
141 super(Bookmarks, self).__init__(host, 'bookmarks', use_profile=False, help=_('manage bookmarks')) 173 super(Bookmarks, self).__init__(
174 host, "bookmarks", use_profile=False, help=_("manage bookmarks")
175 )