Mercurial > libervia-backend
diff sat_frontends/jp/cmd_encryption.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 | 7574f795bd1e |
line wrap: on
line diff
--- a/sat_frontends/jp/cmd_encryption.py Wed Sep 25 08:53:38 2019 +0200 +++ b/sat_frontends/jp/cmd_encryption.py Wed Sep 25 08:56:41 2019 +0200 @@ -20,7 +20,6 @@ from sat_frontends.jp import base from sat_frontends.jp.constants import Const as C from sat.core.i18n import _ -from functools import partial from sat.tools.common import data_format from sat_frontends.jp import xmlui_manager @@ -37,34 +36,27 @@ extra_outputs=extra_outputs, use_profile=False, help=_("show available encryption algorithms")) - self.need_loop = True def add_parser_options(self): pass - def encryptionPluginsGetCb(self, plugins): - self.output(plugins) - self.host.quit() - def default_output(self, plugins): if not plugins: self.disp(_("No encryption plugin registered!")) - self.host.quit(C.EXIT_NOT_FOUND) else: self.disp(_("Following encryption algorithms are available: {algos}").format( algos=', '.join([p['name'] for p in plugins]))) + + async def start(self): + try: + plugins = await self.host.bridge.encryptionPluginsGet() + except Exception as e: + self.disp(f"can't retrieve plugins: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + else: + await self.output(plugins) self.host.quit() - def start(self): - self.host.bridge.encryptionPluginsGet( - callback=self.encryptionPluginsGetCb, - errback=partial( - self.errback, - msg=_("can't retrieve plugins: {}"), - exit_code=C.EXIT_BRIDGE_ERRBACK, - ), - ) - class EncryptionGet(base.CommandBase): @@ -73,7 +65,6 @@ host, "get", use_output=C.OUTPUT_DICT, help=_("get encryption session data")) - self.need_loop = True def add_parser_options(self): self.parser.add_argument( @@ -81,28 +72,23 @@ help=_("jid of the entity to check") ) - def messageEncryptionGetCb(self, serialised): + async def start(self): + jids = await self.host.check_jids([self.args.jid]) + jid = jids[0] + try: + serialised = await self.host.bridge.messageEncryptionGet(jid, self.profile) + except Exception as e: + self.disp(f"can't get session: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + session_data = data_format.deserialise(serialised) if session_data is None: self.disp( "No encryption session found, the messages are sent in plain text.") self.host.quit(C.EXIT_NOT_FOUND) - self.output(session_data) + await self.output(session_data) self.host.quit() - def start(self): - jids = self.host.check_jids([self.args.jid]) - jid = jids[0] - self.host.bridge.messageEncryptionGet( - jid, self.profile, - callback=self.messageEncryptionGetCb, - errback=partial( - self.errback, - msg=_("can't get session: {}"), - exit_code=C.EXIT_BRIDGE_ERRBACK, - ), - ) - class EncryptionStart(base.CommandBase): @@ -110,7 +96,6 @@ super(EncryptionStart, self).__init__( host, "start", help=_("start encrypted session with an entity")) - self.need_loop = True def add_parser_options(self): self.parser.add_argument( @@ -128,30 +113,30 @@ help=_("jid of the entity to stop encrypted session with") ) - def encryptionNamespaceGetCb(self, namespace): - jids = self.host.check_jids([self.args.jid]) - jid = jids[0] - self.host.bridge.messageEncryptionStart( - jid, namespace, not self.args.encrypt_noreplace, - self.profile, - callback=self.host.quit, - errback=partial(self.errback, - msg=_("Can't start encryption session: {}"), - exit_code=C.EXIT_BRIDGE_ERRBACK, - )) + async def start(self): + if self.args.name is not None: + try: + namespace = await self.host.bridge.encryptionNamespaceGet(self.args.name) + except Exception as e: + self.disp(f"can't get encryption namespace: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + elif self.args.namespace is not None: + namespace = self.args.namespace + else: + namespace = "" - def start(self): - if self.args.name is not None: - self.host.bridge.encryptionNamespaceGet(self.args.name, - callback=self.encryptionNamespaceGetCb, - errback=partial(self.errback, - msg=_("Can't get encryption namespace: {}"), - exit_code=C.EXIT_BRIDGE_ERRBACK, - )) - elif self.args.namespace is not None: - self.encryptionNamespaceGetCb(self.args.namespace) - else: - self.encryptionNamespaceGetCb("") + jids = await self.host.check_jids([self.args.jid]) + jid = jids[0] + + try: + await self.host.bridge.messageEncryptionStart( + jid, namespace, not self.args.encrypt_noreplace, + self.profile) + except Exception as e: + self.disp(f"can't get encryption namespace: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + + self.host.quit() class EncryptionStop(base.CommandBase): @@ -160,7 +145,6 @@ super(EncryptionStop, self).__init__( host, "stop", help=_("stop encrypted session with an entity")) - self.need_loop = True def add_parser_options(self): self.parser.add_argument( @@ -168,18 +152,16 @@ help=_("jid of the entity to stop encrypted session with") ) - def start(self): - jids = self.host.check_jids([self.args.jid]) + async def start(self): + jids = await self.host.check_jids([self.args.jid]) jid = jids[0] - self.host.bridge.messageEncryptionStop( - jid, self.profile, - callback=self.host.quit, - errback=partial( - self.errback, - msg=_("can't end encrypted session: {}"), - exit_code=C.EXIT_BRIDGE_ERRBACK, - ), - ) + try: + await self.host.bridge.messageEncryptionStop(jid, self.profile) + except Exception as e: + self.disp(f"can't end encrypted session: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + + self.host.quit() class TrustUI(base.CommandBase): @@ -188,7 +170,6 @@ super(TrustUI, self).__init__( host, "ui", help=_("get UI to manage trust")) - self.need_loop = True def add_parser_options(self): self.parser.add_argument( @@ -202,37 +183,33 @@ "-N", "--namespace", help=_("algorithm namespace (DEFAULT: current algorithm)")) - def encryptionTrustUIGetCb(self, xmlui_raw): - xmlui = xmlui_manager.create(self.host, xmlui_raw) - xmlui.show() - xmlui.submitForm() + async def start(self): + if self.args.name is not None: + try: + namespace = await self.host.bridge.encryptionNamespaceGet(self.args.name) + except Exception as e: + self.disp(f"can't get encryption namespace: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + elif self.args.namespace is not None: + namespace = self.args.namespace + else: + namespace = "" - def encryptionNamespaceGetCb(self, namespace): - jids = self.host.check_jids([self.args.jid]) + jids = await self.host.check_jids([self.args.jid]) jid = jids[0] - self.host.bridge.encryptionTrustUIGet( - jid, namespace, self.profile, - callback=self.encryptionTrustUIGetCb, - errback=partial( - self.errback, - msg=_("can't end encrypted session: {}"), - exit_code=C.EXIT_BRIDGE_ERRBACK, - ), - ) - def start(self): - if self.args.name is not None: - self.host.bridge.encryptionNamespaceGet(self.args.name, - callback=self.encryptionNamespaceGetCb, - errback=partial(self.errback, - msg=_("Can't get encryption namespace: {}"), - exit_code=C.EXIT_BRIDGE_ERRBACK, - )) - elif self.args.namespace is not None: - self.encryptionNamespaceGetCb(self.args.namespace) - else: - self.encryptionNamespaceGetCb("") + try: + xmlui_raw = await self.host.bridge.encryptionTrustUIGet( + jid, namespace, self.profile) + except Exception as e: + self.disp(f"can't get encryption session trust UI: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + xmlui = xmlui_manager.create(self.host, xmlui_raw) + await xmlui.show() + if xmlui.type != C.XMLUI_DIALOG: + await xmlui.submitForm() + self.host.quit() class EncryptionTrust(base.CommandBase): subcommands = (TrustUI,)