diff sat_frontends/jp/cmd_forums.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_forums.py	Wed Sep 25 08:53:38 2019 +0200
+++ b/sat_frontends/jp/cmd_forums.py	Wed Sep 25 08:56:41 2019 +0200
@@ -23,7 +23,6 @@
 from sat_frontends.jp.constants import Const as C
 from sat_frontends.jp import common
 from sat.tools.common.ansi import ANSI as A
-from functools import partial
 import codecs
 import json
 
@@ -46,7 +45,6 @@
             help=_("edit forums"),
         )
         common.BaseEdit.__init__(self, self.host, FORUMS_TMP_DIR)
-        self.need_loop = True
 
     def add_parser_options(self):
         self.parser.add_argument(
@@ -60,26 +58,37 @@
         """return suffix used for content file"""
         return "json"
 
-    def forumsSetCb(self):
-        self.disp(_("forums have been edited"), 1)
-        self.host.quit()
+    async def publish(self, forums_raw):
+        try:
+            await self.host.bridge.forumsSet(
+                forums_raw,
+                self.args.service,
+                self.args.node,
+                self.args.key,
+                self.profile,
+            )
+        except Exception as e:
+            self.disp(f"can't set forums: {e}", error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        else:
+            self.disp(_("forums have been edited"), 1)
+            self.host.quit()
 
-    def publish(self, forums_raw):
-        self.host.bridge.forumsSet(
-            forums_raw,
-            self.args.service,
-            self.args.node,
-            self.args.key,
-            self.profile,
-            callback=self.forumsSetCb,
-            errback=partial(
-                self.errback,
-                msg=_("can't set forums: {}"),
-                exit_code=C.EXIT_BRIDGE_ERRBACK,
-            ),
-        )
+    async def start(self):
+        try:
+            forums_json = await self.host.bridge.forumsGet(
+                self.args.service,
+                self.args.node,
+                self.args.key,
+                self.profile,
+            )
+        except Exception as e:
+            if e.classname == "NotFound":
+                forums_json = ""
+            else:
+                self.disp(f"can't get node configuration: {e}", error=True)
+                self.host.quit(C.EXIT_BRIDGE_ERRBACK)
 
-    def forumsGetCb(self, forums_json):
         content_file_obj, content_file_path = self.getTmpFile()
         forums_json = forums_json.strip()
         if forums_json:
@@ -89,28 +98,7 @@
             f = codecs.getwriter("utf-8")(content_file_obj)
             json.dump(forums, f, ensure_ascii=False, indent=4)
             content_file_obj.seek(0)
-        self.runEditor("forums_editor_args", content_file_path, content_file_obj)
-
-    def forumsGetEb(self, failure_):
-        # FIXME: error handling with bridge is broken, need to be properly fixed
-        if failure_.condition == "item-not-found":
-            self.forumsGetCb("")
-        else:
-            self.errback(
-                failure_,
-                msg=_("can't get forums structure: {}"),
-                exit_code=C.EXIT_BRIDGE_ERRBACK,
-            )
-
-    def start(self):
-        self.host.bridge.forumsGet(
-            self.args.service,
-            self.args.node,
-            self.args.key,
-            self.profile,
-            callback=self.forumsGetCb,
-            errback=self.forumsGetEb,
-        )
+        await self.runEditor("forums_editor_args", content_file_path, content_file_obj)
 
 
 class Get(base.CommandBase):
@@ -126,7 +114,6 @@
             use_verbose=True,
             help=_("get forums structure"),
         )
-        self.need_loop = True
 
     def add_parser_options(self):
         self.parser.add_argument(
@@ -165,27 +152,24 @@
                         A.color(level * 4 * " ", head_color, key, A.RESET, ": ", value)
                     )
 
-    def forumsGetCb(self, forums_raw):
-        if not forums_raw:
-            self.disp(_("no schema found"), 1)
-            self.host.quit(1)
-        forums = json.loads(forums_raw)
-        self.output(forums)
-        self.host.quit()
-
-    def start(self):
-        self.host.bridge.forumsGet(
-            self.args.service,
-            self.args.node,
-            self.args.key,
-            self.profile,
-            callback=self.forumsGetCb,
-            errback=partial(
-                self.errback,
-                msg=_("can't get forums: {}"),
-                exit_code=C.EXIT_BRIDGE_ERRBACK,
-            ),
-        )
+    async def start(self):
+        try:
+            forums_raw = await self.host.bridge.forumsGet(
+                self.args.service,
+                self.args.node,
+                self.args.key,
+                self.profile,
+            )
+        except Exception as e:
+            self.disp(f"can't get forums: {e}", error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        else:
+            if not forums_raw:
+                self.disp(_("no schema found"), 1)
+                self.host.quit(1)
+            forums = json.loads(forums_raw)
+            await self.output(forums)
+            self.host.quit()
 
 
 class Forums(base.CommandBase):