diff frontends/src/jp/cmd_pubsub.py @ 2439:6908fe4c6eca

jp (pubsub): added new "set" command tu publish or update an item in a non-interactive way (while "edit" do it interactively).
author Goffi <goffi@goffi.org>
date Sat, 18 Nov 2017 18:37:21 +0100
parents e2cbd449c002
children b8ffb7f8056b
line wrap: on
line diff
--- a/frontends/src/jp/cmd_pubsub.py	Fri Nov 17 10:59:59 2017 +0100
+++ b/frontends/src/jp/cmd_pubsub.py	Sat Nov 18 18:37:21 2017 +0100
@@ -484,6 +484,51 @@
         super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling'))
 
 
+class Set(base.CommandBase):
+
+    def __init__(self, host):
+        base.CommandBase.__init__(self, host, 'set', use_pubsub_node_req=True, help=_(u'publish a new item or update an existing one'))
+        self.need_loop=True
+
+    def add_parser_options(self):
+        self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'', help=_(u"id, URL of the item to update, keyword, or nothing for new item"))
+
+    def psItemsSendCb(self, published_id):
+        if published_id:
+            self.disp(u"Item published at {pub_id}".format(pub_id=published_id))
+        else:
+            self.disp(u"Item published")
+        self.host.quit(C.EXIT_OK)
+
+    def start(self):
+        try:
+            from lxml import etree
+        except ImportError:
+            self.disp(u"lxml module must be installed to use edit, please install it with \"pip install lxml\"", error=True)
+            self.host.quit(1)
+        common.checkURI(self.args)
+        try:
+            element = etree.parse(sys.stdin).getroot()
+        except Exception as e:
+            self.parser.error(_(u"Can't parse the payload XML in input: {msg}").format(msg=e))
+        if element.tag in ('item', '{http://jabber.org/protocol/pubsub}item'):
+            if len(element) > 1:
+                self.parser.error(_(u"<item> can only have one child element (the payload)"))
+            element = element[0]
+        payload = etree.tostring(element, encoding='unicode')
+
+        self.host.bridge.psItemSend(self.args.service,
+                                    self.args.node,
+                                    payload,
+                                    self.args.item,
+                                    {},
+                                    self.profile,
+                                    callback=self.psItemsSendCb,
+                                    errback=partial(self.errback,
+                                                    msg=_(u"can't send item: {}"),
+                                                    exit_code=C.EXIT_BRIDGE_ERRBACK))
+
+
 class Get(base.CommandBase):
 
     def __init__(self, host):
@@ -1172,7 +1217,7 @@
 
 
 class Pubsub(base.CommandBase):
-    subcommands = (Get, Delete, Edit, Subscribe, Unsubscribe, Subscriptions, Node, Affiliations, Search, Hook, Uri)
+    subcommands = (Set, Get, Delete, Edit, Subscribe, Unsubscribe, Subscriptions, Node, Affiliations, Search, Hook, Uri)
 
     def __init__(self, host):
         super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management'))