comparison frontends/src/jp/cmd_pubsub.py @ 2308:0b21d87c91cf

jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
author Goffi <goffi@goffi.org>
date Wed, 05 Jul 2017 15:05:49 +0200
parents bd4d8c73b1d3
children 7b448ac50a69
comparison
equal deleted inserted replaced
2307:8fa7edd0da24 2308:0b21d87c91cf
23 from sat_frontends.jp.constants import Const as C 23 from sat_frontends.jp.constants import Const as C
24 from sat_frontends.jp import common 24 from sat_frontends.jp import common
25 from functools import partial 25 from functools import partial
26 from sat.tools.common import uri 26 from sat.tools.common import uri
27 from sat_frontends.tools import jid 27 from sat_frontends.tools import jid
28 import os.path
28 29
29 __commands__ = ["Pubsub"] 30 __commands__ = ["Pubsub"]
30 31
31 PUBSUB_TMP_DIR = u"pubsub" 32 PUBSUB_TMP_DIR = u"pubsub"
33
34 # TODO: need to split this class in several modules, plugin should handle subcommands
32 35
33 36
34 class NodeInfo(base.CommandBase): 37 class NodeInfo(base.CommandBase):
35 38
36 def __init__(self, host): 39 def __init__(self, host):
446 exit_code=C.EXIT_BRIDGE_ERRBACK)) 449 exit_code=C.EXIT_BRIDGE_ERRBACK))
447 else: 450 else:
448 self.display_uri(None) 451 self.display_uri(None)
449 452
450 453
454 class HookCreate(base.CommandBase):
455
456 def __init__(self, host):
457 base.CommandBase.__init__(self, host, 'create', use_pubsub_node_req=True, help=_(u'create a Pubsub hook'))
458 self.need_loop=True
459
460 def add_parser_options(self):
461 self.parser.add_argument('-t', '--type', default=u'python', choices=('python', 'python_file', 'python_code'), help=_(u"hook type"))
462 self.parser.add_argument('-P', '--persistent', action='store_true', help=_(u"make hook persistent across restarts"))
463 self.parser.add_argument("hook_arg", type=base.unicode_decoder, help=_(u"argument of the hook (depend of the type)"))
464
465 @staticmethod
466 def checkArgs(self):
467 if self.args.type == u'python_file':
468 self.args.hook_arg = os.path.abspath(self.args.hook_arg)
469 if not os.path.isfile(self.args.hook_arg):
470 self.parser.error(_(u"{path} is not a file").format(path=self.args.hook_arg))
471
472 def start(self):
473 common.checkURI(self.args)
474 self.checkArgs(self)
475 self.host.bridge.psHookAdd(
476 self.args.service,
477 self.args.node,
478 self.args.type,
479 self.args.hook_arg,
480 self.args.persistent,
481 self.profile,
482 callback=self.host.quit,
483 errback=partial(self.errback,
484 msg=_(u"can't create hook: {}"),
485 exit_code=C.EXIT_BRIDGE_ERRBACK))
486
487
488 class HookDelete(base.CommandBase):
489
490 def __init__(self, host):
491 base.CommandBase.__init__(self, host, 'delete', use_pubsub_node_req=True, help=_(u'delete a Pubsub hook'))
492 self.need_loop=True
493
494 def add_parser_options(self):
495 self.parser.add_argument('-t', '--type', default=u'', choices=('', 'python', 'python_file', 'python_code'), help=_(u"hook type to remove, empty to remove all (DEFAULT: remove all)"))
496 self.parser.add_argument('-a', '--arg', dest='hook_arg', type=base.unicode_decoder, default=u'', help=_(u"argument of the hook to remove, empty to remove all (DEFAULT: remove all)"))
497
498 def psHookRemoveCb(self, nb_deleted):
499 self.disp(_(u'{nb_deleted} hook(s) have been deleted').format(
500 nb_deleted = nb_deleted))
501 self.host.quit()
502
503 def start(self):
504 common.checkURI(self.args)
505 HookCreate.checkArgs(self)
506 self.host.bridge.psHookRemove(
507 self.args.service,
508 self.args.node,
509 self.args.type,
510 self.args.hook_arg,
511 self.profile,
512 callback=self.psHookRemoveCb,
513 errback=partial(self.errback,
514 msg=_(u"can't delete hook: {}"),
515 exit_code=C.EXIT_BRIDGE_ERRBACK))
516
517
518 class HookList(base.CommandBase):
519
520 def __init__(self, host):
521 base.CommandBase.__init__(self, host, 'list', use_output=C.OUTPUT_LIST_DICT, help=_(u'list hooks of a profile'))
522 self.need_loop = True
523
524 def add_parser_options(self):
525 pass
526
527 def psHookListCb(self, data):
528 if not data:
529 self.disp(_(u'No hook found.'))
530 self.output(data)
531 self.host.quit()
532
533 def start(self):
534 self.host.bridge.psHookList(
535 self.profile,
536 callback=self.psHookListCb,
537 errback=partial(self.errback,
538 msg=_(u"can't list hooks: {}"),
539 exit_code=C.EXIT_BRIDGE_ERRBACK))
540
541
542 class Hook(base.CommandBase):
543 subcommands = (HookCreate, HookDelete, HookList)
544
545 def __init__(self, host):
546 super(Hook, self).__init__(host, 'hook', use_profile=False, help=_('trigger action on Pubsub notifications'))
547
548
451 class Pubsub(base.CommandBase): 549 class Pubsub(base.CommandBase):
452 subcommands = (Get, Delete, Edit, Node, Affiliations, Uri) 550 subcommands = (Get, Delete, Edit, Node, Affiliations, Hook, Uri)
453 551
454 def __init__(self, host): 552 def __init__(self, host):
455 super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management')) 553 super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management'))