Mercurial > libervia-backend
comparison frontends/src/jp/base.py @ 2155:cee6561ad086
jp (core): added extra_outputs:
- extra outputs are output locals to a command which can be used in addition to the generic one which are compatible
- added --output_option argument(s) to specify option specific to selected output
- Jp.command now references the running command
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 16 Feb 2017 00:49:46 +0100 |
parents | fff88c33442f |
children | 62dfa6e02f54 |
comparison
equal
deleted
inserted
replaced
2154:7cbffd754b4a | 2155:cee6561ad086 |
---|---|
253 if error: | 253 if error: |
254 print >>sys.stderr,msg | 254 print >>sys.stderr,msg |
255 else: | 255 else: |
256 print msg | 256 print msg |
257 | 257 |
258 def output(self, type_, name, data): | 258 def output(self, type_, name, extra_outputs, data): |
259 self._outputs[type_][name]['callback'](data) | 259 if name in extra_outputs: |
260 extra_outputs[name](data) | |
261 else: | |
262 self._outputs[type_][name]['callback'](data) | |
260 | 263 |
261 def addOnQuitCallback(self, callback, *args, **kwargs): | 264 def addOnQuitCallback(self, callback, *args, **kwargs): |
262 """Add a callback which will be called on quit command | 265 """Add a callback which will be called on quit command |
263 | 266 |
264 @param callback(callback): method to call | 267 @param callback(callback): method to call |
503 return param_jid | 506 return param_jid |
504 | 507 |
505 | 508 |
506 class CommandBase(object): | 509 class CommandBase(object): |
507 | 510 |
508 def __init__(self, host, name, use_profile=True, use_output=False, | 511 def __init__(self, host, name, use_profile=True, use_output=False, extra_outputs=None, |
509 need_connect=None, help=None, **kwargs): | 512 need_connect=None, help=None, **kwargs): |
510 """Initialise CommandBase | 513 """Initialise CommandBase |
511 | 514 |
512 @param host: Jp instance | 515 @param host: Jp instance |
513 @param name(unicode): name of the new command | 516 @param name(unicode): name of the new command |
514 @param use_profile(bool): if True, add profile selection/connection commands | 517 @param use_profile(bool): if True, add profile selection/connection commands |
515 @param use_output(bool, unicode): if not False, add --output option | 518 @param use_output(bool, unicode): if not False, add --output option |
519 @param extra_outputs(dict): list of command specific outputs: | |
520 key is output name ("default" to use as main output) | |
521 value is a callable which will format the output (data will be used as only argument) | |
522 if a key already exists with normal outputs, the extra one will be used | |
516 @param need_connect(bool, None): True if profile connection is needed | 523 @param need_connect(bool, None): True if profile connection is needed |
517 False else (profile session must still be started) | 524 False else (profile session must still be started) |
518 None to set auto value (i.e. True if use_profile is set) | 525 None to set auto value (i.e. True if use_profile is set) |
519 Can't be set if use_profile is False | 526 Can't be set if use_profile is False |
520 @param help(unicode): help message to display | 527 @param help(unicode): help message to display |
544 # from this point, self.need_connect is None if connection is not needed at all | 551 # from this point, self.need_connect is None if connection is not needed at all |
545 # False if session starting is needed, and True if full connection is needed | 552 # False if session starting is needed, and True if full connection is needed |
546 | 553 |
547 # --output option | 554 # --output option |
548 if use_output: | 555 if use_output: |
556 if extra_outputs is None: | |
557 extra_outputs = {} | |
558 self.extra_outputs = extra_outputs | |
549 if use_output == True: | 559 if use_output == True: |
550 use_output = C.OUTPUT_TEXT | 560 use_output = C.OUTPUT_TEXT |
551 assert use_output in C.OUTPUT_TYPES | 561 assert use_output in C.OUTPUT_TYPES |
552 self._output_type = use_output | 562 self._output_type = use_output |
553 output_parent = argparse.ArgumentParser(add_help=False) | 563 output_parent = argparse.ArgumentParser(add_help=False) |
554 choices = self.host.getOutputChoices(use_output) | 564 choices = set(self.host.getOutputChoices(use_output)) |
565 choices.update(extra_outputs) | |
555 if not choices: | 566 if not choices: |
556 raise exceptions.InternalError("No choice found for {} output type".format(use_output)) | 567 raise exceptions.InternalError("No choice found for {} output type".format(use_output)) |
557 default = 'default' if 'default' in choices else choices[0] | 568 if 'default' in choices: |
558 output_parent.add_argument('--output', '-O', choices=choices, default=default, help=_(u"Select output format")) | 569 default = 'default' |
570 elif 'simple' in choices: | |
571 default = 'simple' | |
572 else: | |
573 default = choices[0] | |
574 output_parent.add_argument('--output', '-O', choices=sorted(choices), default=default, help=_(u"select output format (default: {})".format(default))) | |
575 output_parent.add_argument('--output-option', '--oo', type=unicode_decoder, action="append", dest='output_opts', default=[], help=_(u"output specific option")) | |
559 parents.add(output_parent) | 576 parents.add(output_parent) |
577 else: | |
578 assert extra_outputs is None | |
560 | 579 |
561 # other common options | 580 # other common options |
562 use_opts = {k:v for k,v in kwargs.iteritems() if k.startswith('use_')} | 581 use_opts = {k:v for k,v in kwargs.iteritems() if k.startswith('use_')} |
563 for param, do_use in use_opts.iteritems(): | 582 for param, do_use in use_opts.iteritems(): |
564 opt=param[4:] # if param is use_verbose, opt is verbose | 583 opt=param[4:] # if param is use_verbose, opt is verbose |
693 | 712 |
694 def disp(self, msg, verbosity=0, error=False): | 713 def disp(self, msg, verbosity=0, error=False): |
695 return self.host.disp(msg, verbosity, error) | 714 return self.host.disp(msg, verbosity, error) |
696 | 715 |
697 def output(self, data): | 716 def output(self, data): |
698 return self.host.output(self._output_type, self.args.output, data) | 717 return self.host.output(self._output_type, self.args.output, self.extra_outputs, data) |
699 | 718 |
700 def add_parser_options(self): | 719 def add_parser_options(self): |
701 try: | 720 try: |
702 subcommands = self.subcommands | 721 subcommands = self.subcommands |
703 except AttributeError: | 722 except AttributeError: |
712 """this method is called when a command is actually run | 731 """this method is called when a command is actually run |
713 | 732 |
714 It set stuff like progression callbacks and profile connection | 733 It set stuff like progression callbacks and profile connection |
715 You should not overide this method: you should call self.start instead | 734 You should not overide this method: you should call self.start instead |
716 """ | 735 """ |
736 # we keep a reference to run command, it may be useful e.g. for outputs | |
737 self.host.command = self | |
717 # host._need_loop is set here from our current value and not before | 738 # host._need_loop is set here from our current value and not before |
718 # as the need_loop decision must be taken only by then running command | 739 # as the need_loop decision must be taken only by then running command |
719 self.host._need_loop = self.need_loop | 740 self.host._need_loop = self.need_loop |
720 | 741 |
721 try: | 742 try: |