diff frontends/src/jp/cmd_pubsub.py @ 2281:4af1805cc6df

jp (pubsub/delete): delete command implementation (to delete an item)
author Goffi <goffi@goffi.org>
date Wed, 28 Jun 2017 20:28:58 +0200
parents 4bc9a2c2d6c9
children d5c75be1c8c0
line wrap: on
line diff
--- a/frontends/src/jp/cmd_pubsub.py	Wed Jun 28 20:28:24 2017 +0200
+++ b/frontends/src/jp/cmd_pubsub.py	Wed Jun 28 20:28:58 2017 +0200
@@ -299,6 +299,42 @@
             callback=self.psItemsGetCb,
             errback=self.psItemsGetEb)
 
+class Delete(base.CommandBase):
+
+    def __init__(self, host):
+        base.CommandBase.__init__(self, host, 'delete', use_pubsub_node_req=True, help=_(u'delete an item'))
+        self.need_loop=True
+
+    def add_parser_options(self):
+        self.parser.add_argument("item", nargs='?', type=base.unicode_decoder, help=_(u"item to delete"))
+        self.parser.add_argument("-f", "--force", action='store_true', help=_(u"delete without confirmation"))
+        self.parser.add_argument("-n", "--notify", action='store_true', help=_(u"notify deletion"))
+
+    def psItemsDeleteCb(self):
+        self.disp(_(u'item {item_id} has been deleted').format(item_id=self.args.item))
+        self.host.quit(C.EXIT_OK)
+
+    def start(self):
+        common.checkURI(self.args)
+        if not self.args.item:
+            self.parser.error(_(u"You need to specify an item to delete"))
+        if not self.args.force:
+            message = _(u"Are you sure to delete item {item_id} ?").format(item_id=self.args.item)
+            res = raw_input("{} (y/N)? ".format(message))
+            if res not in ("y", "Y"):
+                self.disp(_(u"Item deletion cancelled"))
+                self.host.quit(2)
+        self.host.bridge.psRetractItem(
+            self.args.service,
+            self.args.node,
+            self.args.item,
+            self.args.notify,
+            self.profile,
+            callback=self.psItemsDeleteCb,
+            errback=partial(self.errback,
+                            msg=_(u"can't delete item: {}"),
+                            exit_code=C.EXIT_BRIDGE_ERRBACK))
+
 
 class Edit(base.CommandBase, common.BaseEdit):
 
@@ -411,7 +447,7 @@
 
 
 class Pubsub(base.CommandBase):
-    subcommands = (Get, Edit, Node, Affiliations, Uri)
+    subcommands = (Get, Delete, Edit, Node, Affiliations, Uri)
 
     def __init__(self, host):
         super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management'))