comparison frontends/src/jp/cmd_pubsub.py @ 2224:87fcd4a7c7e4

jp (pubsub): added uri command to build pubsub URI
author Goffi <goffi@goffi.org>
date Sun, 16 Apr 2017 17:59:54 +0200
parents a6c9bc4d1de0
children 4db836386641
comparison
equal deleted inserted replaced
2223:c6c9a97ffebf 2224:87fcd4a7c7e4
20 20
21 import base 21 import base
22 from sat.core.i18n import _ 22 from sat.core.i18n import _
23 from sat_frontends.jp.constants import Const as C 23 from sat_frontends.jp.constants import Const as C
24 from functools import partial 24 from functools import partial
25 from sat.tools.common import uri
26 from sat_frontends.tools import jid
25 27
26 __commands__ = ["Pubsub"] 28 __commands__ = ["Pubsub"]
27 29
28 30
29 class NodeCommon(object): 31 class NodeCommon(object):
85 base.CommandBase.__init__(self, host, 'create', use_output=C.OUTPUT_DICT, use_verbose=True, help=_(u'create a node')) 87 base.CommandBase.__init__(self, host, 'create', use_output=C.OUTPUT_DICT, use_verbose=True, help=_(u'create a node'))
86 self.need_loop=True 88 self.need_loop=True
87 89
88 def add_parser_options(self): 90 def add_parser_options(self):
89 self.parser.add_argument("-f", "--field", type=base.unicode_decoder, action='append', nargs=2, dest='fields', 91 self.parser.add_argument("-f", "--field", type=base.unicode_decoder, action='append', nargs=2, dest='fields',
90 required=True, metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set (required)")) 92 default={}, metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set (required)"))
91 self.parser.add_argument("-F", "--full-prefix", action="store_true", help=_(u"don't prepend \"pubsub#\" prefix to field names")) 93 self.parser.add_argument("-F", "--full-prefix", action="store_true", help=_(u"don't prepend \"pubsub#\" prefix to field names"))
92 NodeCommon.add_parser_options(self) 94 NodeCommon.add_parser_options(self)
93 95
94 def psNodeCreateCb(self, node_id): 96 def psNodeCreateCb(self, node_id):
95 if self.host.verbosity: 97 if self.host.verbosity:
273 275
274 def __init__(self, host): 276 def __init__(self, host):
275 super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling')) 277 super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling'))
276 278
277 279
278 class Get(base.CommandBase): 280 class Get(base.CommandBase, NodeCommon):
279 281
280 def __init__(self, host): 282 def __init__(self, host):
281 base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_LIST_XML, help=_(u'get pubsub item(s)')) 283 base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_LIST_XML, help=_(u'get pubsub item(s)'))
282 self.need_loop=True 284 self.need_loop=True
283 285
286 help=_(u"item(s) id(s) to get (default: request all items)")) 288 help=_(u"item(s) id(s) to get (default: request all items)"))
287 self.parser.add_argument("-S", "--sub-id", type=base.unicode_decoder, default=u'', 289 self.parser.add_argument("-S", "--sub-id", type=base.unicode_decoder, default=u'',
288 help=_(u"subscription id")) 290 help=_(u"subscription id"))
289 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))) 291 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)))
290 # TODO: a key(s) argument to select keys to display 292 # TODO: a key(s) argument to select keys to display
291 self.parser.add_argument("-s", "--service", type=base.unicode_decoder, default=u'',
292 help=_(u"JID of the PubSub service (default: request profile own pubsub)"))
293 self.parser.add_argument("node", type=base.unicode_decoder, help=_(u"node to request"))
294 # TODO: add MAM filters 293 # TODO: add MAM filters
295 294
296 295
297 def psItemGetCb(self, ps_result): 296 def psItemGetCb(self, ps_result):
298 self.output(ps_result[0]) 297 self.output(ps_result[0])
314 self.profile, 313 self.profile,
315 callback=self.psItemGetCb, 314 callback=self.psItemGetCb,
316 errback=self.psItemGetEb) 315 errback=self.psItemGetEb)
317 316
318 317
319 class Affiliations(base.CommandBase): 318 class Affiliations(base.CommandBase, NodeCommon):
320 319
321 def __init__(self, host): 320 def __init__(self, host):
321 NodeCommon.__init__(self, node_required=False)
322 base.CommandBase.__init__(self, host, 'affiliations', use_output=C.OUTPUT_DICT, help=_(u'retrieve all affiliations on a service')) 322 base.CommandBase.__init__(self, host, 'affiliations', use_output=C.OUTPUT_DICT, help=_(u'retrieve all affiliations on a service'))
323 self.need_loop=True 323 self.need_loop=True
324 324
325 def add_parser_options(self): 325 def add_parser_options(self):
326 self.parser.add_argument("-n", "--node", type=base.unicode_decoder, default=u'', help=_(u"node to request")) 326 NodeCommon.add_parser_options(self)
327 self.parser.add_argument("-s", "--service", type=base.unicode_decoder, default=u'',
328 help=_(u"JID of the PubSub service (default: request profile own pubsub)"))
329 327
330 def psAffiliationsGetCb(self, affiliations): 328 def psAffiliationsGetCb(self, affiliations):
331 self.output(affiliations) 329 self.output(affiliations)
332 self.host.quit() 330 self.host.quit()
333 331
343 self.profile, 341 self.profile,
344 callback=self.psAffiliationsGetCb, 342 callback=self.psAffiliationsGetCb,
345 errback=self.psAffiliationsGetEb) 343 errback=self.psAffiliationsGetEb)
346 344
347 345
346 class Uri(base.CommandBase, NodeCommon):
347
348 def __init__(self, host):
349 base.CommandBase.__init__(self, host, 'uri', use_profile=False, help=_(u'build URI'))
350 self.need_loop=True
351
352 def add_parser_options(self):
353 NodeCommon.add_parser_options(self)
354 self.parser.add_argument("-i", "--item", type=base.unicode_decoder, help=_(u"item to link"))
355 self.parser.add_argument("-p", "--profile", type=base.unicode_decoder, default=C.PROF_KEY_DEFAULT, help=_(u"profile (used when no server is specified)"))
356
357 def display_uri(self, jid_):
358 uri_args = {u'type': 'pubsub'}
359 if not self.args.service:
360 self.args.service = jid.JID(jid_).bare
361
362 for key in ('node', 'service', 'item'):
363 value = getattr(self.args, key)
364 if key == 'service':
365 key = 'path'
366 if value:
367 uri_args[key] = value
368 self.disp(uri.buildXMPPUri(**uri_args))
369 self.host.quit()
370
371 def start(self):
372 if not self.args.service:
373 self.host.bridge.asyncGetParamA(
374 u'JabberID',
375 u'Connection',
376 profile_key=self.args.profile,
377 callback=self.display_uri,
378 errback=partial(self.errback,
379 msg=_(u"can't retrieve jid: {}"),
380 exit_code=C.EXIT_BRIDGE_ERRBACK))
381 else:
382 self.display_uri(None)
383
384
348 class Pubsub(base.CommandBase): 385 class Pubsub(base.CommandBase):
349 subcommands = (Get, Node, Affiliations) 386 subcommands = (Get, Node, Affiliations, Uri)
350 387
351 def __init__(self, host): 388 def __init__(self, host):
352 super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management')) 389 super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management'))