comparison frontends/src/jp/cmd_bookmarks.py @ 986:224cafc67324

jp: added bookmarks subcommands
author Goffi <goffi@goffi.org>
date Mon, 07 Apr 2014 16:24:29 +0200
parents
children 069ad98b360d
comparison
equal deleted inserted replaced
985:9ebdba4ab907 986:224cafc67324
1 #! /usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # jp: a SAT command line tool
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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 logging import debug, info, error, warning
21
22 import base
23 from sat.core.i18n import _
24
25 __commands__ = ["Bookmarks"]
26
27 STORAGE_LOCATIONS = ('local', 'private', 'pubsub')
28 TYPES = ('muc', 'url')
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('-l', '--location', type=str, choices=(location_default,) + STORAGE_LOCATIONS, default=location_default, help=_("storage location (default: %(default)s)"))
35 self.parser.add_argument('-t', '--type', type=str, choices=TYPES, default=TYPES[0], help=_("bookmarks type (default: %(default)s)"))
36
37 def _errback(self, failure):
38 print (("Something went wrong: [%s]") % failure)
39 self.host.quit(1)
40
41 class BookmarksList(BookmarksCommon):
42
43 def __init__(self, host):
44 super(BookmarksList, self).__init__(host, 'list', help=_('list bookmarks'))
45
46 def connected(self):
47 super(BookmarksList, self).connected()
48 data = self.host.bridge.bookmarksList(self.args.type, self.args.location, self.host.profile)
49 mess = []
50 for location in STORAGE_LOCATIONS:
51 if not data[location]:
52 continue
53 loc_mess = []
54 loc_mess.append(u"%s:" % location)
55 book_mess = []
56 for book_link, book_data in data[location].items():
57 name = book_data.get('name')
58 autojoin = book_data.get('autojoin', 'false') == 'true'
59 nick = book_data.get('nick')
60 book_mess.append(u"\t%s[%s%s]%s" % ((name+' ') if name else '',
61 book_link,
62 u' (%s)' % nick if nick else '',
63 u' (*)' if autojoin else ''))
64 loc_mess.append(u'\n'.join(book_mess))
65 mess.append(u'\n'.join(loc_mess))
66
67 print u'\n\n'.join(mess)
68
69
70 class BookmarksRemove(BookmarksCommon):
71 need_loop = True
72
73 def __init__(self, host):
74 super(BookmarksRemove, self).__init__(host, 'remove', help=_('remove a bookmark'))
75
76 def add_parser_options(self):
77 super(BookmarksRemove, self).add_parser_options()
78 self.parser.add_argument('bookmark', type=base.unicode_decoder, help=_('jid (for muc bookmark) or url of to remove'))
79
80 def connected(self):
81 super(BookmarksRemove, self).connected()
82 self.host.bridge.bookmarksRemove(self.args.type, self.args.bookmark, self.args.location, self.host.profile, callback = lambda: self.host.quit(), errback=self._errback)
83
84
85 class BookmarksAdd(BookmarksCommon):
86
87 def __init__(self, host):
88 super(BookmarksAdd, self).__init__(host, 'add', help=_('add a bookmark'))
89
90 def add_parser_options(self):
91 super(BookmarksAdd, self).add_parser_options(location_default='auto')
92 self.parser.add_argument('bookmark', type=base.unicode_decoder, help=_('jid (for muc bookmark) or url of to remove'))
93 self.parser.add_argument('-n', '--name', type=base.unicode_decoder, help=_("bookmark name"))
94 muc_group = self.parser.add_argument_group(_('MUC specific options'))
95 muc_group.add_argument('-N', '--nick', type=base.unicode_decoder, help=_('nickname'))
96 muc_group.add_argument('-a', '--autojoin', action='store_true', help=_('join room on profile connection'))
97
98 def connected(self):
99 self.need_loop = True
100 super(BookmarksAdd, self).connected()
101 if self.args.type == 'url' and (self.args.autojoin or self.args.nick is not None):
102 # XXX: Argparse doesn't seem to manage this case, any better way ?
103 print _(u"You can't use --autojoin or --nick with --type url")
104 self.host.quit(1)
105 data = {}
106 if self.args.autojoin:
107 data['autojoin'] = 'true'
108 if self.args.nick is not None:
109 data['nick'] = self.args.nick
110 if self.args.name is not None:
111 data['name'] = self.args.name
112 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)
113
114
115 class Bookmarks(base.CommandBase):
116 subcommands = (BookmarksList, BookmarksRemove, BookmarksAdd)
117
118 def __init__(self, host):
119 super(Bookmarks, self).__init__(host, 'bookmarks', use_profile=False, help=_('manage bookmarks'))