diff 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
line wrap: on
line diff
--- a/sat_frontends/jp/cmd_bookmarks.py	Wed Sep 25 08:53:38 2019 +0200
+++ b/sat_frontends/jp/cmd_bookmarks.py	Wed Sep 25 08:56:41 2019 +0200
@@ -19,6 +19,7 @@
 
 from . import base
 from sat.core.i18n import _
+from sat_frontends.jp.constants import Const as C
 
 __commands__ = ["Bookmarks"]
 
@@ -32,23 +33,25 @@
         self.parser.add_argument('-l', '--location', type=str, choices=(location_default,) + STORAGE_LOCATIONS, default=location_default, help=_("storage location (default: %(default)s)"))
         self.parser.add_argument('-t', '--type', type=str, choices=TYPES, default=TYPES[0], help=_("bookmarks type (default: %(default)s)"))
 
-    def _errback(self, failure):
-        print((("Something went wrong: [%s]") % failure))
-        self.host.quit(1)
-
 class BookmarksList(BookmarksCommon):
 
     def __init__(self, host):
         super(BookmarksList, self).__init__(host, 'list', help=_('list bookmarks'))
 
-    def start(self):
-        data = self.host.bridge.bookmarksList(self.args.type, self.args.location, self.host.profile)
+    async def start(self):
+        try:
+            data = await self.host.bridge.bookmarksList(
+                self.args.type, self.args.location, self.host.profile)
+        except Exception as e:
+            self.disp(f"can't get bookmarks list: {e}", error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+
         mess = []
         for location in STORAGE_LOCATIONS:
             if not data[location]:
                 continue
             loc_mess = []
-            loc_mess.append("%s:" % location)
+            loc_mess.append(f"{location}:")
             book_mess = []
             for book_link, book_data in list(data[location].items()):
                 name = book_data.get('name')
@@ -62,41 +65,56 @@
             mess.append('\n'.join(loc_mess))
 
         print('\n\n'.join(mess))
+        self.host.quit()
 
 
 class BookmarksRemove(BookmarksCommon):
 
     def __init__(self, host):
         super(BookmarksRemove, self).__init__(host, 'remove', help=_('remove a bookmark'))
-        self.need_loop = True
 
     def add_parser_options(self):
         super(BookmarksRemove, self).add_parser_options()
-        self.parser.add_argument('bookmark', help=_('jid (for muc bookmark) or url of to remove'))
+        self.parser.add_argument(
+            'bookmark', help=_('jid (for muc bookmark) or url of to remove'))
+        self.parser.add_argument(
+            "-f", "--force", action="store_true",
+            help=_("delete bookmark without confirmation"),)
+
+    async def start(self):
+        if not self.args.force:
+            await self.host.confirmOrQuit(_("Are you sure to delete this bookmark?"))
 
-    def start(self):
-        self.host.bridge.bookmarksRemove(self.args.type, self.args.bookmark, self.args.location, self.host.profile, callback = lambda: self.host.quit(), errback=self._errback)
+        try:
+            await self.host.bridge.bookmarksRemove(
+                self.args.type, self.args.bookmark, self.args.location, self.host.profile)
+        except Exception as e:
+            self.disp(_(f"can't delete bookmark: {e}"), error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        else:
+            self.disp(_('bookmark deleted'))
+            self.host.quit()
 
 
 class BookmarksAdd(BookmarksCommon):
 
     def __init__(self, host):
         super(BookmarksAdd, self).__init__(host, 'add', help=_('add a bookmark'))
-        self.need_loop = True
 
     def add_parser_options(self):
         super(BookmarksAdd, self).add_parser_options(location_default='auto')
-        self.parser.add_argument('bookmark', help=_('jid (for muc bookmark) or url of to remove'))
+        self.parser.add_argument(
+            'bookmark', help=_('jid (for muc bookmark) or url of to remove'))
         self.parser.add_argument('-n', '--name', help=_("bookmark name"))
         muc_group = self.parser.add_argument_group(_('MUC specific options'))
         muc_group.add_argument('-N', '--nick', help=_('nickname'))
-        muc_group.add_argument('-a', '--autojoin', action='store_true', help=_('join room on profile connection'))
+        muc_group.add_argument(
+            '-a', '--autojoin', action='store_true',
+            help=_('join room on profile connection'))
 
-    def start(self):
+    async def start(self):
         if self.args.type == 'url' and (self.args.autojoin or self.args.nick is not None):
-            # XXX: Argparse doesn't seem to manage this case, any better way ?
-            print(_("You can't use --autojoin or --nick with --type url"))
-            self.host.quit(1)
+            self.parser.error(_("You can't use --autojoin or --nick with --type url"))
         data = {}
         if self.args.autojoin:
             data['autojoin'] = 'true'
@@ -104,7 +122,16 @@
             data['nick'] = self.args.nick
         if self.args.name is not None:
             data['name'] = self.args.name
-        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)
+        try:
+            await self.host.bridge.bookmarksAdd(
+                self.args.type, self.args.bookmark, data, self.args.location,
+                self.host.profile)
+        except Exception as e:
+            self.disp(f"can't add bookmark: {e}", error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        else:
+            self.disp(_('bookmark successfully added'))
+            self.host.quit()
 
 
 class Bookmarks(base.CommandBase):