jp (pubsub): added new "set" command tu publish or update an item in a non-interactive way (while "edit" do it interactively).

This commit is contained in:
Goffi 2017-11-18 18:37:21 +01:00
commit b79a309250

View file

@ -484,6 +484,51 @@ class Node(base.CommandBase):
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 Hook(base.CommandBase):
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'))