Mercurial > libervia-backend
annotate sat_frontends/jp/cmd_bookmarks.py @ 4068:5241267a92b3
tests (units): tests for plugin XEP-0339:
fix 439
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 01 Jun 2023 21:37:31 +0200 |
parents | 524856bd7b19 |
children | 4b842c1fb686 |
rev | line source |
---|---|
3137 | 1 #!/usr/bin/env python3 |
2 | |
986 | 3 |
4 # jp: a SAT command line tool | |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
986 | 6 |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
3028 | 20 from . import base |
986 | 21 from sat.core.i18n import _ |
3040 | 22 from sat_frontends.jp.constants import Const as C |
986 | 23 |
24 __commands__ = ["Bookmarks"] | |
25 | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
26 STORAGE_LOCATIONS = ("local", "private", "pubsub") |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
27 TYPES = ("muc", "url") |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
28 |
986 | 29 |
30 class BookmarksCommon(base.CommandBase): | |
31 """Class used to group common options of bookmarks subcommands""" | |
32 | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
33 def add_parser_options(self, location_default="all"): |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
34 self.parser.add_argument( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
35 "-l", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
36 "--location", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
37 type=str, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
38 choices=(location_default,) + STORAGE_LOCATIONS, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
39 default=location_default, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
40 help=_("storage location (default: %(default)s)"), |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
41 ) |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
42 self.parser.add_argument( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
43 "-t", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
44 "--type", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
45 type=str, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
46 choices=TYPES, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
47 default=TYPES[0], |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
48 help=_("bookmarks type (default: %(default)s)"), |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
49 ) |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
50 |
986 | 51 |
52 class BookmarksList(BookmarksCommon): | |
53 def __init__(self, host): | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
54 super(BookmarksList, self).__init__(host, "list", help=_("list bookmarks")) |
986 | 55 |
3040 | 56 async def start(self): |
57 try: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
58 data = await self.host.bridge.bookmarks_list( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
59 self.args.type, self.args.location, self.host.profile |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
60 ) |
3040 | 61 except Exception as e: |
62 self.disp(f"can't get bookmarks list: {e}", error=True) | |
63 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
64 | |
986 | 65 mess = [] |
66 for location in STORAGE_LOCATIONS: | |
67 if not data[location]: | |
68 continue | |
69 loc_mess = [] | |
3040 | 70 loc_mess.append(f"{location}:") |
986 | 71 book_mess = [] |
3028 | 72 for book_link, book_data in list(data[location].items()): |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
73 name = book_data.get("name") |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
74 autojoin = book_data.get("autojoin", "false") == "true" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
75 nick = book_data.get("nick") |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
76 book_mess.append( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
77 "\t%s[%s%s]%s" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
78 % ( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
79 (name + " ") if name else "", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
80 book_link, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
81 " (%s)" % nick if nick else "", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
82 " (*)" if autojoin else "", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
83 ) |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
84 ) |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
85 loc_mess.append("\n".join(book_mess)) |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
86 mess.append("\n".join(loc_mess)) |
986 | 87 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
88 print("\n\n".join(mess)) |
3040 | 89 self.host.quit() |
986 | 90 |
91 | |
92 class BookmarksRemove(BookmarksCommon): | |
93 def __init__(self, host): | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
94 super(BookmarksRemove, self).__init__(host, "remove", help=_("remove a bookmark")) |
986 | 95 |
96 def add_parser_options(self): | |
97 super(BookmarksRemove, self).add_parser_options() | |
3040 | 98 self.parser.add_argument( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
99 "bookmark", help=_("jid (for muc bookmark) or url of to remove") |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
100 ) |
3040 | 101 self.parser.add_argument( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
102 "-f", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
103 "--force", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
104 action="store_true", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
105 help=_("delete bookmark without confirmation"), |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
106 ) |
3040 | 107 |
108 async def start(self): | |
109 if not self.args.force: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
110 await self.host.confirm_or_quit(_("Are you sure to delete this bookmark?")) |
986 | 111 |
3040 | 112 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
113 await self.host.bridge.bookmarks_remove( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
114 self.args.type, self.args.bookmark, self.args.location, self.host.profile |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
115 ) |
3040 | 116 except Exception as e: |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
117 self.disp(_("can't delete bookmark: {e}").format(e=e), error=True) |
3040 | 118 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
119 else: | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
120 self.disp(_("bookmark deleted")) |
3040 | 121 self.host.quit() |
986 | 122 |
123 | |
124 class BookmarksAdd(BookmarksCommon): | |
125 def __init__(self, host): | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
126 super(BookmarksAdd, self).__init__(host, "add", help=_("add a bookmark")) |
986 | 127 |
128 def add_parser_options(self): | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
129 super(BookmarksAdd, self).add_parser_options(location_default="auto") |
3040 | 130 self.parser.add_argument( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
131 "bookmark", help=_("jid (for muc bookmark) or url of to remove") |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
132 ) |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
133 self.parser.add_argument("-n", "--name", help=_("bookmark name")) |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
134 muc_group = self.parser.add_argument_group(_("MUC specific options")) |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
135 muc_group.add_argument("-N", "--nick", help=_("nickname")) |
3040 | 136 muc_group.add_argument( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
137 "-a", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
138 "--autojoin", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
139 action="store_true", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
140 help=_("join room on profile connection"), |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
141 ) |
986 | 142 |
3040 | 143 async def start(self): |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
144 if self.args.type == "url" and (self.args.autojoin or self.args.nick is not None): |
3040 | 145 self.parser.error(_("You can't use --autojoin or --nick with --type url")) |
986 | 146 data = {} |
147 if self.args.autojoin: | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
148 data["autojoin"] = "true" |
986 | 149 if self.args.nick is not None: |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
150 data["nick"] = self.args.nick |
986 | 151 if self.args.name is not None: |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
152 data["name"] = self.args.name |
3040 | 153 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
154 await self.host.bridge.bookmarks_add( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
155 self.args.type, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
156 self.args.bookmark, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
157 data, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
158 self.args.location, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
159 self.host.profile, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
160 ) |
3040 | 161 except Exception as e: |
162 self.disp(f"can't add bookmark: {e}", error=True) | |
163 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
164 else: | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
165 self.disp(_("bookmark successfully added")) |
3040 | 166 self.host.quit() |
986 | 167 |
168 | |
169 class Bookmarks(base.CommandBase): | |
170 subcommands = (BookmarksList, BookmarksRemove, BookmarksAdd) | |
171 | |
172 def __init__(self, host): | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
173 super(Bookmarks, self).__init__( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
174 host, "bookmarks", use_profile=False, help=_("manage bookmarks") |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
175 ) |