Mercurial > libervia-backend
diff sat_frontends/jp/cmd_account.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_account.py Wed Sep 25 08:53:38 2019 +0200 +++ b/sat_frontends/jp/cmd_account.py Wed Sep 25 08:56:41 2019 +0200 @@ -39,7 +39,6 @@ use_verbose=True, help=_("create a XMPP account"), ) - self.need_loop = True def add_parser_options(self): self.parser.add_argument( @@ -72,80 +71,81 @@ "--port", type=int, default=0, - help=_("server port (IP address or domain, default: use localhost)"), - ) - - def _setParamCb(self): - self.host.bridge.setParam( - "Password", - self.args.password, - "Connection", - profile_key=self.args.profile, - callback=self.host.quit, - errback=self.errback, - ) - - def _session_started(self, __): - self.host.bridge.setParam( - "JabberID", - self.args.jid, - "Connection", - profile_key=self.args.profile, - callback=self._setParamCb, - errback=self.errback, + help=_(f"server port (default: {C.XMPP_C2S_PORT})"), ) - def _profileCreateCb(self): - self.disp(_("profile created"), 1) - self.host.bridge.profileStartSession( - self.args.password, - self.args.profile, - callback=self._session_started, - errback=self.errback, - ) + async def start(self): + try: + await self.host.bridge.inBandAccountNew( + self.args.jid, + self.args.password, + self.args.email, + self.args.host, + self.args.port, + ) + except Exception as e: + self.disp( + f"can't create account on {self.args.host or 'localhost'!r} with jid " + f"{self.args.jid!r} using In-Band Registration: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) - def _profileCreateEb(self, failure_): - self.disp( - _( - "Can't create profile {profile} to associate with jid {jid}: {msg}" - ).format(profile=self.args.profile, jid=self.args.jid, msg=failure_), - error=True, - ) - self.host.quit(C.EXIT_BRIDGE_ERRBACK) + self.disp(_("XMPP account created"), 1) + + if self.args.profile is None: + self.host.quit() - def accountNewCb(self): - self.disp(_("XMPP account created"), 1) - if self.args.profile is not None: - self.disp(_("creating profile"), 2) - self.host.bridge.profileCreate( + + self.disp(_("creating profile"), 2) + try: + await self.host.bridge.profileCreate( self.args.profile, self.args.password, "", - callback=self._profileCreateCb, - errback=self._profileCreateEb, + ) + except Exception as e: + self.disp( + _(f"Can't create profile {self.args.profile} to associate with jid " + f"{self.args.jid}: {e}"), + error=True, + ) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + + self.disp(_("profile created"), 1) + try: + await self.host.bridge.profileStartSession( + self.args.password, + self.args.profile, ) - else: - self.host.quit() + except Exception as e: + self.disp(f"can't start profile session: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) - def accountNewEb(self, failure_): + try: + await self.host.bridge.setParam( + "JabberID", + self.args.jid, + "Connection", + profile_key=self.args.profile, + ) + except Exception as e: + self.disp(f"can't set JabberID parameter: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + + try: + await self.host.bridge.setParam( + "Password", + self.args.password, + "Connection", + profile_key=self.args.profile, + ) + except Exception as e: + self.disp(f"can't set Password parameter: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + self.disp( - _("Can't create new account on server {host} with jid {jid}: {msg}").format( - host=self.args.host or "localhost", jid=self.args.jid, msg=failure_ - ), - error=True, - ) - self.host.quit(C.EXIT_BRIDGE_ERRBACK) - - def start(self): - self.host.bridge.inBandAccountNew( - self.args.jid, - self.args.password, - self.args.email, - self.args.host, - self.args.port, - callback=self.accountNewCb, - errback=self.accountNewEb, - ) + f"profile {self.args.profile} successfully created and associated to the new " + f"account", 1) + self.host.quit() class AccountModify(base.CommandBase): @@ -153,20 +153,23 @@ super(AccountModify, self).__init__( host, "modify", help=_("change password for XMPP account") ) - self.need_loop = True def add_parser_options(self): self.parser.add_argument( "password", help=_("new XMPP password") ) - def start(self): - self.host.bridge.inBandPasswordChange( - self.args.password, - self.args.profile, - callback=self.host.quit, - errback=self.errback, - ) + async def start(self): + try: + await self.host.bridge.inBandPasswordChange( + self.args.password, + self.args.profile, + ) + except Exception as e: + self.disp(f"can't change XMPP password: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + else: + self.host.quit() class AccountDelete(base.CommandBase): @@ -174,7 +177,6 @@ super(AccountDelete, self).__init__( host, "delete", help=_("delete a XMPP account") ) - self.need_loop = True def add_parser_options(self): self.parser.add_argument( @@ -184,32 +186,33 @@ help=_("delete account without confirmation"), ) - def _got_jid(self, jid_str): + async def start(self): + try: + jid_str = await self.host.bridge.asyncGetParamA( + "JabberID", + "Connection", + profile_key=self.profile, + ) + except Exception as e: + self.disp(f"can't get JID of the profile: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + jid_ = jid.JID(jid_str) if not self.args.force: message = ( - "You are about to delete the XMPP account with jid {jid_}\n" - 'This is the XMPP account of profile "{profile}"\n' - "Are you sure that you want to delete this account ?".format( - jid_=jid_, profile=self.profile - ) + f"You are about to delete the XMPP account with jid {jid_!r}\n" + f"This is the XMPP account of profile {self.profile!r}\n" + f"Are you sure that you want to delete this account?" ) - res = input("{} (y/N)? ".format(message)) - if res not in ("y", "Y"): - self.disp(_("Account deletion cancelled")) - self.host.quit(2) - self.host.bridge.inBandUnregister( - jid_.domain, self.args.profile, callback=self.host.quit, errback=self.errback - ) + await self.host.confirmOrQuit(message, _("Account deletion cancelled")) - def start(self): - self.host.bridge.asyncGetParamA( - "JabberID", - "Connection", - profile_key=self.profile, - callback=self._got_jid, - errback=self.errback, - ) + try: + await self.host.bridge.inBandUnregister(jid_.domain, self.args.profile) + except Exception as e: + self.disp(f"can't delete XMPP account with jid {jid_!r}: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + + self.host.quit() class Account(base.CommandBase):