annotate sat_frontends/jp/cmd_bookmarks.py @ 2901:f6b0088ce247

code (xmpp): store version after roster item update + fix item removal: roster version is now stored after roster item has been added or removed. This way if something interrupting the process happens, former item update will be received and roster should be updated correctly. Roster item removal was crashing because `load()` is not used (local PersistentDict cache is then not updated, and the key to remove is missing there). The exception is now catched and ignored to avoid loading the data only to remove an item.
author Goffi <goffi@goffi.org>
date Wed, 10 Apr 2019 21:04:41 +0200
parents 003b8b4b56a7
children ab2696e34d29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1960
3e168cde7a7d jp: fixed shebang python call
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
1 #!/usr/bin/env python2
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
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
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
5 # Copyright (C) 2009-2019 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
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
20 import base
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.i18n import _
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
22
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
23 __commands__ = ["Bookmarks"]
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
24
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
25 STORAGE_LOCATIONS = ('local', 'private', 'pubsub')
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
26 TYPES = ('muc', 'url')
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
27
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
28 class BookmarksCommon(base.CommandBase):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
29 """Class used to group common options of bookmarks subcommands"""
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
30
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
31 def add_parser_options(self, location_default='all'):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
32 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
33 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
34
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
35 def _errback(self, failure):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
36 print (("Something went wrong: [%s]") % failure)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
37 self.host.quit(1)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
38
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
39 class BookmarksList(BookmarksCommon):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
40
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def __init__(self, host):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
42 super(BookmarksList, self).__init__(host, 'list', help=_('list bookmarks'))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
43
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
44 def start(self):
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
45 data = self.host.bridge.bookmarksList(self.args.type, self.args.location, self.host.profile)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
46 mess = []
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
47 for location in STORAGE_LOCATIONS:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
48 if not data[location]:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
49 continue
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
50 loc_mess = []
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
51 loc_mess.append(u"%s:" % location)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
52 book_mess = []
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
53 for book_link, book_data in data[location].items():
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
54 name = book_data.get('name')
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
55 autojoin = book_data.get('autojoin', 'false') == 'true'
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
56 nick = book_data.get('nick')
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
57 book_mess.append(u"\t%s[%s%s]%s" % ((name+' ') if name else '',
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
58 book_link,
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
59 u' (%s)' % nick if nick else '',
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
60 u' (*)' if autojoin else ''))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
61 loc_mess.append(u'\n'.join(book_mess))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
62 mess.append(u'\n'.join(loc_mess))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
63
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
64 print u'\n\n'.join(mess)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
65
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
66
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
67 class BookmarksRemove(BookmarksCommon):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
68
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
69 def __init__(self, host):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
70 super(BookmarksRemove, self).__init__(host, 'remove', help=_('remove a bookmark'))
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
71 self.need_loop = True
986
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 add_parser_options(self):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
74 super(BookmarksRemove, self).add_parser_options()
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self.parser.add_argument('bookmark', type=base.unicode_decoder, help=_('jid (for muc bookmark) or url of to remove'))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
76
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
77 def start(self):
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
78 self.host.bridge.bookmarksRemove(self.args.type, self.args.bookmark, self.args.location, self.host.profile, callback = lambda: self.host.quit(), errback=self._errback)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
79
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
80
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
81 class BookmarksAdd(BookmarksCommon):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
82
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
83 def __init__(self, host):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
84 super(BookmarksAdd, self).__init__(host, 'add', help=_('add a bookmark'))
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
85 self.need_loop = True
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
86
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
87 def add_parser_options(self):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
88 super(BookmarksAdd, self).add_parser_options(location_default='auto')
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.parser.add_argument('bookmark', type=base.unicode_decoder, help=_('jid (for muc bookmark) or url of to remove'))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.parser.add_argument('-n', '--name', type=base.unicode_decoder, help=_("bookmark name"))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
91 muc_group = self.parser.add_argument_group(_('MUC specific options'))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
92 muc_group.add_argument('-N', '--nick', type=base.unicode_decoder, help=_('nickname'))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
93 muc_group.add_argument('-a', '--autojoin', action='store_true', help=_('join room on profile connection'))
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
94
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
95 def start(self):
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
96 if self.args.type == 'url' and (self.args.autojoin or self.args.nick is not None):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
97 # XXX: Argparse doesn't seem to manage this case, any better way ?
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
98 print _(u"You can't use --autojoin or --nick with --type url")
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
99 self.host.quit(1)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
100 data = {}
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
101 if self.args.autojoin:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
102 data['autojoin'] = 'true'
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
103 if self.args.nick is not None:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
104 data['nick'] = self.args.nick
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
105 if self.args.name is not None:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
106 data['name'] = self.args.name
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
107 self.host.bridge.bookmarksAdd(self.args.type, self.args.bookmark, data, self.args.location, self.host.profile, callback = lambda: self.host.quit(), errback=self._errback)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
108
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
109
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
110 class Bookmarks(base.CommandBase):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
111 subcommands = (BookmarksList, BookmarksRemove, BookmarksAdd)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
112
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def __init__(self, host):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
114 super(Bookmarks, self).__init__(host, 'bookmarks', use_profile=False, help=_('manage bookmarks'))