comparison libervia/cli/cmd_bookmarks.py @ 4075:47401850dec6

refactoring: rename `libervia.frontends.jp` to `libervia.cli`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:54:26 +0200
parents libervia/frontends/jp/cmd_bookmarks.py@26b7ed2817da
children
comparison
equal deleted inserted replaced
4074:26b7ed2817da 4075:47401850dec6
1 #!/usr/bin/env python3
2
3
4 # Libervia CLI
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
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
20 from . import base
21 from libervia.backend.core.i18n import _
22 from libervia.cli.constants import Const as C
23
24 __commands__ = ["Bookmarks"]
25
26 STORAGE_LOCATIONS = ("local", "private", "pubsub")
27 TYPES = ("muc", "url")
28
29
30 class BookmarksCommon(base.CommandBase):
31 """Class used to group common options of bookmarks subcommands"""
32
33 def add_parser_options(self, location_default="all"):
34 self.parser.add_argument(
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
51
52 class BookmarksList(BookmarksCommon):
53 def __init__(self, host):
54 super(BookmarksList, self).__init__(host, "list", help=_("list bookmarks"))
55
56 async def start(self):
57 try:
58 data = await self.host.bridge.bookmarks_list(
59 self.args.type, self.args.location, self.host.profile
60 )
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
65 mess = []
66 for location in STORAGE_LOCATIONS:
67 if not data[location]:
68 continue
69 loc_mess = []
70 loc_mess.append(f"{location}:")
71 book_mess = []
72 for book_link, book_data in list(data[location].items()):
73 name = book_data.get("name")
74 autojoin = book_data.get("autojoin", "false") == "true"
75 nick = book_data.get("nick")
76 book_mess.append(
77 "\t%s[%s%s]%s"
78 % (
79 (name + " ") if name else "",
80 book_link,
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))
87
88 print("\n\n".join(mess))
89 self.host.quit()
90
91
92 class BookmarksRemove(BookmarksCommon):
93 def __init__(self, host):
94 super(BookmarksRemove, self).__init__(host, "remove", help=_("remove a bookmark"))
95
96 def add_parser_options(self):
97 super(BookmarksRemove, self).add_parser_options()
98 self.parser.add_argument(
99 "bookmark", help=_("jid (for muc bookmark) or url of to remove")
100 )
101 self.parser.add_argument(
102 "-f",
103 "--force",
104 action="store_true",
105 help=_("delete bookmark without confirmation"),
106 )
107
108 async def start(self):
109 if not self.args.force:
110 await self.host.confirm_or_quit(_("Are you sure to delete this bookmark?"))
111
112 try:
113 await self.host.bridge.bookmarks_remove(
114 self.args.type, self.args.bookmark, self.args.location, self.host.profile
115 )
116 except Exception as e:
117 self.disp(_("can't delete bookmark: {e}").format(e=e), error=True)
118 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
119 else:
120 self.disp(_("bookmark deleted"))
121 self.host.quit()
122
123
124 class BookmarksAdd(BookmarksCommon):
125 def __init__(self, host):
126 super(BookmarksAdd, self).__init__(host, "add", help=_("add a bookmark"))
127
128 def add_parser_options(self):
129 super(BookmarksAdd, self).add_parser_options(location_default="auto")
130 self.parser.add_argument(
131 "bookmark", help=_("jid (for muc bookmark) or url of to remove")
132 )
133 self.parser.add_argument("-n", "--name", help=_("bookmark name"))
134 muc_group = self.parser.add_argument_group(_("MUC specific options"))
135 muc_group.add_argument("-N", "--nick", help=_("nickname"))
136 muc_group.add_argument(
137 "-a",
138 "--autojoin",
139 action="store_true",
140 help=_("join room on profile connection"),
141 )
142
143 async def start(self):
144 if self.args.type == "url" and (self.args.autojoin or self.args.nick is not None):
145 self.parser.error(_("You can't use --autojoin or --nick with --type url"))
146 data = {}
147 if self.args.autojoin:
148 data["autojoin"] = "true"
149 if self.args.nick is not None:
150 data["nick"] = self.args.nick
151 if self.args.name is not None:
152 data["name"] = self.args.name
153 try:
154 await self.host.bridge.bookmarks_add(
155 self.args.type,
156 self.args.bookmark,
157 data,
158 self.args.location,
159 self.host.profile,
160 )
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:
165 self.disp(_("bookmark successfully added"))
166 self.host.quit()
167
168
169 class Bookmarks(base.CommandBase):
170 subcommands = (BookmarksList, BookmarksRemove, BookmarksAdd)
171
172 def __init__(self, host):
173 super(Bookmarks, self).__init__(
174 host, "bookmarks", use_profile=False, help=_("manage bookmarks")
175 )