Better handling of "tiny" choices, allow forcing

This commit is contained in:
Laurent Bachelier 2013-07-19 13:37:50 +02:00 committed by Romain Bignon
commit c8b013be53
2 changed files with 17 additions and 9 deletions

View file

@ -346,7 +346,7 @@ class ConsoleApplication(BaseApplication):
print >>sys.stderr, 'Backend "%s" already exists.' % name print >>sys.stderr, 'Backend "%s" already exists.' % name
return 1 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. Ask a question to user.
@ -355,6 +355,7 @@ class ConsoleApplication(BaseApplication):
@param masked if True, do not show typed text (bool) @param masked if True, do not show typed text (bool)
@param regexp text must match this regexp (str) @param regexp text must match this regexp (str)
@param choices choices to do (list) @param choices choices to do (list)
@param tiny ask for the (small) value of the choice (bool)
@return entered text by user (str) @return entered text by user (str)
""" """
@ -368,6 +369,8 @@ class ConsoleApplication(BaseApplication):
v.regexp = regexp v.regexp = regexp
if choices: if choices:
v.choices = choices v.choices = choices
if tiny:
v.tiny = tiny
else: else:
if isinstance(default, bool): if isinstance(default, bool):
klass = ValueBool klass = ValueBool
@ -378,7 +381,7 @@ class ConsoleApplication(BaseApplication):
else: else:
klass = Value 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 question = v.label
if v.id: if v.id:
@ -388,15 +391,18 @@ class ConsoleApplication(BaseApplication):
if isinstance(v, ValueBool): if isinstance(v, ValueBool):
question = u'%s (%s/%s)' % (question, 'Y' if v.default else 'y', 'n' if v.default else 'N') question = u'%s (%s/%s)' % (question, 'Y' if v.default else 'y', 'n' if v.default else 'N')
elif v.choices: elif v.choices:
tiny = True if v.tiny is None:
for key in v.choices.iterkeys(): v.tiny = True
if len(key) > 5 or ' ' in key: for key in v.choices.iterkeys():
tiny = False if len(key) > 5 or ' ' in key:
break v.tiny = False
break
if tiny: if v.tiny:
question = u'%s (%s)' % (question, '/'.join((s.upper() if s == v.default else s) question = u'%s (%s)' % (question, '/'.join((s.upper() if s == v.default else s)
for s in (v.choices.iterkeys()))) 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: else:
for n, (key, value) in enumerate(v.choices.iteritems()): for n, (key, value) in enumerate(v.choices.iteritems()):
print '%s%2d)%s %s' % (self.BOLD, n + 1, self.NC, value) print '%s%2d)%s %s' % (self.BOLD, n + 1, self.NC, value)

View file

@ -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 :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 :type regexp: str
:param choices: if this parameter is set, the value must be in the list :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) :type choices: (list,dict)
""" """
@ -68,6 +69,7 @@ class Value(object):
self.choices = kwargs.get('choices', None) self.choices = kwargs.get('choices', None)
if isinstance(self.choices, (list, tuple)): if isinstance(self.choices, (list, tuple)):
self.choices = dict(((v, v) for v in self.choices)) self.choices = dict(((v, v) for v in self.choices))
self.tiny = kwargs.get('tiny', None)
self.masked = kwargs.get('masked', False) self.masked = kwargs.get('masked', False)
self.required = kwargs.get('required', self.default is None) self.required = kwargs.get('required', self.default is None)
self._value = kwargs.get('value', None) self._value = kwargs.get('value', None)