comparison 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
comparison
equal deleted inserted replaced
2438:a34b4fca16e2 2439:6908fe4c6eca
480 class Node(base.CommandBase): 480 class Node(base.CommandBase):
481 subcommands = (NodeInfo, NodeCreate, NodeDelete, NodeSet, NodeAffiliations, NodeSubscriptions, NodeSchema) 481 subcommands = (NodeInfo, NodeCreate, NodeDelete, NodeSet, NodeAffiliations, NodeSubscriptions, NodeSchema)
482 482
483 def __init__(self, host): 483 def __init__(self, host):
484 super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling')) 484 super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling'))
485
486
487 class Set(base.CommandBase):
488
489 def __init__(self, host):
490 base.CommandBase.__init__(self, host, 'set', use_pubsub_node_req=True, help=_(u'publish a new item or update an existing one'))
491 self.need_loop=True
492
493 def add_parser_options(self):
494 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"))
495
496 def psItemsSendCb(self, published_id):
497 if published_id:
498 self.disp(u"Item published at {pub_id}".format(pub_id=published_id))
499 else:
500 self.disp(u"Item published")
501 self.host.quit(C.EXIT_OK)
502
503 def start(self):
504 try:
505 from lxml import etree
506 except ImportError:
507 self.disp(u"lxml module must be installed to use edit, please install it with \"pip install lxml\"", error=True)
508 self.host.quit(1)
509 common.checkURI(self.args)
510 try:
511 element = etree.parse(sys.stdin).getroot()
512 except Exception as e:
513 self.parser.error(_(u"Can't parse the payload XML in input: {msg}").format(msg=e))
514 if element.tag in ('item', '{http://jabber.org/protocol/pubsub}item'):
515 if len(element) > 1:
516 self.parser.error(_(u"<item> can only have one child element (the payload)"))
517 element = element[0]
518 payload = etree.tostring(element, encoding='unicode')
519
520 self.host.bridge.psItemSend(self.args.service,
521 self.args.node,
522 payload,
523 self.args.item,
524 {},
525 self.profile,
526 callback=self.psItemsSendCb,
527 errback=partial(self.errback,
528 msg=_(u"can't send item: {}"),
529 exit_code=C.EXIT_BRIDGE_ERRBACK))
485 530
486 531
487 class Get(base.CommandBase): 532 class Get(base.CommandBase):
488 533
489 def __init__(self, host): 534 def __init__(self, host):
1170 def __init__(self, host): 1215 def __init__(self, host):
1171 super(Hook, self).__init__(host, 'hook', use_profile=False, use_verbose=True, help=_('trigger action on Pubsub notifications')) 1216 super(Hook, self).__init__(host, 'hook', use_profile=False, use_verbose=True, help=_('trigger action on Pubsub notifications'))
1172 1217
1173 1218
1174 class Pubsub(base.CommandBase): 1219 class Pubsub(base.CommandBase):
1175 subcommands = (Get, Delete, Edit, Subscribe, Unsubscribe, Subscriptions, Node, Affiliations, Search, Hook, Uri) 1220 subcommands = (Set, Get, Delete, Edit, Subscribe, Unsubscribe, Subscriptions, Node, Affiliations, Search, Hook, Uri)
1176 1221
1177 def __init__(self, host): 1222 def __init__(self, host):
1178 super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management')) 1223 super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management'))