comparison frontends/src/jp/base.py @ 2189:a25a256688e2

jp (base): output can now specify a default when registering
author Goffi <goffi@goffi.org>
date Sun, 12 Mar 2017 23:33:26 +0100
parents 052d560d0dce
children 33b82250eadd
comparison
equal deleted inserted replaced
2188:052d560d0dce 2189:a25a256688e2
181 181
182 # outputs 182 # outputs
183 self._outputs = {} 183 self._outputs = {}
184 for type_ in C.OUTPUT_TYPES: 184 for type_ in C.OUTPUT_TYPES:
185 self._outputs[type_] = OrderedDict() 185 self._outputs[type_] = OrderedDict()
186 self.default_output = {}
186 187
187 def _bridgeEb(self, failure): 188 def _bridgeEb(self, failure):
188 if isinstance(failure, exceptions.BridgeExceptionNoService): 189 if isinstance(failure, exceptions.BridgeExceptionNoService):
189 print(_(u"Can't connect to SàT backend, are you sure it's launched ?")) 190 print(_(u"Can't connect to SàT backend, are you sure it's launched ?"))
190 elif isinstance(failure, exceptions.BridgeInitError): 191 elif isinstance(failure, exceptions.BridgeInitError):
309 verbose_parent.add_argument('--verbose', '-v', action='count', default=0, help=_(u"Add a verbosity level (can be used multiple times)")) 310 verbose_parent.add_argument('--verbose', '-v', action='count', default=0, help=_(u"Add a verbosity level (can be used multiple times)"))
310 311
311 def add_parser_options(self): 312 def add_parser_options(self):
312 self.parser.add_argument('--version', action='version', version=("%(name)s %(version)s %(copyleft)s" % {'name': PROG_NAME, 'version': self.version, 'copyleft': COPYLEFT})) 313 self.parser.add_argument('--version', action='version', version=("%(name)s %(version)s %(copyleft)s" % {'name': PROG_NAME, 'version': self.version, 'copyleft': COPYLEFT}))
313 314
314 def register_output(self, type_, name, callback, description=""): 315 def register_output(self, type_, name, callback, description="", default=False):
315 if type_ not in C.OUTPUT_TYPES: 316 if type_ not in C.OUTPUT_TYPES:
316 log.error(u"Invalid output type {}".format(type_)) 317 log.error(u"Invalid output type {}".format(type_))
317 return 318 return
318 self._outputs[type_][name] = {'callback': callback, 319 self._outputs[type_][name] = {'callback': callback,
319 'description': description 320 'description': description
320 } 321 }
322 if default:
323 if type_ in self.default_output:
324 self.disp(_(u'there is already a default output for {}, ignoring new one').format(type_))
325 else:
326 self.default_output[type_] = name
327
321 328
322 def parse_output_options(self): 329 def parse_output_options(self):
323 options = self.command.args.output_opts 330 options = self.command.args.output_opts
324 options_dict = {} 331 options_dict = {}
325 for option in options: 332 for option in options:
592 output_parent = argparse.ArgumentParser(add_help=False) 599 output_parent = argparse.ArgumentParser(add_help=False)
593 choices = set(self.host.getOutputChoices(use_output)) 600 choices = set(self.host.getOutputChoices(use_output))
594 choices.update(extra_outputs) 601 choices.update(extra_outputs)
595 if not choices: 602 if not choices:
596 raise exceptions.InternalError("No choice found for {} output type".format(use_output)) 603 raise exceptions.InternalError("No choice found for {} output type".format(use_output))
597 if 'default' in choices: 604 try:
598 default = 'default' 605 default = self.host.default_output[use_output]
599 elif 'simple' in choices: 606 except KeyError:
600 default = 'simple' 607 if u'default' in choices:
601 else: 608 default = u'default'
602 default = choices[0] 609 elif u'simple' in choices:
610 default = u'simple'
611 else:
612 default = list(choices)[0]
603 output_parent.add_argument('--output', '-O', choices=sorted(choices), default=default, help=_(u"select output format (default: {})".format(default))) 613 output_parent.add_argument('--output', '-O', choices=sorted(choices), default=default, help=_(u"select output format (default: {})".format(default)))
604 output_parent.add_argument('--output-option', '--oo', type=unicode_decoder, action="append", dest='output_opts', default=[], help=_(u"output specific option")) 614 output_parent.add_argument('--output-option', '--oo', type=unicode_decoder, action="append", dest='output_opts', default=[], help=_(u"output specific option"))
605 parents.add(output_parent) 615 parents.add(output_parent)
606 else: 616 else:
607 assert extra_outputs is None 617 assert extra_outputs is None