Mercurial > libervia-backend
changeset 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 | 8fa7edd0da24 |
children | c7a72b75232b |
files | frontends/src/jp/cmd_pubsub.py |
diffstat | 1 files changed, 99 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/frontends/src/jp/cmd_pubsub.py Wed Jul 05 15:05:47 2017 +0200 +++ b/frontends/src/jp/cmd_pubsub.py Wed Jul 05 15:05:49 2017 +0200 @@ -25,11 +25,14 @@ from functools import partial from sat.tools.common import uri from sat_frontends.tools import jid +import os.path __commands__ = ["Pubsub"] PUBSUB_TMP_DIR = u"pubsub" +# TODO: need to split this class in several modules, plugin should handle subcommands + class NodeInfo(base.CommandBase): @@ -448,8 +451,103 @@ self.display_uri(None) +class HookCreate(base.CommandBase): + + def __init__(self, host): + base.CommandBase.__init__(self, host, 'create', use_pubsub_node_req=True, help=_(u'create a Pubsub hook')) + self.need_loop=True + + def add_parser_options(self): + self.parser.add_argument('-t', '--type', default=u'python', choices=('python', 'python_file', 'python_code'), help=_(u"hook type")) + self.parser.add_argument('-P', '--persistent', action='store_true', help=_(u"make hook persistent across restarts")) + self.parser.add_argument("hook_arg", type=base.unicode_decoder, help=_(u"argument of the hook (depend of the type)")) + + @staticmethod + def checkArgs(self): + if self.args.type == u'python_file': + self.args.hook_arg = os.path.abspath(self.args.hook_arg) + if not os.path.isfile(self.args.hook_arg): + self.parser.error(_(u"{path} is not a file").format(path=self.args.hook_arg)) + + def start(self): + common.checkURI(self.args) + self.checkArgs(self) + self.host.bridge.psHookAdd( + self.args.service, + self.args.node, + self.args.type, + self.args.hook_arg, + self.args.persistent, + self.profile, + callback=self.host.quit, + errback=partial(self.errback, + msg=_(u"can't create hook: {}"), + exit_code=C.EXIT_BRIDGE_ERRBACK)) + + +class HookDelete(base.CommandBase): + + def __init__(self, host): + base.CommandBase.__init__(self, host, 'delete', use_pubsub_node_req=True, help=_(u'delete a Pubsub hook')) + self.need_loop=True + + def add_parser_options(self): + 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)")) + 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)")) + + def psHookRemoveCb(self, nb_deleted): + self.disp(_(u'{nb_deleted} hook(s) have been deleted').format( + nb_deleted = nb_deleted)) + self.host.quit() + + def start(self): + common.checkURI(self.args) + HookCreate.checkArgs(self) + self.host.bridge.psHookRemove( + self.args.service, + self.args.node, + self.args.type, + self.args.hook_arg, + self.profile, + callback=self.psHookRemoveCb, + errback=partial(self.errback, + msg=_(u"can't delete hook: {}"), + exit_code=C.EXIT_BRIDGE_ERRBACK)) + + +class HookList(base.CommandBase): + + def __init__(self, host): + base.CommandBase.__init__(self, host, 'list', use_output=C.OUTPUT_LIST_DICT, help=_(u'list hooks of a profile')) + self.need_loop = True + + def add_parser_options(self): + pass + + def psHookListCb(self, data): + if not data: + self.disp(_(u'No hook found.')) + self.output(data) + self.host.quit() + + def start(self): + self.host.bridge.psHookList( + self.profile, + callback=self.psHookListCb, + errback=partial(self.errback, + msg=_(u"can't list hooks: {}"), + exit_code=C.EXIT_BRIDGE_ERRBACK)) + + +class Hook(base.CommandBase): + subcommands = (HookCreate, HookDelete, HookList) + + def __init__(self, host): + super(Hook, self).__init__(host, 'hook', use_profile=False, help=_('trigger action on Pubsub notifications')) + + class Pubsub(base.CommandBase): - subcommands = (Get, Delete, Edit, Node, Affiliations, Uri) + subcommands = (Get, Delete, Edit, Node, Affiliations, Hook, Uri) def __init__(self, host): super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management'))