jp (pubsub): added uri command to build pubsub URI

This commit is contained in:
Goffi 2017-04-16 17:59:54 +02:00
commit f89b014caa

View file

@ -22,6 +22,8 @@ import base
from sat.core.i18n import _
from sat_frontends.jp.constants import Const as C
from functools import partial
from sat.tools.common import uri
from sat_frontends.tools import jid
__commands__ = ["Pubsub"]
@ -87,7 +89,7 @@ class NodeCreate(base.CommandBase, NodeCommon):
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)"))
default={}, metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set (required)"))
self.parser.add_argument("-F", "--full-prefix", action="store_true", help=_(u"don't prepend \"pubsub#\" prefix to field names"))
NodeCommon.add_parser_options(self)
@ -275,7 +277,7 @@ class Node(base.CommandBase):
super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling'))
class Get(base.CommandBase):
class Get(base.CommandBase, NodeCommon):
def __init__(self, host):
base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_LIST_XML, help=_(u'get pubsub item(s)'))
@ -288,9 +290,6 @@ class Get(base.CommandBase):
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("-s", "--service", type=base.unicode_decoder, default=u'',
help=_(u"JID of the PubSub service (default: request profile own pubsub)"))
self.parser.add_argument("node", type=base.unicode_decoder, help=_(u"node to request"))
# TODO: add MAM filters
@ -316,16 +315,15 @@ class Get(base.CommandBase):
errback=self.psItemGetEb)
class Affiliations(base.CommandBase):
class Affiliations(base.CommandBase, NodeCommon):
def __init__(self, host):
NodeCommon.__init__(self, node_required=False)
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)"))
NodeCommon.add_parser_options(self)
def psAffiliationsGetCb(self, affiliations):
self.output(affiliations)
@ -345,8 +343,47 @@ class Affiliations(base.CommandBase):
errback=self.psAffiliationsGetEb)
class Uri(base.CommandBase, NodeCommon):
def __init__(self, host):
base.CommandBase.__init__(self, host, 'uri', use_profile=False, help=_(u'build URI'))
self.need_loop=True
def add_parser_options(self):
NodeCommon.add_parser_options(self)
self.parser.add_argument("-i", "--item", type=base.unicode_decoder, help=_(u"item to link"))
self.parser.add_argument("-p", "--profile", type=base.unicode_decoder, default=C.PROF_KEY_DEFAULT, help=_(u"profile (used when no server is specified)"))
def display_uri(self, jid_):
uri_args = {u'type': 'pubsub'}
if not self.args.service:
self.args.service = jid.JID(jid_).bare
for key in ('node', 'service', 'item'):
value = getattr(self.args, key)
if key == 'service':
key = 'path'
if value:
uri_args[key] = value
self.disp(uri.buildXMPPUri(**uri_args))
self.host.quit()
def start(self):
if not self.args.service:
self.host.bridge.asyncGetParamA(
u'JabberID',
u'Connection',
profile_key=self.args.profile,
callback=self.display_uri,
errback=partial(self.errback,
msg=_(u"can't retrieve jid: {}"),
exit_code=C.EXIT_BRIDGE_ERRBACK))
else:
self.display_uri(None)
class Pubsub(base.CommandBase):
subcommands = (Get, Node, Affiliations)
subcommands = (Get, Node, Affiliations, Uri)
def __init__(self, host):
super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management'))