changeset 3510:059742e925f2

jp (list): implement `set` and `delete` subcommands.
author Goffi <goffi@goffi.org>
date Thu, 22 Apr 2021 18:21:33 +0200
parents b977c74f9c85
children db0f4d419448
files sat_frontends/jp/cmd_list.py
diffstat 1 files changed, 120 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/sat_frontends/jp/cmd_list.py	Thu Apr 22 18:20:41 2021 +0200
+++ b/sat_frontends/jp/cmd_list.py	Thu Apr 22 18:21:33 2021 +0200
@@ -71,6 +71,125 @@
             self.host.quit(C.EXIT_OK)
 
 
+class Set(base.CommandBase):
+
+    def __init__(self, host):
+        base.CommandBase.__init__(
+            self,
+            host,
+            "set",
+            use_pubsub=True,
+            pubsub_defaults={"service": _("auto"), "node": _("auto")},
+            help=_("set a list item"),
+        )
+
+    def add_parser_options(self):
+        self.parser.add_argument(
+            "-f",
+            "--field",
+            action="append",
+            nargs="+",
+            dest="fields",
+            required=True,
+            metavar=("NAME", "VALUES"),
+            help=_("field(s) to set (required)"),
+        )
+        self.parser.add_argument(
+            "-U",
+            "--update",
+            choices=("auto", "true", "false"),
+            default="auto",
+            help=_("update existing item instead of replacing it (DEFAULT: auto)"),
+        )
+        self.parser.add_argument(
+            "item",
+            nargs="?",
+            default="",
+            help=_("id, URL of the item to update, or nothing for new item"),
+        )
+
+    async def start(self):
+        await common.fill_well_known_uri(self, os.getcwd(), "tickets", meta_map={})
+        if self.args.update == "auto":
+            # we update if we have a item id specified
+            update = bool(self.args.item)
+        else:
+            update = C.bool(self.args.update)
+
+        values = {}
+
+        for field_data in self.args.fields:
+            values.setdefault(field_data[0], []).extend(field_data[1:])
+
+        extra = {"update": update}
+
+        try:
+            item_id = await self.host.bridge.listSet(
+                self.args.service,
+                self.args.node,
+                values,
+                '',
+                self.args.item,
+                data_format.serialise(extra),
+                self.profile
+            )
+        except Exception as e:
+            self.disp(f"can't set list item: {e}", error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        else:
+            self.disp(f"item {str(item_id or self.args.item)!r} set successfully")
+            self.host.quit(C.EXIT_OK)
+
+
+class Delete(base.CommandBase):
+
+    def __init__(self, host):
+        base.CommandBase.__init__(
+            self,
+            host,
+            "delete",
+            use_pubsub=True,
+            pubsub_defaults={"service": _("auto"), "node": _("auto")},
+            help=_("delete a list item"),
+        )
+
+    def add_parser_options(self):
+        self.parser.add_argument(
+            "-f", "--force", action="store_true", help=_("delete without confirmation")
+        )
+        self.parser.add_argument(
+            "-N", "--notify", action="store_true", help=_("notify deletion")
+        )
+        self.parser.add_argument(
+            "item",
+            help=_("id of the item to delete"),
+        )
+
+    async def start(self):
+        await common.fill_well_known_uri(self, os.getcwd(), "tickets", meta_map={})
+        if not self.args.item:
+            self.parser.error(_("You need to specify a list item to delete"))
+        if not self.args.force:
+            message = _("Are you sure to delete list item {item_id} ?").format(
+                item_id=self.args.item
+            )
+            await self.host.confirmOrQuit(message, _("item deletion cancelled"))
+        try:
+            await self.host.bridge.listDeleteItem(
+                self.args.service,
+                self.args.node,
+                self.args.item,
+                self.args.notify,
+                self.profile,
+            )
+        except Exception as e:
+            self.disp(_(f"can't delete item: {e}"), error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        else:
+            self.disp(_(f"item {self.args.item} has been deleted"))
+            self.host.quit(C.EXIT_OK)
+
+
 class Import(base.CommandBase):
     # TODO: factorize with blog/import
 
@@ -212,7 +331,7 @@
 
 
 class List(base.CommandBase):
-    subcommands = (Get, Import)
+    subcommands = (Get, Set, Delete, Import)
 
     def __init__(self, host):
         super(List, self).__init__(