diff 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
line wrap: on
line diff
--- a/frontends/src/jp/base.py	Sun Mar 12 23:32:43 2017 +0100
+++ b/frontends/src/jp/base.py	Sun Mar 12 23:33:26 2017 +0100
@@ -183,6 +183,7 @@
         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 @@
     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 @@
             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)