prompt (beautifully) user during configuration of a backend

This commit is contained in:
Romain Bignon 2014-01-23 23:52:41 +01:00
commit b36daee5b1
2 changed files with 46 additions and 13 deletions

View file

@ -22,6 +22,7 @@
from copy import copy from copy import copy
import getpass import getpass
import logging import logging
import subprocess
import sys import sys
import os import os
import locale import locale
@ -34,8 +35,9 @@ from weboob.core.backendscfg import BackendAlreadyExists
from weboob.core.modules import ModuleLoadError from weboob.core.modules import ModuleLoadError
from weboob.core.repositories import ModuleInstallError from weboob.core.repositories import ModuleInstallError
from weboob.tools.browser import BrowserUnavailable, BrowserIncorrectPassword, BrowserForbidden from weboob.tools.browser import BrowserUnavailable, BrowserIncorrectPassword, BrowserForbidden
from weboob.tools.value import Value, ValueBool, ValueFloat, ValueInt from weboob.tools.value import Value, ValueBool, ValueFloat, ValueInt, ValueBackendPassword
from weboob.tools.misc import to_unicode from weboob.tools.misc import to_unicode
from weboob.tools.ordereddict import OrderedDict
from .base import BaseApplication, MoreResultsAvailable from .base import BaseApplication, MoreResultsAvailable
@ -345,7 +347,7 @@ class ConsoleApplication(BaseApplication):
continue continue
config[key].set(value) config[key].set(value)
config.save(edit=edit) config.save(edit=edit)
print 'Backend "%s" successfully added.' % name print 'Backend "%s" successfully %s.' % (name, 'edited' if edit else 'added')
return name return name
except BackendAlreadyExists: except BackendAlreadyExists:
print >>sys.stderr, 'Backend "%s" already exists.' % name print >>sys.stderr, 'Backend "%s" already exists.' % name
@ -366,15 +368,14 @@ class ConsoleApplication(BaseApplication):
if isinstance(question, Value): if isinstance(question, Value):
v = copy(question) v = copy(question)
if default: if default is not None:
v.default = default v.default = default
if masked:
v.masked = masked v.masked = masked
if regexp: if regexp is not None:
v.regexp = regexp v.regexp = regexp
if choices: if choices is not None:
v.choices = choices v.choices = choices
if tiny: if tiny is not None:
v.tiny = tiny v.tiny = tiny
else: else:
if isinstance(default, bool): if isinstance(default, bool):
@ -392,6 +393,38 @@ class ConsoleApplication(BaseApplication):
if v.id: if v.id:
question = u'[%s] %s' % (v.id, question) question = u'[%s] %s' % (v.id, question)
if isinstance(v, ValueBackendPassword):
choices = OrderedDict()
choices['c'] = 'Run an external tool during backend load'
if not v.noprompt:
choices['p'] = 'Prompt value when needed (do not store it)'
choices['s'] = 'Store value in config'
if v.is_command(v.default):
d = 'c'
elif v.default == '' and not v.noprompt:
d = 'p'
else:
d = 's'
r = self.ask('%s: How do you want to store it?' % question, choices=choices, tiny=True, default=d)
if r == 'p':
return ''
if r == 'c':
print 'Enter the shell command that will print the required value on the standard output'
if v.is_command(v.default):
print ': %s' % v.default[1:-1]
else:
d = None
while True:
cmd = self.ask('')
try:
subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError as e:
print '%s' % e
else:
return '`%s`' % cmd
aliases = {} aliases = {}
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')
@ -413,12 +446,12 @@ class ConsoleApplication(BaseApplication):
print '%s%2d)%s %s' % (self.BOLD, n + 1, self.NC, value) print '%s%2d)%s %s' % (self.BOLD, n + 1, self.NC, value)
aliases[str(n + 1)] = key aliases[str(n + 1)] = key
question = u'%s (choose in list)' % question question = u'%s (choose in list)' % question
elif default not in (None, '') and not v.masked:
question = u'%s [%s]' % (question, v.default)
if v.masked: if v.masked:
question = u'%s (hidden input)' % question question = u'%s (hidden input)' % question
if not isinstance(v, ValueBool) and not v.tiny and v.default not in (None, ''):
question = u'%s [%s]' % (question, '*******' if v.masked else v.default)
question += ': ' question += ': '
while True: while True:
@ -451,6 +484,7 @@ class ConsoleApplication(BaseApplication):
else: else:
break break
v.noprompt = True
return v.get() return v.get()
def acquire_input(self, content=None, editor_params=None): def acquire_input(self, content=None, editor_params=None):

View file

@ -20,7 +20,6 @@
import re import re
import subprocess import subprocess
import sys
from .ordereddict import OrderedDict from .ordereddict import OrderedDict
from .misc import to_unicode from .misc import to_unicode
@ -130,7 +129,7 @@ class Value(object):
Test if a value begin with ` and end with ` Test if a value begin with ` and end with `
(`command` is used to call external programms) (`command` is used to call external programms)
""" """
return (v.startswith(u'`') and v.endswith(u'`')) return isinstance(v, basestring) and v.startswith(u'`') and v.endswith(u'`')