From c8b013be53c957ce19bb6fd1ddb71990406a9d18 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Fri, 19 Jul 2013 13:37:50 +0200 Subject: [PATCH] Better handling of "tiny" choices, allow forcing --- weboob/tools/application/console.py | 22 ++++++++++++++-------- weboob/tools/value.py | 2 ++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/weboob/tools/application/console.py b/weboob/tools/application/console.py index 295c7fd1..f9872313 100644 --- a/weboob/tools/application/console.py +++ b/weboob/tools/application/console.py @@ -346,7 +346,7 @@ class ConsoleApplication(BaseApplication): print >>sys.stderr, 'Backend "%s" already exists.' % name return 1 - def ask(self, question, default=None, masked=False, regexp=None, choices=None): + def ask(self, question, default=None, masked=False, regexp=None, choices=None, tiny=None): """ Ask a question to user. @@ -355,6 +355,7 @@ class ConsoleApplication(BaseApplication): @param masked if True, do not show typed text (bool) @param regexp text must match this regexp (str) @param choices choices to do (list) + @param tiny ask for the (small) value of the choice (bool) @return entered text by user (str) """ @@ -368,6 +369,8 @@ class ConsoleApplication(BaseApplication): v.regexp = regexp if choices: v.choices = choices + if tiny: + v.tiny = tiny else: if isinstance(default, bool): klass = ValueBool @@ -378,7 +381,7 @@ class ConsoleApplication(BaseApplication): else: klass = Value - v = klass(label=question, default=default, masked=masked, regexp=regexp, choices=choices) + v = klass(label=question, default=default, masked=masked, regexp=regexp, choices=choices, tiny=tiny) question = v.label if v.id: @@ -388,15 +391,18 @@ class ConsoleApplication(BaseApplication): if isinstance(v, ValueBool): question = u'%s (%s/%s)' % (question, 'Y' if v.default else 'y', 'n' if v.default else 'N') elif v.choices: - tiny = True - for key in v.choices.iterkeys(): - if len(key) > 5 or ' ' in key: - tiny = False - break + if v.tiny is None: + v.tiny = True + for key in v.choices.iterkeys(): + if len(key) > 5 or ' ' in key: + v.tiny = False + break - if tiny: + if v.tiny: question = u'%s (%s)' % (question, '/'.join((s.upper() if s == v.default else s) for s in (v.choices.iterkeys()))) + for key, value in v.choices.iteritems(): + print '%s%s%s: %s' % (self.BOLD, key, self.NC, value) else: for n, (key, value) in enumerate(v.choices.iteritems()): print '%s%2d)%s %s' % (self.BOLD, n + 1, self.NC, value) diff --git a/weboob/tools/value.py b/weboob/tools/value.py index e7a3c1a8..48f71cb3 100644 --- a/weboob/tools/value.py +++ b/weboob/tools/value.py @@ -53,6 +53,7 @@ class Value(object): :param regexp: if specified, on load the specified value is checked against this regexp, and an error is raised if it doesn't match :type regexp: str :param choices: if this parameter is set, the value must be in the list + :param tiny: the value of choices can be entered by an user (as they are small) :type choices: (list,dict) """ @@ -68,6 +69,7 @@ class Value(object): self.choices = kwargs.get('choices', None) if isinstance(self.choices, (list, tuple)): self.choices = dict(((v, v) for v in self.choices)) + self.tiny = kwargs.get('tiny', None) self.masked = kwargs.get('masked', False) self.required = kwargs.get('required', self.default is None) self._value = kwargs.get('value', None)