diff sat_frontends/jp/cmd_invitation.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_invitation.py	Wed Sep 25 08:53:38 2019 +0200
+++ b/sat_frontends/jp/cmd_invitation.py	Wed Sep 25 08:56:41 2019 +0200
@@ -23,7 +23,6 @@
 from sat_frontends.jp.constants import Const as C
 from sat.tools.common.ansi import ANSI as A
 from sat.tools.common import data_format
-from functools import partial
 
 __commands__ = ["Invitation"]
 
@@ -38,7 +37,6 @@
             use_output=C.OUTPUT_DICT,
             help=_("create and send an invitation"),
         )
-        self.need_loop = True
 
     def add_parser_options(self):
         self.parser.add_argument(
@@ -115,17 +113,7 @@
             help="profile doing the invitation (default: don't associate profile)",
         )
 
-    def invitationCreateCb(self, invitation_data):
-        self.output(invitation_data)
-        self.host.quit(C.EXIT_OK)
-
-    def invitationCreateEb(self, failure_):
-        self.disp(
-            "can't create invitation: {reason}".format(reason=failure_), error=True
-        )
-        self.host.quit(C.EXIT_BRIDGE_ERRBACK)
-
-    def start(self):
+    async def start(self):
         extra = dict(self.args.extra)
         email = self.args.email[0] if self.args.email else None
         emails_extra = self.args.email[1:]
@@ -139,22 +127,27 @@
                     _("you need to specify an email address to send email invitation")
                 )
 
-        self.host.bridge.invitationCreate(
-            email,
-            emails_extra,
-            self.args.jid,
-            self.args.password,
-            self.args.name,
-            self.args.host_name,
-            self.args.lang,
-            self.args.url,
-            self.args.subject,
-            self.args.body,
-            extra,
-            self.args.profile,
-            callback=self.invitationCreateCb,
-            errback=self.invitationCreateEb,
-        )
+        try:
+            invitation_data = await self.host.bridge.invitationCreate(
+                email,
+                emails_extra,
+                self.args.jid,
+                self.args.password,
+                self.args.name,
+                self.args.host_name,
+                self.args.lang,
+                self.args.url,
+                self.args.subject,
+                self.args.body,
+                extra,
+                self.args.profile,
+            )
+        except Exception as e:
+            self.disp(f"can't create invitation: {e}", error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        else:
+            await self.output(invitation_data)
+            self.host.quit(C.EXIT_OK)
 
 
 class Get(base.CommandBase):
@@ -167,7 +160,6 @@
             use_output=C.OUTPUT_DICT,
             help=_("get invitation data"),
         )
-        self.need_loop = True
 
     def add_parser_options(self):
         self.parser.add_argument(
@@ -180,52 +172,45 @@
             help=_("start profile session and retrieve jid"),
         )
 
-    def output_data(self, data, jid_=None):
+    async def output_data(self, data, jid_=None):
         if jid_ is not None:
             data["jid"] = jid_
-        self.output(data)
+        await self.output(data)
         self.host.quit()
 
-    def invitationGetCb(self, invitation_data):
-        if self.args.with_jid:
+    async def start(self):
+        try:
+            invitation_data = await self.host.bridge.invitationGet(
+                self.args.id,
+            )
+        except Exception as e:
+            self.disp(msg=_(f"can't get invitation data: {e}"), error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+
+        if not self.args.with_jid:
+            await self.output_data(invitation_data)
+        else:
             profile = invitation_data["guest_profile"]
+            try:
+                await self.host.bridge.profileStartSession(
+                    invitation_data["password"],
+                    profile,
+                )
+            except Exception as e:
+                self.disp(msg=_(f"can't start session: {e}"), error=True)
+                self.host.quit(C.EXIT_BRIDGE_ERRBACK)
 
-            def session_started(__):
-                self.host.bridge.asyncGetParamA(
+            try:
+                jid_ = await self.host.bridge.asyncGetParamA(
                     "JabberID",
                     "Connection",
                     profile_key=profile,
-                    callback=lambda jid_: self.output_data(invitation_data, jid_),
-                    errback=partial(
-                        self.errback,
-                        msg=_("can't retrieve jid: {}"),
-                        exit_code=C.EXIT_BRIDGE_ERRBACK,
-                    ),
                 )
+            except Exception as e:
+                self.disp(msg=_(f"can't retrieve jid: {e}"), error=True)
+                self.host.quit(C.EXIT_BRIDGE_ERRBACK)
 
-            self.host.bridge.profileStartSession(
-                invitation_data["password"],
-                profile,
-                callback=session_started,
-                errback=partial(
-                    self.errback,
-                    msg=_("can't start session: {}"),
-                    exit_code=C.EXIT_BRIDGE_ERRBACK,
-                ),
-            )
-        else:
-            self.output_data(invitation_data)
-
-    def start(self):
-        self.host.bridge.invitationGet(
-            self.args.id,
-            callback=self.invitationGetCb,
-            errback=partial(
-                self.errback,
-                msg=_("can't get invitation data: {}"),
-                exit_code=C.EXIT_BRIDGE_ERRBACK,
-            ),
-        )
+            await self.output_data(invitation_data, jid_)
 
 
 class Modify(base.CommandBase):
@@ -233,7 +218,6 @@
         base.CommandBase.__init__(
             self, host, "modify", use_profile=False, help=_("modify existing invitation")
         )
-        self.need_loop = True
 
     def add_parser_options(self):
         self.parser.add_argument(
@@ -283,17 +267,7 @@
             "id", help=_("invitation UUID")
         )
 
-    def invitationModifyCb(self):
-        self.disp(_("invitations have been modified correctly"))
-        self.host.quit(C.EXIT_OK)
-
-    def invitationModifyEb(self, failure_):
-        self.disp(
-            "can't create invitation: {reason}".format(reason=failure_), error=True
-        )
-        self.host.quit(C.EXIT_BRIDGE_ERRBACK)
-
-    def start(self):
+    async def start(self):
         extra = dict(self.args.extra)
         for arg_name in ("name", "host_name", "email", "language", "profile"):
             value = getattr(self.args, arg_name)
@@ -301,18 +275,23 @@
                 continue
             if arg_name in extra:
                 self.parser.error(
-                    _(
-                        "you can't set {arg_name} in both optional argument and extra"
-                    ).format(arg_name=arg_name)
+                    _(f"you can't set {arg_name} in both optional argument and extra")
                 )
             extra[arg_name] = value
-        self.host.bridge.invitationModify(
-            self.args.id,
-            extra,
-            self.args.replace,
-            callback=self.invitationModifyCb,
-            errback=self.invitationModifyEb,
-        )
+        try:
+            await self.host.bridge.invitationModify(
+                self.args.id,
+                extra,
+                self.args.replace,
+            )
+        except Exception as e:
+            self.disp(
+                f"can't modify invitation: {e}", error=True
+            )
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        else:
+            self.disp(_("invitations have been modified successfuly"))
+            self.host.quit(C.EXIT_OK)
 
 
 class List(base.CommandBase):
@@ -327,7 +306,6 @@
             extra_outputs=extra_outputs,
             help=_("list invitations data"),
         )
-        self.need_loop = True
 
     def default_output(self, data):
         for idx, datum in enumerate(data.items()):
@@ -347,20 +325,17 @@
             help=_("return only invitations linked to this profile"),
         )
 
-    def invitationListCb(self, data):
-        self.output(data)
-        self.host.quit()
-
-    def start(self):
-        self.host.bridge.invitationList(
-            self.args.profile,
-            callback=self.invitationListCb,
-            errback=partial(
-                self.errback,
-                msg=_("can't list invitations: {}"),
-                exit_code=C.EXIT_BRIDGE_ERRBACK,
-            ),
-        )
+    async def start(self):
+        try:
+            data = await self.host.bridge.invitationList(
+                self.args.profile,
+            )
+        except Exception as e:
+            self.disp(f"return only invitations linked to this profile: {e}", error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        else:
+            await self.output(data)
+            self.host.quit()
 
 
 class Invitation(base.CommandBase):