diff sat_frontends/jp/cmd_adhoc.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_adhoc.py	Wed Sep 25 08:53:38 2019 +0200
+++ b/sat_frontends/jp/cmd_adhoc.py	Wed Sep 25 08:56:41 2019 +0200
@@ -19,7 +19,6 @@
 
 from . import base
 from sat.core.i18n import _
-from functools import partial
 from sat_frontends.jp.constants import Const as C
 from sat_frontends.jp import xmlui_manager
 
@@ -67,7 +66,7 @@
             "-l", "--loop", action="store_true", help=_("loop on the commands")
         )
 
-    def start(self):
+    async def start(self):
         name = self.args.software.lower()
         flags = []
         magics = {jid for jid in self.args.jids if jid.count("@") > 1}
@@ -75,29 +74,33 @@
         jids = set(self.args.jids).difference(magics)
         if self.args.loop:
             flags.append(FLAG_LOOP)
-        bus_name, methods = self.host.bridge.adHocDBusAddAuto(
-            name,
-            jids,
-            self.args.groups,
-            magics,
-            self.args.forbidden_jids,
-            self.args.forbidden_groups,
-            flags,
-            self.profile,
-        )
-        if not bus_name:
-            self.disp(_("No bus name found"), 1)
-            return
-        self.disp(_("Bus name found: [%s]" % bus_name), 1)
-        for method in methods:
-            path, iface, command = method
-            self.disp(
-                _(
-                    "Command found: (path:%(path)s, iface: %(iface)s) [%(command)s]"
-                    % {"path": path, "iface": iface, "command": command}
-                ),
-                1,
+        try:
+            bus_name, methods = await self.host.bridge.adHocDBusAddAuto(
+                name,
+                list(jids),
+                self.args.groups,
+                magics,
+                self.args.forbidden_jids,
+                self.args.forbidden_groups,
+                flags,
+                self.profile,
             )
+        except Exception as e:
+            self.disp(f"can't create remote control: {e}", error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        else:
+            if not bus_name:
+                self.disp(_("No bus name found"), 1)
+                self.host.quit(C.EXIT_NOT_FOUND)
+            else:
+                self.disp(_("Bus name found: [%s]" % bus_name), 1)
+                for method in methods:
+                    path, iface, command = method
+                    self.disp(
+                        _(f"Command found: (path:{path}, iface: {iface}) [{command}]"),
+                        1,
+                    )
+                self.host.quit()
 
 
 class Run(base.CommandBase):
@@ -107,7 +110,6 @@
         super(Run, self).__init__(
             host, "run", use_verbose=True, help=_("run an Ad-Hoc command")
         )
-        self.need_loop = True
 
     def add_parser_options(self):
         self.parser.add_argument(
@@ -140,28 +142,24 @@
             help=_("node of the command (default: list commands)"),
         )
 
-    def adHocRunCb(self, xmlui_raw):
-        xmlui = xmlui_manager.create(self.host, xmlui_raw)
-        workflow = self.args.workflow
-        xmlui.show(workflow)
-        if not workflow:
-            if xmlui.type == "form":
-                xmlui.submitForm()
-            else:
-                self.host.quit()
-
-    def start(self):
-        self.host.bridge.adHocRun(
-            self.args.jid,
-            self.args.node,
-            self.profile,
-            callback=self.adHocRunCb,
-            errback=partial(
-                self.errback,
-                msg=_("can't get ad-hoc commands list: {}"),
-                exit_code=C.EXIT_BRIDGE_ERRBACK,
-            ),
-        )
+    async def start(self):
+        try:
+            xmlui_raw = await self.host.bridge.adHocRun(
+                self.args.jid,
+                self.args.node,
+                self.profile,
+            )
+        except Exception as e:
+            self.disp(f"can't get ad-hoc commands list: {e}", error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        else:
+            xmlui = xmlui_manager.create(self.host, xmlui_raw)
+            workflow = self.args.workflow
+            await xmlui.show(workflow)
+            if not workflow:
+                if xmlui.type == "form":
+                    await xmlui.submitForm()
+            self.host.quit()
 
 
 class List(base.CommandBase):
@@ -171,7 +169,6 @@
         super(List, self).__init__(
             host, "list", use_verbose=True, help=_("list Ad-Hoc commands of a service")
         )
-        self.need_loop = True
 
     def add_parser_options(self):
         self.parser.add_argument(
@@ -181,23 +178,19 @@
             help=_("jid of the service (default: profile's server"),
         )
 
-    def adHocListCb(self, xmlui_raw):
-        xmlui = xmlui_manager.create(self.host, xmlui_raw)
-        xmlui.readonly = True
-        xmlui.show()
-        self.host.quit()
-
-    def start(self):
-        self.host.bridge.adHocList(
-            self.args.jid,
-            self.profile,
-            callback=self.adHocListCb,
-            errback=partial(
-                self.errback,
-                msg=_("can't get ad-hoc commands list: {}"),
-                exit_code=C.EXIT_BRIDGE_ERRBACK,
-            ),
-        )
+    async def start(self):
+        try:
+            xmlui_raw = await self.host.bridge.adHocList(
+                self.args.jid,
+                self.profile,
+            )
+        except Exception as e:
+            self.disp(f"can't get ad-hoc commands list: {e}", error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        else:
+            xmlui = xmlui_manager.create(self.host, xmlui_raw)
+            await xmlui.show(read_only=True)
+            self.host.quit()
 
 
 class AdHoc(base.CommandBase):