comparison sat_frontends/jp/base.py @ 2764:92af49cde255

jp (base): MAM and RSM arguments can now be used for pubsub commands: - RSM and MAM args arguments have been added to pubsub group, and activated when multiple items are possible - /!\ --max-items (i.e. pubsub original max) short option has been change for -M, and -m is now used for RSM max - pubsub/search -m option has been renamed -M for same reason as above - pubsub/search -o option has been replaced by -k (for keep), to avoid conflict with order-by (and long version is now --node-max)
author Goffi <goffi@goffi.org>
date Fri, 11 Jan 2019 10:18:02 +0100
parents 4b693ea24d5f
children 378188abe941
comparison
equal deleted inserted replaced
2763:c4190d5340ab 2764:92af49cde255
368 if default is not None: 368 if default is not None:
369 item_help += _(u" (DEFAULT: {default})".format(default=default)) 369 item_help += _(u" (DEFAULT: {default})".format(default=default))
370 pubsub_group.add_argument("-i", "--item", type=unicode_decoder, help=item_help) 370 pubsub_group.add_argument("-i", "--item", type=unicode_decoder, help=item_help)
371 pubsub_group.add_argument("-L", "--last-item", action='store_true', help=_(u'retrieve last item')) 371 pubsub_group.add_argument("-L", "--last-item", action='store_true', help=_(u'retrieve last item'))
372 elif flags.multi_items: 372 elif flags.multi_items:
373 # mutiple items 373 # mutiple items, this activate several features: max-items, RSM, MAM
374 # and Orbder-by
374 pubsub_group.add_argument("-i", "--item", type=unicode_decoder, action='append', dest='items', default=[], help=_(u"items to retrieve (DEFAULT: all)")) 375 pubsub_group.add_argument("-i", "--item", type=unicode_decoder, action='append', dest='items', default=[], help=_(u"items to retrieve (DEFAULT: all)"))
375 if not flags.no_max: 376 if not flags.no_max:
376 pubsub_group.add_argument("-m", "--max-items", dest="max", type=int, default=10,
377 help=_(u"maximum number of items to get ({no_limit} to get all items)".format(no_limit=C.NO_LIMIT)))
378 # TODO: order-by should be a list to handle several levels of ordering
379 # but this is not yet done in SàT (and not really useful with
380 # current specifications, as only "creation" and "modification" are
381 # available)
382 pubsub_group.add_argument( 377 pubsub_group.add_argument(
383 "-o", "--order-by", choices=[C.ORDER_BY_CREATION, 378 "-M", "--max-items", dest="max", type=int, default=10,
384 C.ORDER_BY_MODIFICATION], 379 help=_(u"maximum number of items to get ({no_limit} to get all items)"
385 help=_(u"how items should be ordered")) 380 .format(no_limit=C.NO_LIMIT)))
381
382 # RSM
383
384 # FIXME: it could be possible to no duplicate max (between pubsub max-items
385 # and RSM max)should not be duplicated, RSM could be used when
386 # available and pubsub max otherwise
387 pubsub_group.add_argument(
388 "-m", "--max", dest="rsm_max", type=int, default=10,
389 help=_(u"maximum number of items to get per page (DEFAULT: 10)"))
390 rsm_page_group = pubsub_group.add_mutually_exclusive_group()
391 rsm_page_group.add_argument(
392 "-a", "--after", dest="rsm_after", type=unicode_decoder,
393 help=_(u"find page after this item"), metavar='ITEM_ID')
394 rsm_page_group.add_argument(
395 "-b", "--before", dest="rsm_before", type=unicode_decoder,
396 help=_(u"find page before this item"), metavar='ITEM_ID')
397 rsm_page_group.add_argument(
398 "--index", dest="rsm_index", type=int,
399 help=_(u"index of the page to retrieve"))
400
401
402 # MAM
403
404 pubsub_group.add_argument(
405 "-f", "--filter", dest='mam_filters', type=unicode_decoder, nargs=2,
406 action='append', default=[], help=_(u"MAM filters to use"),
407 metavar=(u"FILTER_NAME", u"VALUE")
408 )
409
410 # Order-By
411
412 # TODO: order-by should be a list to handle several levels of ordering
413 # but this is not yet done in SàT (and not really useful with
414 # current specifications, as only "creation" and "modification" are
415 # available)
416 pubsub_group.add_argument(
417 "-o", "--order-by", choices=[C.ORDER_BY_CREATION,
418 C.ORDER_BY_MODIFICATION],
419 help=_(u"how items should be ordered"))
386 420
387 if not flags.all_used: 421 if not flags.all_used:
388 raise exceptions.InternalError('unknown flags: {flags}'.format(flags=u', '.join(flags.unused))) 422 raise exceptions.InternalError('unknown flags: {flags}'.format(flags=u', '.join(flags.unused)))
389 if defaults: 423 if defaults:
390 raise exceptions.InternalError('unused defaults: {defaults}'.format(defaults=defaults)) 424 raise exceptions.InternalError('unused defaults: {defaults}'.format(defaults=defaults))
994 @return (dict): dict which can be used directly in the bridge for pubsub 1028 @return (dict): dict which can be used directly in the bridge for pubsub
995 """ 1029 """
996 if extra is None: 1030 if extra is None:
997 extra = {} 1031 extra = {}
998 else: 1032 else:
999 if {C.KEY_ORDER_BY}.intersection(extra.keys()): 1033 intersection = {C.KEY_ORDER_BY}.intersection(extra.keys())
1000 raise exceptions.InternalError( 1034 if intersection:
1001 u"given extra dict has conflicting keys with pubsub keys") 1035 raise exceptions.ConflictError(
1036 u"given extra dict has conflicting keys with pubsub keys "
1037 u"{intersection}".format(intersection=intersection))
1038
1039 # RSM
1040
1041 for attribute in (u'max', u'after', u'before', 'index'):
1042 key = u'rsm_' + attribute
1043 if key in extra:
1044 raise exceptions.ConflictError(
1045 u"This key already exists in extra: u{key}".format(key=key))
1046 value = getattr(self.args, key, None)
1047 if value is not None:
1048 extra[key] = unicode(value)
1049
1050 # MAM
1051
1052 if hasattr(self.args, u'mam_filters'):
1053 for key, value in self.args.mam_filters:
1054 key = u'filter_' + key
1055 if key in extra:
1056 raise exceptions.ConflictError(
1057 u"This key already exists in extra: u{key}".format(key=key))
1058 extra[key] = value
1059
1060 # Order-By
1061
1002 try: 1062 try:
1003 order_by = self.args.order_by 1063 order_by = self.args.order_by
1004 except AttributeError: 1064 except AttributeError:
1005 pass 1065 pass
1006 else: 1066 else: