Mercurial > libervia-backend
annotate sat_frontends/jp/cmd_bookmarks.py @ 2886:b06cb71079fa
core (xmpp): new networkEnabled() and networkDisabled() methods:
those methods can be called by platform specific plugins when network is known to be (un)available. This way, connection attempts can be cancelled when no network is available, saving resources (notably battery on mobile devices), or attempts can be restarted immediately when network is known to be available again.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 06 Apr 2019 19:05:57 +0200 |
parents | 003b8b4b56a7 |
children | ab2696e34d29 |
rev | line source |
---|---|
1960 | 1 #!/usr/bin/env python2 |
986 | 2 # -*- coding: utf-8 -*- |
3 | |
4 # jp: a SAT command line tool | |
2771 | 5 # Copyright (C) 2009-2019 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 | |
20 import base | |
21 from sat.core.i18n import _ | |
22 | |
23 __commands__ = ["Bookmarks"] | |
24 | |
25 STORAGE_LOCATIONS = ('local', 'private', 'pubsub') | |
26 TYPES = ('muc', 'url') | |
27 | |
28 class BookmarksCommon(base.CommandBase): | |
29 """Class used to group common options of bookmarks subcommands""" | |
30 | |
31 def add_parser_options(self, location_default='all'): | |
32 self.parser.add_argument('-l', '--location', type=str, choices=(location_default,) + STORAGE_LOCATIONS, default=location_default, help=_("storage location (default: %(default)s)")) | |
33 self.parser.add_argument('-t', '--type', type=str, choices=TYPES, default=TYPES[0], help=_("bookmarks type (default: %(default)s)")) | |
34 | |
35 def _errback(self, failure): | |
36 print (("Something went wrong: [%s]") % failure) | |
37 self.host.quit(1) | |
38 | |
39 class BookmarksList(BookmarksCommon): | |
40 | |
41 def __init__(self, host): | |
42 super(BookmarksList, self).__init__(host, 'list', help=_('list bookmarks')) | |
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 | 45 data = self.host.bridge.bookmarksList(self.args.type, self.args.location, self.host.profile) |
46 mess = [] | |
47 for location in STORAGE_LOCATIONS: | |
48 if not data[location]: | |
49 continue | |
50 loc_mess = [] | |
51 loc_mess.append(u"%s:" % location) | |
52 book_mess = [] | |
53 for book_link, book_data in data[location].items(): | |
54 name = book_data.get('name') | |
55 autojoin = book_data.get('autojoin', 'false') == 'true' | |
56 nick = book_data.get('nick') | |
57 book_mess.append(u"\t%s[%s%s]%s" % ((name+' ') if name else '', | |
58 book_link, | |
59 u' (%s)' % nick if nick else '', | |
60 u' (*)' if autojoin else '')) | |
61 loc_mess.append(u'\n'.join(book_mess)) | |
62 mess.append(u'\n'.join(loc_mess)) | |
63 | |
64 print u'\n\n'.join(mess) | |
65 | |
66 | |
67 class BookmarksRemove(BookmarksCommon): | |
68 | |
69 def __init__(self, host): | |
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 | 72 |
73 def add_parser_options(self): | |
74 super(BookmarksRemove, self).add_parser_options() | |
75 self.parser.add_argument('bookmark', type=base.unicode_decoder, help=_('jid (for muc bookmark) or url of to remove')) | |
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 | 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) |
79 | |
80 | |
81 class BookmarksAdd(BookmarksCommon): | |
82 | |
83 def __init__(self, host): | |
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 | 86 |
87 def add_parser_options(self): | |
88 super(BookmarksAdd, self).add_parser_options(location_default='auto') | |
89 self.parser.add_argument('bookmark', type=base.unicode_decoder, help=_('jid (for muc bookmark) or url of to remove')) | |
90 self.parser.add_argument('-n', '--name', type=base.unicode_decoder, help=_("bookmark name")) | |
91 muc_group = self.parser.add_argument_group(_('MUC specific options')) | |
92 muc_group.add_argument('-N', '--nick', type=base.unicode_decoder, help=_('nickname')) | |
93 muc_group.add_argument('-a', '--autojoin', action='store_true', help=_('join room on profile connection')) | |
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 | 96 if self.args.type == 'url' and (self.args.autojoin or self.args.nick is not None): |
97 # XXX: Argparse doesn't seem to manage this case, any better way ? | |
98 print _(u"You can't use --autojoin or --nick with --type url") | |
99 self.host.quit(1) | |
100 data = {} | |
101 if self.args.autojoin: | |
102 data['autojoin'] = 'true' | |
103 if self.args.nick is not None: | |
104 data['nick'] = self.args.nick | |
105 if self.args.name is not None: | |
106 data['name'] = self.args.name | |
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) | |
108 | |
109 | |
110 class Bookmarks(base.CommandBase): | |
111 subcommands = (BookmarksList, BookmarksRemove, BookmarksAdd) | |
112 | |
113 def __init__(self, host): | |
114 super(Bookmarks, self).__init__(host, 'bookmarks', use_profile=False, help=_('manage bookmarks')) |