jp (base): output can now specify a default when registering

This commit is contained in:
Goffi 2017-03-12 23:33:26 +01:00
commit 4d96b85178

View file

@ -183,6 +183,7 @@ class Jp(object):
self._outputs = {}
for type_ in C.OUTPUT_TYPES:
self._outputs[type_] = OrderedDict()
self.default_output = {}
def _bridgeEb(self, failure):
if isinstance(failure, exceptions.BridgeExceptionNoService):
@ -311,13 +312,19 @@ class Jp(object):
def add_parser_options(self):
self.parser.add_argument('--version', action='version', version=("%(name)s %(version)s %(copyleft)s" % {'name': PROG_NAME, 'version': self.version, 'copyleft': COPYLEFT}))
def register_output(self, type_, name, callback, description=""):
def register_output(self, type_, name, callback, description="", default=False):
if type_ not in C.OUTPUT_TYPES:
log.error(u"Invalid output type {}".format(type_))
return
self._outputs[type_][name] = {'callback': callback,
'description': description
}
if default:
if type_ in self.default_output:
self.disp(_(u'there is already a default output for {}, ignoring new one').format(type_))
else:
self.default_output[type_] = name
def parse_output_options(self):
options = self.command.args.output_opts
@ -594,12 +601,15 @@ class CommandBase(object):
choices.update(extra_outputs)
if not choices:
raise exceptions.InternalError("No choice found for {} output type".format(use_output))
if 'default' in choices:
default = 'default'
elif 'simple' in choices:
default = 'simple'
else:
default = choices[0]
try:
default = self.host.default_output[use_output]
except KeyError:
if u'default' in choices:
default = u'default'
elif u'simple' in choices:
default = u'simple'
else:
default = list(choices)[0]
output_parent.add_argument('--output', '-O', choices=sorted(choices), default=default, help=_(u"select output format (default: {})".format(default)))
output_parent.add_argument('--output-option', '--oo', type=unicode_decoder, action="append", dest='output_opts', default=[], help=_(u"output specific option"))
parents.add(output_parent)