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.
This commit is contained in:
Goffi 2019-09-25 08:56:41 +02:00
commit 76aadbb3fb

View file

@ -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,33 +36,26 @@ class EncryptionAlgorithms(base.CommandBase):
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])))
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,
),
)
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()
class EncryptionGet(base.CommandBase):
@ -73,7 +65,6 @@ class EncryptionGet(base.CommandBase):
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 @@ class EncryptionGet(base.CommandBase):
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 @@ class EncryptionStart(base.CommandBase):
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 @@ class EncryptionStart(base.CommandBase):
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,
))
def start(self):
async 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,
))
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:
self.encryptionNamespaceGetCb(self.args.namespace)
namespace = self.args.namespace
else:
self.encryptionNamespaceGetCb("")
namespace = ""
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 @@ class EncryptionStop(base.CommandBase):
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 @@ class EncryptionStop(base.CommandBase):
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 @@ class TrustUI(base.CommandBase):
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 @@ class TrustUI(base.CommandBase):
"-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()
def encryptionNamespaceGetCb(self, namespace):
jids = 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):
async 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,
))
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:
self.encryptionNamespaceGetCb(self.args.namespace)
namespace = self.args.namespace
else:
self.encryptionNamespaceGetCb("")
namespace = ""
jids = await self.host.check_jids([self.args.jid])
jid = jids[0]
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,)