Mercurial > libervia-backend
comparison frontends/src/jp/cmd_pubsub.py @ 2339:d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
added subscriptions commands for node owner, with get/set subcommands (work in a similar way as affiliations)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 20 Aug 2017 10:33:33 +0200 |
parents | f4a0723042ee |
children | 3c0a3fae1862 |
comparison
equal
deleted
inserted
replaced
2338:b1bbd2994ceb | 2339:d94e932be8b3 |
---|---|
35 import sys | 35 import sys |
36 | 36 |
37 __commands__ = ["Pubsub"] | 37 __commands__ = ["Pubsub"] |
38 | 38 |
39 PUBSUB_TMP_DIR = u"pubsub" | 39 PUBSUB_TMP_DIR = u"pubsub" |
40 ALLOWED_SUBSCRIPTIONS_OWNER = ('subscribed', 'pending', 'none') | |
40 | 41 |
41 # TODO: need to split this class in several modules, plugin should handle subcommands | 42 # TODO: need to split this class in several modules, plugin should handle subcommands |
42 | 43 |
43 | 44 |
44 class NodeInfo(base.CommandBase): | 45 class NodeInfo(base.CommandBase): |
264 | 265 |
265 def __init__(self, host): | 266 def __init__(self, host): |
266 super(NodeAffiliations, self).__init__(host, 'affiliations', use_profile=False, help=_(u'set or retrieve node affiliations')) | 267 super(NodeAffiliations, self).__init__(host, 'affiliations', use_profile=False, help=_(u'set or retrieve node affiliations')) |
267 | 268 |
268 | 269 |
270 class NodeSubscriptionsGet(base.CommandBase): | |
271 | |
272 def __init__(self, host): | |
273 base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_DICT, use_pubsub_node_req=True, help=_(u'retrieve node subscriptions (for node owner)')) | |
274 self.need_loop=True | |
275 | |
276 def add_parser_options(self): | |
277 pass | |
278 | |
279 def psNodeSubscriptionsGetCb(self, subscriptions): | |
280 self.output(subscriptions) | |
281 self.host.quit() | |
282 | |
283 def psNodeSubscriptionsGetEb(self, failure_): | |
284 self.disp(u"can't get node subscriptions: {reason}".format( | |
285 reason=failure_), error=True) | |
286 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
287 | |
288 def start(self): | |
289 common.checkURI(self.args) | |
290 self.host.bridge.psNodeSubscriptionsGet( | |
291 self.args.service, | |
292 self.args.node, | |
293 self.profile, | |
294 callback=self.psNodeSubscriptionsGetCb, | |
295 errback=self.psNodeSubscriptionsGetEb) | |
296 | |
297 | |
298 class StoreSubscriptionAction(argparse.Action): | |
299 """Action which handle subscription parameter for owner | |
300 | |
301 list is given by pairs: jid and subscription state | |
302 if subscription state is not specified, it default to "subscribed" | |
303 """ | |
304 | |
305 def __call__(self, parser, namespace, values, option_string): | |
306 dest_dict = getattr(namespace, self.dest) | |
307 while values: | |
308 jid_s = values.pop(0) | |
309 try: | |
310 subscription = values.pop(0) | |
311 except IndexError: | |
312 subscription = 'subscribed' | |
313 if subscription not in ALLOWED_SUBSCRIPTIONS_OWNER: | |
314 parser.error(_(u"subscription must be one of {}").format(u', '.join(ALLOWED_SUBSCRIPTIONS_OWNER))) | |
315 dest_dict[jid_s] = subscription | |
316 | |
317 | |
318 class NodeSubscriptionsSet(base.CommandBase): | |
319 | |
320 def __init__(self, host): | |
321 base.CommandBase.__init__(self, host, 'set', use_pubsub_node_req=True, use_verbose=True, help=_(u'set/modify subscriptions (for node owner)')) | |
322 self.need_loop=True | |
323 | |
324 def add_parser_options(self): | |
325 # XXX: we use optional argument syntax for a required one because list of list of 2 elements | |
326 # (uses to construct dicts) don't work with positional arguments | |
327 self.parser.add_argument("-S", | |
328 "--subscription", | |
329 dest="subscriptions", | |
330 default={}, | |
331 nargs='+', | |
332 metavar=('JID [SUSBSCRIPTION]'), | |
333 required=True, | |
334 type=base.unicode_decoder, | |
335 action=StoreSubscriptionAction, | |
336 help=_(u"entity/subscription couple(s)")) | |
337 | |
338 def psNodeSubscriptionsSetCb(self): | |
339 self.disp(_(u"subscriptions have been set"), 1) | |
340 self.host.quit() | |
341 | |
342 def psNodeSubscriptionsSetEb(self, failure_): | |
343 self.disp(u"can't set node subscriptions: {reason}".format( | |
344 reason=failure_), error=True) | |
345 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
346 | |
347 def start(self): | |
348 common.checkURI(self.args) | |
349 self.host.bridge.psNodeSubscriptionsSet( | |
350 self.args.service, | |
351 self.args.node, | |
352 self.args.subscriptions, | |
353 self.profile, | |
354 callback=self.psNodeSubscriptionsSetCb, | |
355 errback=self.psNodeSubscriptionsSetEb) | |
356 | |
357 | |
358 class NodeSubscriptions(base.CommandBase): | |
359 subcommands = (NodeSubscriptionsGet, NodeSubscriptionsSet) | |
360 | |
361 def __init__(self, host): | |
362 super(NodeSubscriptions, self).__init__(host, 'subscriptions', use_profile=False, help=_(u'get or modify node subscriptions')) | |
363 | |
364 | |
269 class Node(base.CommandBase): | 365 class Node(base.CommandBase): |
270 subcommands = (NodeInfo, NodeCreate, NodeDelete, NodeSet, NodeAffiliations) | 366 subcommands = (NodeInfo, NodeCreate, NodeDelete, NodeSet, NodeAffiliations, NodeSubscriptions) |
271 | 367 |
272 def __init__(self, host): | 368 def __init__(self, host): |
273 super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling')) | 369 super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling')) |
274 | 370 |
275 | 371 |