weboob-config now supports new Value* classes instead of BackendConfig ones

This commit is contained in:
Romain Bignon 2010-10-26 20:59:49 +02:00
commit 8777e6c1d3
2 changed files with 47 additions and 32 deletions

View file

@ -73,7 +73,7 @@ class WeboobCfg(ReplApplication):
row = OrderedDict([('Instance name', instance_name), row = OrderedDict([('Instance name', instance_name),
('Backend', name), ('Backend', name),
('Configuration', ', '.join( ('Configuration', ', '.join(
'%s=%s' % (key, ('*****' if key in backend.config and backend.config[key].is_masked \ '%s=%s' % (key, ('*****' if key in backend.config and backend.config[key].masked \
else value)) \ else value)) \
for key, value in params.iteritems())), for key, value in params.iteritems())),
]) ])
@ -145,7 +145,7 @@ class WeboobCfg(ReplApplication):
print '| Capabilities | %s' % ', '.join([cap.__name__ for cap in backend.iter_caps()]) print '| Capabilities | %s' % ', '.join([cap.__name__ for cap in backend.iter_caps()])
first = True first = True
for key, field in backend.config.iteritems(): for key, field in backend.config.iteritems():
value = field.description value = field.label
if not field.default is None: if not field.default is None:
value += ' (default: %s)' % field.default value += ' (default: %s)' % field.default
if first: if first:

View file

@ -24,14 +24,15 @@ import getpass
import logging import logging
from optparse import OptionGroup from optparse import OptionGroup
import os import os
import re
import sys import sys
from copy import deepcopy
from weboob.capabilities.base import FieldNotFound from weboob.capabilities.base import FieldNotFound
from weboob.core import CallErrors from weboob.core import CallErrors
from weboob.core.backendscfg import BackendsConfig, BackendAlreadyExists from weboob.core.backendscfg import BackendsConfig, BackendAlreadyExists
from weboob.tools.misc import iter_fields from weboob.tools.misc import iter_fields
from weboob.tools.browser import BrowserUnavailable, BrowserIncorrectPassword from weboob.tools.browser import BrowserUnavailable, BrowserIncorrectPassword
from weboob.tools.value import Value, ValueBool, ValueFloat, ValueInt
from .base import BackendNotFound, BaseApplication from .base import BackendNotFound, BaseApplication
from .formatters.load import FormattersLoader, FormatterLoadError from .formatters.load import FormattersLoader, FormatterLoadError
@ -181,11 +182,7 @@ class ReplApplication(Cmd, BaseApplication):
print 'Configuration of backend' print 'Configuration of backend'
print '------------------------' print '------------------------'
if key not in params or edit: if key not in params or edit:
params[key] = self.ask(' [%s] %s' % (key, value.description), params[key] = self.ask(value, default=params[key] if (key in params) else value.default)
default=params[key] if (edit and key in params) else value.default,
masked=value.is_masked,
choices=value.choices,
regexp=value.regexp)
else: else:
print ' [%s] %s: %s' % (key, value.description, '(masked)' if value.is_masked else params[key]) print ' [%s] %s: %s' % (key, value.description, '(masked)' if value.is_masked else params[key])
if asked_config: if asked_config:
@ -838,43 +835,61 @@ class ReplApplication(Cmd, BaseApplication):
@param default optional default value (str) @param default optional default value (str)
@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)
@return entered text by user (str) @return entered text by user (str)
""" """
is_bool = False if isinstance(question, Value):
v = deepcopy(question)
if choices: if default:
question = u'%s (%s)' % (question, '/'.join( v.default = default
[s for s in (choices.iterkeys() if isinstance(choices, dict) else choices)])) if masked:
if default is not None: v.masked = masked
if regexp:
v.regexp = regexp
if choices:
v.choices = choices
else:
if isinstance(default, bool): if isinstance(default, bool):
question = u'%s (%s/%s)' % (question, 'Y' if default else 'y', 'n' if default else 'N') klass = ValueBool
choices = ('y', 'n', 'Y', 'N') elif isinstance(default, float):
default = 'y' if default else 'n' klass = ValueFloat
is_bool = True elif isinstance(default, (int,long)):
klass = ValueInt
else: else:
question = u'%s [%s]' % (question, default) klass = Value
if masked: v = klass(label=question, default=default, masked=masked, regexp=regexp, choices=choices)
question = v.label
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:
question = u'%s (%s)' % (question, '/'.join([(s.upper() if s == v.default else v) for s in (v.choices.iterkeys())]))
elif default is not None:
question = u'%s [%s]' % (question, v.default)
if v.masked:
question = u'%s (hidden input)' % question question = u'%s (hidden input)' % question
question += ': ' question += ': '
correct = False while True:
while not correct: line = getpass.getpass(question) if v.masked else raw_input(question)
line = getpass.getpass(question) if masked else raw_input(question) if not line and v.default is not None:
if not line and default is not None: line = v.default
line = default
if isinstance(line, str): if isinstance(line, str):
line = line.decode('utf-8') line = line.decode('utf-8')
correct = (not regexp or re.match(unicode(regexp), unicode(line))) and \
(not choices or unicode(line) in
[unicode(s) for s in (choices.iterkeys() if isinstance(choices, dict) else choices)])
if is_bool: try:
return line.lower() == 'y' v.set_value(line)
else: except ValueError, e:
return line print 'Error: %s' % e
else:
break
return v.value
# formatting related methods # formatting related methods