From 19b58d86f5c8e9cc761f0e1e0f85be32016e4235 Mon Sep 17 00:00:00 2001 From: mathieui Date: Fri, 29 Jan 2021 17:56:51 +0100 Subject: bookmarks: get rid of callbacks --- poezio/bookmarks.py | 54 +++++++++++++++++++------------- poezio/core/commands.py | 75 +++++++++++++++++++++++++++------------------ poezio/tabs/bookmarkstab.py | 19 ++++++++---- 3 files changed, 91 insertions(+), 57 deletions(-) diff --git a/poezio/bookmarks.py b/poezio/bookmarks.py index f62f67ce..b1ad1f9c 100644 --- a/poezio/bookmarks.py +++ b/poezio/bookmarks.py @@ -30,9 +30,19 @@ Adding a remote bookmark: import functools import logging -from typing import Optional, List, Union - -from slixmpp import InvalidJID, JID +from typing import ( + Callable, + List, + Optional, + Union, +) + +from slixmpp import ( + InvalidJID, + JID, + ClientXMPP, +) +from slixmpp.exceptions import IqError, IqTimeout from slixmpp.plugins.xep_0048 import Bookmarks, Conference, URL from poezio.config import config @@ -213,17 +223,17 @@ class BookmarkList: self.preferred = value config.set_and_save('use_bookmarks_method', value) - def save_remote(self, xmpp, callback): + async def save_remote(self, xmpp: ClientXMPP): """Save the remote bookmarks.""" if not any(self.available_storage.values()): return method = 'xep_0049' if self.preferred == 'privatexml' else 'xep_0223' if method: - xmpp.plugin['xep_0048'].set_bookmarks( + return await xmpp.plugin['xep_0048'].set_bookmarks( stanza_storage(self.bookmarks), method=method, - callback=callback) + ) def save_local(self): """Save the local bookmarks.""" @@ -231,22 +241,24 @@ class BookmarkList: if bookmark.method == 'local') config.set_and_save('rooms', local) - def save(self, xmpp, core=None, callback=None): + async def save(self, xmpp: ClientXMPP, core=None): """Save all the bookmarks.""" self.save_local() - - def _cb(iq): - if callback: - callback(iq) - if iq["type"] == "error" and core: - core.information('Could not save remote bookmarks.', 'Error') - elif core: - core.information('Bookmarks saved', 'Info') - if config.get('use_remote_bookmarks'): - self.save_remote(xmpp, _cb) - - async def get_pep(self, xmpp): + try: + result = await self.save_remote(xmpp) + if core is not None: + core.information('Bookmarks saved', 'Info') + return result + except (IqError, IqTimeout) as iq: + if core is not None: + core.information( + 'Could not save remote bookmarks.', + 'Error' + ) + raise + + async def get_pep(self, xmpp: ClientXMPP): """Add the remotely stored bookmarks via pep to the list.""" iq = await xmpp.plugin['xep_0048'].get_bookmarks(method='xep_0223') for conf in iq['pubsub']['items']['item']['bookmarks'][ @@ -257,7 +269,7 @@ class BookmarkList: self.append(bookm) return iq - async def get_privatexml(self, xmpp): + async def get_privatexml(self, xmpp: ClientXMPP): """ Fetch the remote bookmarks stored via privatexml. """ @@ -268,7 +280,7 @@ class BookmarkList: self.append(bookm) return iq - async def get_remote(self, xmpp, information): + async def get_remote(self, xmpp: ClientXMPP, information: Callable): """Add the remotely stored bookmarks to the list.""" if xmpp.anon or not any(self.available_storage.values()): information('No remote bookmark storage available', 'Warning') diff --git a/poezio/core/commands.py b/poezio/core/commands.py index 45704978..a665d25c 100644 --- a/poezio/core/commands.py +++ b/poezio/core/commands.py @@ -440,12 +440,14 @@ class CommandCore: room, nick = self._parse_join_jid(args[0] if args else '') password = args[1] if len(args) > 1 else None - self._add_bookmark( - room=room, - nick=nick, - autojoin=True, - password=password, - method='local', + asyncio.ensure_future( + self._add_bookmark( + room=room, + nick=nick, + autojoin=True, + password=password, + method='local', + ) ) @command_args_parser.quoted(0, 3) @@ -463,9 +465,11 @@ class CommandCore: autojoin = (method == 'local' or (len(args) > 1 and args[1].lower() == 'true')) - self._add_bookmark(room, nick, autojoin, password, method) + asyncio.ensure_future( + self._add_bookmark(room, nick, autojoin, password, method) + ) - def _add_bookmark( + async def _add_bookmark( self, room: Optional[str], nick: Optional[str], @@ -496,7 +500,7 @@ class CommandCore: if password is None and tab.password is not None: password = tab.password elif room == '*': - return self._add_wildcard_bookmarks(method) + return await self._add_wildcard_bookmarks(method) # Once we found which room to bookmark, find corresponding tab if it # exists and fill nickname if none was specified and not default. @@ -528,10 +532,15 @@ class CommandCore: bookmark.password = password self.core.bookmarks.save_local() - self.core.bookmarks.save_remote(self.core.xmpp, - self.core.handler.on_bookmark_result) + try: + result = await self.core.bookmarks.save_remote( + self.core.xmpp, + ) + self.core.handler.on_bookmark_result(result) + except (IqError, IqTimeout) as iq: + self.core.handler.on_bookmark_result(iq) - def _add_wildcard_bookmarks(self, method): + async def _add_wildcard_bookmarks(self, method): new_bookmarks = [] for tab in self.core.get_tabs(tabs.MucTab): bookmark = self.core.bookmarks[tab.jid.bare] @@ -545,8 +554,11 @@ class CommandCore: new_bookmarks.extend(self.core.bookmarks.bookmarks) self.core.bookmarks.set(new_bookmarks) self.core.bookmarks.save_local() - self.core.bookmarks.save_remote(self.core.xmpp, - self.core.handler.on_bookmark_result) + try: + iq = await self.core.bookmarks.save_remote(self.core.xmpp) + self.core.handler.on_bookmark_result(iq) + except IqError as iq: + self.core.handler.on_bookmark_result(iq) @command_args_parser.ignored def bookmarks(self): @@ -563,27 +575,30 @@ class CommandCore: @command_args_parser.quoted(0, 1) def remove_bookmark(self, args): """/remove_bookmark [jid]""" + jid = None + if not args: + tab = self.core.tabs.current_tab + if isinstance(tab, tabs.MucTab): + jid = tab.jid.bare + else: + jid = args[0] + + asyncio.ensure_future( + self._remove_bookmark_routine(jid) + ) - def cb(success): - if success: + async def _remove_bookmark_routine(self, jid: str): + """Asynchronously remove a bookmark""" + if self.core.bookmarks[jid]: + self.core.bookmarks.remove(jid) + try: + await self.core.bookmarks.save(self.core.xmpp) self.core.information('Bookmark deleted', 'Info') - else: + except (IqError, IqTimeout): self.core.information('Error while deleting the bookmark', 'Error') - - if not args: - tab = self.core.tabs.current_tab - if isinstance(tab, tabs.MucTab) and self.core.bookmarks[tab.jid.bare]: - self.core.bookmarks.remove(tab.jid.bare) - self.core.bookmarks.save(self.core.xmpp, callback=cb) - else: - self.core.information('No bookmark to remove', 'Info') else: - if self.core.bookmarks[args[0]]: - self.core.bookmarks.remove(args[0]) - self.core.bookmarks.save(self.core.xmpp, callback=cb) - else: - self.core.information('No bookmark to remove', 'Info') + self.core.information('No bookmark to remove', 'Info') @deny_anonymous @command_args_parser.quoted(0, 1) diff --git a/poezio/tabs/bookmarkstab.py b/poezio/tabs/bookmarkstab.py index c4fdadd3..a953c750 100644 --- a/poezio/tabs/bookmarkstab.py +++ b/poezio/tabs/bookmarkstab.py @@ -2,9 +2,12 @@ Defines the data-forms Tab """ +import asyncio import logging from typing import Dict, Callable, List +from slixmpp.exceptions import IqError, IqTimeout + from poezio import windows from poezio.bookmarks import Bookmark, BookmarkList from poezio.core.structs import Command @@ -89,17 +92,21 @@ class BookmarksTab(Tab): 'Error') return + for bm in self.removed_bookmarks: if bm in self.bookmarks: self.bookmarks.remove(bm) - def send_cb(success): - if success: - self.core.information('Bookmarks saved', 'Info') - else: - self.core.information('Remote bookmarks not saved.', 'Error') + asyncio.ensure_future( + self.save_routine() + ) - self.bookmarks.save(self.core.xmpp, callback=send_cb) + async def save_routine(self): + try: + await self.bookmarks.save(self.core.xmpp) + self.core.information('Bookmarks saved', 'Info') + except (IqError, IqTimeout): + self.core.information('Remote bookmarks not saved.', 'Error') self.core.close_tab(self) return True -- cgit v1.2.3