# HG changeset patch # User Goffi # Date 1503218013 -7200 # Node ID d94e932be8b39424da3080254b1a44e386e13daf # Parent b1bbd2994cebd1e754e443befa4bc9df8186291b jp (pubsub/node): added subscriptions subcommand: added subscriptions commands for node owner, with get/set subcommands (work in a similar way as affiliations) diff -r b1bbd2994ceb -r d94e932be8b3 frontends/src/jp/cmd_pubsub.py --- a/frontends/src/jp/cmd_pubsub.py Sun Aug 20 10:31:53 2017 +0200 +++ b/frontends/src/jp/cmd_pubsub.py Sun Aug 20 10:33:33 2017 +0200 @@ -37,6 +37,7 @@ __commands__ = ["Pubsub"] PUBSUB_TMP_DIR = u"pubsub" +ALLOWED_SUBSCRIPTIONS_OWNER = ('subscribed', 'pending', 'none') # TODO: need to split this class in several modules, plugin should handle subcommands @@ -266,8 +267,103 @@ super(NodeAffiliations, self).__init__(host, 'affiliations', use_profile=False, help=_(u'set or retrieve node affiliations')) +class NodeSubscriptionsGet(base.CommandBase): + + def __init__(self, host): + base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_DICT, use_pubsub_node_req=True, help=_(u'retrieve node subscriptions (for node owner)')) + self.need_loop=True + + def add_parser_options(self): + pass + + def psNodeSubscriptionsGetCb(self, subscriptions): + self.output(subscriptions) + self.host.quit() + + def psNodeSubscriptionsGetEb(self, failure_): + self.disp(u"can't get node subscriptions: {reason}".format( + reason=failure_), error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + + def start(self): + common.checkURI(self.args) + self.host.bridge.psNodeSubscriptionsGet( + self.args.service, + self.args.node, + self.profile, + callback=self.psNodeSubscriptionsGetCb, + errback=self.psNodeSubscriptionsGetEb) + + +class StoreSubscriptionAction(argparse.Action): + """Action which handle subscription parameter for owner + + list is given by pairs: jid and subscription state + if subscription state is not specified, it default to "subscribed" + """ + + def __call__(self, parser, namespace, values, option_string): + dest_dict = getattr(namespace, self.dest) + while values: + jid_s = values.pop(0) + try: + subscription = values.pop(0) + except IndexError: + subscription = 'subscribed' + if subscription not in ALLOWED_SUBSCRIPTIONS_OWNER: + parser.error(_(u"subscription must be one of {}").format(u', '.join(ALLOWED_SUBSCRIPTIONS_OWNER))) + dest_dict[jid_s] = subscription + + +class NodeSubscriptionsSet(base.CommandBase): + + def __init__(self, host): + base.CommandBase.__init__(self, host, 'set', use_pubsub_node_req=True, use_verbose=True, help=_(u'set/modify subscriptions (for node owner)')) + self.need_loop=True + + def 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("-S", + "--subscription", + dest="subscriptions", + default={}, + nargs='+', + metavar=('JID [SUSBSCRIPTION]'), + required=True, + type=base.unicode_decoder, + action=StoreSubscriptionAction, + help=_(u"entity/subscription couple(s)")) + + def psNodeSubscriptionsSetCb(self): + self.disp(_(u"subscriptions have been set"), 1) + self.host.quit() + + def psNodeSubscriptionsSetEb(self, failure_): + self.disp(u"can't set node subscriptions: {reason}".format( + reason=failure_), error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + + def start(self): + common.checkURI(self.args) + self.host.bridge.psNodeSubscriptionsSet( + self.args.service, + self.args.node, + self.args.subscriptions, + self.profile, + callback=self.psNodeSubscriptionsSetCb, + errback=self.psNodeSubscriptionsSetEb) + + +class NodeSubscriptions(base.CommandBase): + subcommands = (NodeSubscriptionsGet, NodeSubscriptionsSet) + + def __init__(self, host): + super(NodeSubscriptions, self).__init__(host, 'subscriptions', use_profile=False, help=_(u'get or modify node subscriptions')) + + class Node(base.CommandBase): - subcommands = (NodeInfo, NodeCreate, NodeDelete, NodeSet, NodeAffiliations) + subcommands = (NodeInfo, NodeCreate, NodeDelete, NodeSet, NodeAffiliations, NodeSubscriptions) def __init__(self, host): super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling'))