view frontends/src/jp/cmd_pubsub.py @ 2204:afc703419186

jp (pubsub): added affiliations and node/affiliations/get: affiliations allow to retrieve all node were a profile is affiliated on a service, or the affiliation on a specific node. node/affiliations/get is for node owner and allow to retrieve who is affiliated with a node and which kind of affiliation.
author Goffi <goffi@goffi.org>
date Thu, 23 Mar 2017 21:04:01 +0100
parents ea0d0a4e2ad8
children d662bdd682b2
line wrap: on
line source

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

# jp: a SàT command line tool
# Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import base
from sat.core.i18n import _
from sat_frontends.jp.constants import Const as C

__commands__ = ["Pubsub"]


class NodeInfo(base.CommandBase):

    def __init__(self, host):
        base.CommandBase.__init__(self, host, 'info', use_output=C.OUTPUT_DICT, help=_(u'retrieve node configuration'))
        self.need_loop=True

    def add_parser_options(self):
        self.parser.add_argument("-k", "--key", type=base.unicode_decoder, action='append', dest='keys',
                                 help=_(u"data key to filter"))
        self.parser.add_argument("node", type=base.unicode_decoder, help=_(u"node to request"))
        self.parser.add_argument("service", type=base.unicode_decoder, nargs='?', default=u'',
                                 help=_(u"JID of the PubSub service (default: request profile own pubsub)"))

    def removePrefix(self, key):
        return key[7:] if key.startswith(u"pubsub#") else key

    def filterKey(self, key):
        return any((key == k or key == u'pubsub#' + k) for k in self.args.keys)

    def psNodeConfigurationGetCb(self, config_dict):
        key_filter = (lambda k: True) if not self.args.keys else self.filterKey
        config_dict = {self.removePrefix(k):v for k,v in config_dict.iteritems() if key_filter(k)}
        self.output(config_dict)
        self.host.quit()

    def psNodeConfigurationGetEb(self, failure_):
        self.disp(u"can't get node configuration: {reason}".format(
            reason=failure_), error=True)
        self.host.quit(C.EXIT_BRIDGE_ERRBACK)

    def start(self):
        self.host.bridge.psNodeConfigurationGet(
            self.args.service,
            self.args.node,
            self.profile,
            callback=self.psNodeConfigurationGetCb,
            errback=self.psNodeConfigurationGetEb)

class NodeSet(base.CommandBase):

    def __init__(self, host):
        base.CommandBase.__init__(self, host, 'set', use_output=C.OUTPUT_DICT, use_verbose=True, help=_(u'set node configuration'))
        self.need_loop=True

    def add_parser_options(self):
        self.parser.add_argument("-f", "--field", type=base.unicode_decoder, action='append', nargs=2, dest='fields',
                                 required=True, metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set (required)"))
        self.parser.add_argument("node", type=base.unicode_decoder, help=_(u"node to request"))
        self.parser.add_argument("service", type=base.unicode_decoder, nargs='?', default=u'',
                                 help=_(u"JID of the PubSub service (default: request profile own pubsub)"))

    def psNodeConfigurationSetCb(self):
        self.disp(_(u'node configuration successful'), 1)
        self.host.quit()

    def psNodeConfigurationSetEb(self, failure_):
        self.disp(u"can't set node configuration: {reason}".format(
            reason=failure_), error=True)
        self.host.quit(C.EXIT_BRIDGE_ERRBACK)

    def getKeyName(self, k):
        if not k.startswith(u'pubsub#'):
            return u'pubsub#' + k
        else:
            return k

    def start(self):
        self.host.bridge.psNodeConfigurationSet(
            self.args.service,
            self.args.node,
            {self.getKeyName(k): v for k,v in self.args.fields},
            self.profile,
            callback=self.psNodeConfigurationSetCb,
            errback=self.psNodeConfigurationSetEb)


class NodeAffiliationsGet(base.CommandBase):

    def __init__(self, host):
        base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_DICT, help=_(u'retrieve node affiliations (for node owner)'))
        self.need_loop=True

    def add_parser_options(self):
        self.parser.add_argument("node", type=base.unicode_decoder, help=_(u"node to request"))
        self.parser.add_argument("-s", "--service", type=base.unicode_decoder, default=u'',
                                 help=_(u"JID of the PubSub service (default: request profile own pubsub)"))

    def psNodeAffiliationsGetCb(self, affiliations):
        self.output(affiliations)
        self.host.quit()

    def psNodeAffiliationsGetEb(self, failure_):
        self.disp(u"can't get node affiliations: {reason}".format(
            reason=failure_), error=True)
        self.host.quit(C.EXIT_BRIDGE_ERRBACK)

    def start(self):
        self.host.bridge.psNodeAffiliationsGet(
            self.args.service,
            self.args.node,
            self.profile,
            callback=self.psNodeAffiliationsGetCb,
            errback=self.psNodeAffiliationsGetEb)


class NodeAffiliations(base.CommandBase):
    subcommands = (NodeAffiliationsGet,)

    def __init__(self, host):
        super(NodeAffiliations, self).__init__(host, 'affiliations', use_profile=False, help=_(u'set or retrieve node affiliations'))


class Node(base.CommandBase):
    subcommands = (NodeInfo, NodeSet, NodeAffiliations)

    def __init__(self, host):
        super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling'))


class Get(base.CommandBase):

    def __init__(self, host):
        base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_LIST_XML, help=_(u'get pubsub item(s)'))
        self.need_loop=True

    def add_parser_options(self):
        self.parser.add_argument("-i", "--item", type=base.unicode_decoder, action='append', default=[], dest='items',
                                 help=_(u"item(s) id(s) to get (default: request all items)"))
        self.parser.add_argument("-s", "--sub-id", type=base.unicode_decoder, default=u'',
                                 help=_(u"subscription id"))
        self.parser.add_argument("-m", "--max", type=int, default=10, help=_(u"maximum number of items to get ({} to get all items)".format(C.NO_LIMIT)))
        # TODO: a key(s) argument to select keys to display
        self.parser.add_argument("node", type=base.unicode_decoder, help=_(u"node to request"))
        self.parser.add_argument("service", type=base.unicode_decoder, nargs='?', default=u'',
                                 help=_(u"JID of the PubSub service (default: request profile own pubsub)"))
        # TODO: add MAM filters


    def psItemGetCb(self, ps_result):
        self.output(ps_result[0])
        self.host.quit(C.EXIT_OK)

    def psItemGetEb(self, failure_):
        self.disp(u"can't get pubsub items: {reason}".format(
            reason=failure_), error=True)
        self.host.quit(C.EXIT_BRIDGE_ERRBACK)

    def start(self):
        self.host.bridge.psItemGet(
            self.args.service,
            self.args.node,
            self.args.max,
            self.args.items,
            self.args.sub_id,
            {},
            self.profile,
            callback=self.psItemGetCb,
            errback=self.psItemGetEb)


class Affiliations(base.CommandBase):

    def __init__(self, host):
        base.CommandBase.__init__(self, host, 'affiliations', use_output=C.OUTPUT_DICT, help=_(u'retrieve all affiliations on a service'))
        self.need_loop=True

    def add_parser_options(self):
        self.parser.add_argument("-n", "--node", type=base.unicode_decoder, default=u'', help=_(u"node to request"))
        self.parser.add_argument("-s", "--service", type=base.unicode_decoder, default=u'',
                                 help=_(u"JID of the PubSub service (default: request profile own pubsub)"))

    def psAffiliationsGetCb(self, affiliations):
        self.output(affiliations)
        self.host.quit()

    def psAffiliationsGetEb(self, failure_):
        self.disp(u"can't get node affiliations: {reason}".format(
            reason=failure_), error=True)
        self.host.quit(C.EXIT_BRIDGE_ERRBACK)

    def start(self):
        self.host.bridge.psAffiliationsGet(
            self.args.service,
            self.args.node,
            self.profile,
            callback=self.psAffiliationsGetCb,
            errback=self.psAffiliationsGetEb)


class Pubsub(base.CommandBase):
    subcommands = (Get, Node, Affiliations)

    def __init__(self, host):
        super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management'))