annotate sat_frontends/jp/cmd_bookmarks.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 003b8b4b56a7
children fee60f17ebac
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
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 _
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):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
36 print((("Something went wrong: [%s]") % failure))
986
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 = []
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
51 loc_mess.append("%s:" % location)
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
52 book_mess = []
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
53 for book_link, book_data in list(data[location].items()):
986
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')
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
57 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
58 book_link,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
59 ' (%s)' % nick if nick else '',
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
60 ' (*)' if autojoin else ''))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
61 loc_mess.append('\n'.join(book_mess))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
62 mess.append('\n'.join(loc_mess))
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
63
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
64 print('\n\n'.join(mess))
986
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()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
75 self.parser.add_argument('bookmark', help=_('jid (for muc bookmark) or url of to remove'))
986
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')
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
89 self.parser.add_argument('bookmark', help=_('jid (for muc bookmark) or url of to remove'))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
90 self.parser.add_argument('-n', '--name', help=_("bookmark name"))
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents:
diff changeset
91 muc_group = self.parser.add_argument_group(_('MUC specific options'))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
92 muc_group.add_argument('-N', '--nick', help=_('nickname'))
986
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 ?
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
98 print(_("You can't use --autojoin or --nick with --type url"))
986
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'))