annotate sat_frontends/jp/cmd_bookmarks.py @ 3433:63d20854df3a

core (constants): added EXIT_CONFLICT
author Goffi <goffi@goffi.org>
date Fri, 04 Dec 2020 12:30:46 +0100
parents 559a625a236b
children be6d91572633
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #!/usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
3
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # jp: a SAT command line tool
3136
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
5 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
6
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
11
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
16
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
19
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
20 from . import base
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.i18n import _
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
22 from sat_frontends.jp.constants import Const as C
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
23
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
24 __commands__ = ["Bookmarks"]
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
25
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
26 STORAGE_LOCATIONS = ('local', 'private', 'pubsub')
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
27 TYPES = ('muc', 'url')
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
28
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
29 class BookmarksCommon(base.CommandBase):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
30 """Class used to group common options of bookmarks subcommands"""
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
31
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
32 def add_parser_options(self, location_default='all'):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
33 self.parser.add_argument('-l', '--location', type=str, choices=(location_default,) + STORAGE_LOCATIONS, default=location_default, help=_("storage location (default: %(default)s)"))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
34 self.parser.add_argument('-t', '--type', type=str, choices=TYPES, default=TYPES[0], help=_("bookmarks type (default: %(default)s)"))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
35
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
36 class BookmarksList(BookmarksCommon):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
37
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
38 def __init__(self, host):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
39 super(BookmarksList, self).__init__(host, 'list', help=_('list bookmarks'))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
40
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
41 async def start(self):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
42 try:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
43 data = await self.host.bridge.bookmarksList(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
44 self.args.type, self.args.location, self.host.profile)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
45 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
46 self.disp(f"can't get bookmarks list: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
47 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
48
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
49 mess = []
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
50 for location in STORAGE_LOCATIONS:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
51 if not data[location]:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
52 continue
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
53 loc_mess = []
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
54 loc_mess.append(f"{location}:")
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
55 book_mess = []
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
56 for book_link, book_data in list(data[location].items()):
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
57 name = book_data.get('name')
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
58 autojoin = book_data.get('autojoin', 'false') == 'true'
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
59 nick = book_data.get('nick')
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
60 book_mess.append("\t%s[%s%s]%s" % ((name+' ') if name else '',
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
61 book_link,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
62 ' (%s)' % nick if nick else '',
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
63 ' (*)' if autojoin else ''))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
64 loc_mess.append('\n'.join(book_mess))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
65 mess.append('\n'.join(loc_mess))
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
66
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
67 print('\n\n'.join(mess))
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
68 self.host.quit()
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
69
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
70
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
71 class BookmarksRemove(BookmarksCommon):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
72
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def __init__(self, host):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
74 super(BookmarksRemove, self).__init__(host, 'remove', help=_('remove a bookmark'))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
75
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
76 def add_parser_options(self):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
77 super(BookmarksRemove, self).add_parser_options()
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
78 self.parser.add_argument(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
79 'bookmark', help=_('jid (for muc bookmark) or url of to remove'))
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
80 self.parser.add_argument(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
81 "-f", "--force", action="store_true",
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
82 help=_("delete bookmark without confirmation"),)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
83
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
84 async def start(self):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
85 if not self.args.force:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
86 await self.host.confirmOrQuit(_("Are you sure to delete this bookmark?"))
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
87
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
88 try:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
89 await self.host.bridge.bookmarksRemove(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
90 self.args.type, self.args.bookmark, self.args.location, self.host.profile)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
91 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
92 self.disp(_(f"can't delete bookmark: {e}"), error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
93 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
94 else:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
95 self.disp(_('bookmark deleted'))
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
96 self.host.quit()
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
97
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
98
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
99 class BookmarksAdd(BookmarksCommon):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
100
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
101 def __init__(self, host):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
102 super(BookmarksAdd, self).__init__(host, 'add', help=_('add a bookmark'))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
103
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
104 def add_parser_options(self):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
105 super(BookmarksAdd, self).add_parser_options(location_default='auto')
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
106 self.parser.add_argument(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
107 'bookmark', help=_('jid (for muc bookmark) or url of to remove'))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
108 self.parser.add_argument('-n', '--name', help=_("bookmark name"))
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
109 muc_group = self.parser.add_argument_group(_('MUC specific options'))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
110 muc_group.add_argument('-N', '--nick', help=_('nickname'))
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
111 muc_group.add_argument(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
112 '-a', '--autojoin', action='store_true',
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
113 help=_('join room on profile connection'))
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
114
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
115 async def start(self):
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
116 if self.args.type == 'url' and (self.args.autojoin or self.args.nick is not None):
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
117 self.parser.error(_("You can't use --autojoin or --nick with --type url"))
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
118 data = {}
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
119 if self.args.autojoin:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
120 data['autojoin'] = 'true'
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
121 if self.args.nick is not None:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
122 data['nick'] = self.args.nick
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
123 if self.args.name is not None:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
124 data['name'] = self.args.name
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
125 try:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
126 await self.host.bridge.bookmarksAdd(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
127 self.args.type, self.args.bookmark, data, self.args.location,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
128 self.host.profile)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
129 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
130 self.disp(f"can't add bookmark: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
131 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
132 else:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
133 self.disp(_('bookmark successfully added'))
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
134 self.host.quit()
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
135
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
136
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
137 class Bookmarks(base.CommandBase):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
138 subcommands = (BookmarksList, BookmarksRemove, BookmarksAdd)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
139
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
140 def __init__(self, host):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
141 super(Bookmarks, self).__init__(host, 'bookmarks', use_profile=False, help=_('manage bookmarks'))