Mercurial > libervia-backend
comparison sat_frontends/jp/cmd_bookmarks.py @ 3040:fee60f17ebac
jp: jp asyncio port:
/!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\
This patch implements the port of jp to asyncio, so it is now correctly using the bridge
asynchronously, and it can be used with bridges like `pb`. This also simplify the code,
notably for things which were previously implemented with many callbacks (like pagination
with RSM).
During the process, some behaviours have been modified/fixed, in jp and backends, check
diff for details.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 25 Sep 2019 08:56:41 +0200 |
parents | ab2696e34d29 |
children | 9d0df638c8b4 |
comparison
equal
deleted
inserted
replaced
3039:a1bc34f90fa5 | 3040:fee60f17ebac |
---|---|
17 # You should have received a copy of the GNU Affero General Public License | 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/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from . import base | 20 from . import base |
21 from sat.core.i18n import _ | 21 from sat.core.i18n import _ |
22 from sat_frontends.jp.constants import Const as C | |
22 | 23 |
23 __commands__ = ["Bookmarks"] | 24 __commands__ = ["Bookmarks"] |
24 | 25 |
25 STORAGE_LOCATIONS = ('local', 'private', 'pubsub') | 26 STORAGE_LOCATIONS = ('local', 'private', 'pubsub') |
26 TYPES = ('muc', 'url') | 27 TYPES = ('muc', 'url') |
30 | 31 |
31 def add_parser_options(self, location_default='all'): | 32 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('-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 self.parser.add_argument('-t', '--type', type=str, choices=TYPES, default=TYPES[0], help=_("bookmarks type (default: %(default)s)")) |
34 | 35 |
35 def _errback(self, failure): | |
36 print((("Something went wrong: [%s]") % failure)) | |
37 self.host.quit(1) | |
38 | |
39 class BookmarksList(BookmarksCommon): | 36 class BookmarksList(BookmarksCommon): |
40 | 37 |
41 def __init__(self, host): | 38 def __init__(self, host): |
42 super(BookmarksList, self).__init__(host, 'list', help=_('list bookmarks')) | 39 super(BookmarksList, self).__init__(host, 'list', help=_('list bookmarks')) |
43 | 40 |
44 def start(self): | 41 async def start(self): |
45 data = self.host.bridge.bookmarksList(self.args.type, self.args.location, self.host.profile) | 42 try: |
43 data = await self.host.bridge.bookmarksList( | |
44 self.args.type, self.args.location, self.host.profile) | |
45 except Exception as e: | |
46 self.disp(f"can't get bookmarks list: {e}", error=True) | |
47 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
48 | |
46 mess = [] | 49 mess = [] |
47 for location in STORAGE_LOCATIONS: | 50 for location in STORAGE_LOCATIONS: |
48 if not data[location]: | 51 if not data[location]: |
49 continue | 52 continue |
50 loc_mess = [] | 53 loc_mess = [] |
51 loc_mess.append("%s:" % location) | 54 loc_mess.append(f"{location}:") |
52 book_mess = [] | 55 book_mess = [] |
53 for book_link, book_data in list(data[location].items()): | 56 for book_link, book_data in list(data[location].items()): |
54 name = book_data.get('name') | 57 name = book_data.get('name') |
55 autojoin = book_data.get('autojoin', 'false') == 'true' | 58 autojoin = book_data.get('autojoin', 'false') == 'true' |
56 nick = book_data.get('nick') | 59 nick = book_data.get('nick') |
60 ' (*)' if autojoin else '')) | 63 ' (*)' if autojoin else '')) |
61 loc_mess.append('\n'.join(book_mess)) | 64 loc_mess.append('\n'.join(book_mess)) |
62 mess.append('\n'.join(loc_mess)) | 65 mess.append('\n'.join(loc_mess)) |
63 | 66 |
64 print('\n\n'.join(mess)) | 67 print('\n\n'.join(mess)) |
68 self.host.quit() | |
65 | 69 |
66 | 70 |
67 class BookmarksRemove(BookmarksCommon): | 71 class BookmarksRemove(BookmarksCommon): |
68 | 72 |
69 def __init__(self, host): | 73 def __init__(self, host): |
70 super(BookmarksRemove, self).__init__(host, 'remove', help=_('remove a bookmark')) | 74 super(BookmarksRemove, self).__init__(host, 'remove', help=_('remove a bookmark')) |
71 self.need_loop = True | |
72 | 75 |
73 def add_parser_options(self): | 76 def add_parser_options(self): |
74 super(BookmarksRemove, self).add_parser_options() | 77 super(BookmarksRemove, self).add_parser_options() |
75 self.parser.add_argument('bookmark', help=_('jid (for muc bookmark) or url of to remove')) | 78 self.parser.add_argument( |
79 'bookmark', help=_('jid (for muc bookmark) or url of to remove')) | |
80 self.parser.add_argument( | |
81 "-f", "--force", action="store_true", | |
82 help=_("delete bookmark without confirmation"),) | |
76 | 83 |
77 def start(self): | 84 async def start(self): |
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) | 85 if not self.args.force: |
86 await self.host.confirmOrQuit(_("Are you sure to delete this bookmark?")) | |
87 | |
88 try: | |
89 await self.host.bridge.bookmarksRemove( | |
90 self.args.type, self.args.bookmark, self.args.location, self.host.profile) | |
91 except Exception as e: | |
92 self.disp(_(f"can't delete bookmark: {e}"), error=True) | |
93 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
94 else: | |
95 self.disp(_('bookmark deleted')) | |
96 self.host.quit() | |
79 | 97 |
80 | 98 |
81 class BookmarksAdd(BookmarksCommon): | 99 class BookmarksAdd(BookmarksCommon): |
82 | 100 |
83 def __init__(self, host): | 101 def __init__(self, host): |
84 super(BookmarksAdd, self).__init__(host, 'add', help=_('add a bookmark')) | 102 super(BookmarksAdd, self).__init__(host, 'add', help=_('add a bookmark')) |
85 self.need_loop = True | |
86 | 103 |
87 def add_parser_options(self): | 104 def add_parser_options(self): |
88 super(BookmarksAdd, self).add_parser_options(location_default='auto') | 105 super(BookmarksAdd, self).add_parser_options(location_default='auto') |
89 self.parser.add_argument('bookmark', help=_('jid (for muc bookmark) or url of to remove')) | 106 self.parser.add_argument( |
107 'bookmark', help=_('jid (for muc bookmark) or url of to remove')) | |
90 self.parser.add_argument('-n', '--name', help=_("bookmark name")) | 108 self.parser.add_argument('-n', '--name', help=_("bookmark name")) |
91 muc_group = self.parser.add_argument_group(_('MUC specific options')) | 109 muc_group = self.parser.add_argument_group(_('MUC specific options')) |
92 muc_group.add_argument('-N', '--nick', help=_('nickname')) | 110 muc_group.add_argument('-N', '--nick', help=_('nickname')) |
93 muc_group.add_argument('-a', '--autojoin', action='store_true', help=_('join room on profile connection')) | 111 muc_group.add_argument( |
112 '-a', '--autojoin', action='store_true', | |
113 help=_('join room on profile connection')) | |
94 | 114 |
95 def start(self): | 115 async def start(self): |
96 if self.args.type == 'url' and (self.args.autojoin or self.args.nick is not None): | 116 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 ? | 117 self.parser.error(_("You can't use --autojoin or --nick with --type url")) |
98 print(_("You can't use --autojoin or --nick with --type url")) | |
99 self.host.quit(1) | |
100 data = {} | 118 data = {} |
101 if self.args.autojoin: | 119 if self.args.autojoin: |
102 data['autojoin'] = 'true' | 120 data['autojoin'] = 'true' |
103 if self.args.nick is not None: | 121 if self.args.nick is not None: |
104 data['nick'] = self.args.nick | 122 data['nick'] = self.args.nick |
105 if self.args.name is not None: | 123 if self.args.name is not None: |
106 data['name'] = self.args.name | 124 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) | 125 try: |
126 await self.host.bridge.bookmarksAdd( | |
127 self.args.type, self.args.bookmark, data, self.args.location, | |
128 self.host.profile) | |
129 except Exception as e: | |
130 self.disp(f"can't add bookmark: {e}", error=True) | |
131 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
132 else: | |
133 self.disp(_('bookmark successfully added')) | |
134 self.host.quit() | |
108 | 135 |
109 | 136 |
110 class Bookmarks(base.CommandBase): | 137 class Bookmarks(base.CommandBase): |
111 subcommands = (BookmarksList, BookmarksRemove, BookmarksAdd) | 138 subcommands = (BookmarksList, BookmarksRemove, BookmarksAdd) |
112 | 139 |