diff frontends/src/jp/cmd_pubsub.py @ 2221:a6c9bc4d1de0

jp (pubsub/node): added create and delete commands
author Goffi <goffi@goffi.org>
date Mon, 03 Apr 2017 00:23:01 +0200
parents 4e06cd44e667
children 87fcd4a7c7e4
line wrap: on
line diff
--- a/frontends/src/jp/cmd_pubsub.py	Mon Apr 03 00:23:01 2017 +0200
+++ b/frontends/src/jp/cmd_pubsub.py	Mon Apr 03 00:23:01 2017 +0200
@@ -21,11 +21,27 @@
 import base
 from sat.core.i18n import _
 from sat_frontends.jp.constants import Const as C
+from functools import partial
 
 __commands__ = ["Pubsub"]
 
 
-class NodeInfo(base.CommandBase):
+class NodeCommon(object):
+    node_required = True
+
+    def __init__(self, node_required=True):
+        self.node_required = node_required
+
+    def add_parser_options(self):
+        self.parser.add_argument("-s", "--service", type=base.unicode_decoder, default=u'',
+                                 help=_(u"JID of the PubSub service (default: PEP service)"))
+        if self.node_required:
+            self.parser.add_argument("node", type=base.unicode_decoder, help=_(u"node to request"))
+        else:
+            self.parser.add_argument("-n", "--node", type=base.unicode_decoder, default=u'', help=_(u"node to request"))
+
+
+class NodeInfo(base.CommandBase, NodeCommon):
 
     def __init__(self, host):
         base.CommandBase.__init__(self, host, 'info', use_output=C.OUTPUT_DICT, help=_(u'retrieve node configuration'))
@@ -34,9 +50,7 @@
     def add_parser_options(self):
         self.parser.add_argument("-k", "--key", type=base.unicode_decoder, action='append', dest='keys',
                                  help=_(u"data key to filter"))
-        self.parser.add_argument("node", type=base.unicode_decoder, help=_(u"node to request"))
-        self.parser.add_argument("service", type=base.unicode_decoder, nargs='?', default=u'',
-                                 help=_(u"JID of the PubSub service (default: request profile own pubsub)"))
+        NodeCommon.add_parser_options(self)
 
     def removePrefix(self, key):
         return key[7:] if key.startswith(u"pubsub#") else key
@@ -63,7 +77,88 @@
             callback=self.psNodeConfigurationGetCb,
             errback=self.psNodeConfigurationGetEb)
 
-class NodeSet(base.CommandBase):
+
+class NodeCreate(base.CommandBase, NodeCommon):
+
+    def __init__(self, host):
+        NodeCommon.__init__(self, node_required=False)
+        base.CommandBase.__init__(self, host, 'create', use_output=C.OUTPUT_DICT, use_verbose=True, help=_(u'create a node'))
+        self.need_loop=True
+
+    def add_parser_options(self):
+        self.parser.add_argument("-f", "--field", type=base.unicode_decoder, action='append', nargs=2, dest='fields',
+                                 required=True, metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set (required)"))
+        self.parser.add_argument("-F", "--full-prefix", action="store_true", help=_(u"don't prepend \"pubsub#\" prefix to field names"))
+        NodeCommon.add_parser_options(self)
+
+    def psNodeCreateCb(self, node_id):
+        if self.host.verbosity:
+            announce = _(u'node created successfully: ')
+        else:
+            announce = u''
+        self.disp(announce + node_id)
+        self.host.quit()
+
+    def psNodeCreateEb(self, failure_):
+        self.disp(u"can't create: {reason}".format(
+            reason=failure_), error=True)
+        self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+
+    def start(self):
+        if not self.args.full_prefix:
+            options = {u'pubsub#' + k: v for k,v in self.args.fields}
+        else:
+            options = dict(self.args.fields)
+        self.host.bridge.psNodeCreate(
+            self.args.service,
+            self.args.node,
+            options,
+            self.profile,
+            callback=self.psNodeCreateCb,
+            errback=partial(self.errback,
+                            msg=_(u"can't create node: {}"),
+                            exit_code=C.EXIT_BRIDGE_ERRBACK))
+
+
+class NodeDelete(base.CommandBase, NodeCommon):
+
+    def __init__(self, host):
+        base.CommandBase.__init__(self, host, 'delete', help=_(u'delete a node'))
+        self.need_loop=True
+
+    def add_parser_options(self):
+        self.parser.add_argument('-f', '--force', action='store_true', help=_(u'delete node without confirmation'))
+        NodeCommon.add_parser_options(self)
+
+    def psNodeDeleteCb(self):
+        self.disp(_(u'node deleted successfully'))
+        self.host.quit()
+
+    def start(self):
+        if not self.args.force:
+            if not self.args.service:
+                message = _(u"Are you sure to delete pep node [{node_id}] ?").format(
+                    node_id=self.args.node)
+            else:
+                message = _(u"Are you sure to delete node [{node_id}] on service [{service}] ?").format(
+                    node_id=self.args.node, service=self.args.service)
+
+            res = raw_input("{} (y/N)? ".format(message))
+            if res not in ("y", "Y"):
+                self.disp(_(u"node deletion cancelled"))
+                self.host.quit(2)
+
+        self.host.bridge.psNodeDelete(
+            self.args.service,
+            self.args.node,
+            self.profile,
+            callback=self.psNodeDeleteCb,
+            errback=partial(self.errback,
+                            msg=_(u"can't delete node: {}"),
+                            exit_code=C.EXIT_BRIDGE_ERRBACK))
+
+
+class NodeSet(base.CommandBase, NodeCommon):
 
     def __init__(self, host):
         base.CommandBase.__init__(self, host, 'set', use_output=C.OUTPUT_DICT, use_verbose=True, help=_(u'set node configuration'))
@@ -72,9 +167,7 @@
     def add_parser_options(self):
         self.parser.add_argument("-f", "--field", type=base.unicode_decoder, action='append', nargs=2, dest='fields',
                                  required=True, metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set (required)"))
-        self.parser.add_argument("node", type=base.unicode_decoder, help=_(u"node to request"))
-        self.parser.add_argument("service", type=base.unicode_decoder, nargs='?', default=u'',
-                                 help=_(u"JID of the PubSub service (default: request profile own pubsub)"))
+        NodeCommon.add_parser_options(self)
 
     def psNodeConfigurationSetCb(self):
         self.disp(_(u'node configuration successful'), 1)
@@ -101,16 +194,14 @@
             errback=self.psNodeConfigurationSetEb)
 
 
-class NodeAffiliationsGet(base.CommandBase):
+class NodeAffiliationsGet(base.CommandBase, NodeCommon):
 
     def __init__(self, host):
         base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_DICT, help=_(u'retrieve node affiliations (for node owner)'))
         self.need_loop=True
 
     def add_parser_options(self):
-        self.parser.add_argument("-s", "--service", type=base.unicode_decoder, default=u'',
-                                 help=_(u"JID of the PubSub service (default: request profile own pubsub)"))
-        self.parser.add_argument("node", type=base.unicode_decoder, help=_(u"node to request"))
+        NodeCommon.add_parser_options(self)
 
     def psNodeAffiliationsGetCb(self, affiliations):
         self.output(affiliations)
@@ -130,16 +221,14 @@
             errback=self.psNodeAffiliationsGetEb)
 
 
-class NodeAffiliationsSet(base.CommandBase):
+class NodeAffiliationsSet(base.CommandBase, NodeCommon):
 
     def __init__(self, host):
         base.CommandBase.__init__(self, host, 'set', use_verbose=True, help=_(u'set affiliations (for node owner)'))
         self.need_loop=True
 
     def add_parser_options(self):
-        self.parser.add_argument("-s", "--service", type=base.unicode_decoder, default=u'',
-                                 help=_(u"JID of the PubSub service (default: request profile own pubsub)"))
-        self.parser.add_argument("node", type=base.unicode_decoder, help=_(u"node to set"))
+        NodeCommon.add_parser_options(self)
         # XXX: we use optional argument syntax for a required one because list of list of 2 elements
         #      (uses to construct dicts) don't work with positional arguments
         self.parser.add_argument("-a",
@@ -180,7 +269,7 @@
 
 
 class Node(base.CommandBase):
-    subcommands = (NodeInfo, NodeSet, NodeAffiliations)
+    subcommands = (NodeInfo, NodeCreate, NodeDelete, NodeSet, NodeAffiliations)
 
     def __init__(self, host):
         super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling'))