From b36daee5b1d4fb9fd6af81da9f7a574f5e4ffcc2 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Thu, 23 Jan 2014 23:52:41 +0100 Subject: [PATCH] prompt (beautifully) user during configuration of a backend --- weboob/tools/application/console.py | 56 +++++++++++++++++++++++------ weboob/tools/value.py | 3 +- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/weboob/tools/application/console.py b/weboob/tools/application/console.py index a7be47a8..ac56ff63 100644 --- a/weboob/tools/application/console.py +++ b/weboob/tools/application/console.py @@ -22,6 +22,7 @@ from copy import copy import getpass import logging +import subprocess import sys import os import locale @@ -34,8 +35,9 @@ from weboob.core.backendscfg import BackendAlreadyExists from weboob.core.modules import ModuleLoadError from weboob.core.repositories import ModuleInstallError 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.ordereddict import OrderedDict from .base import BaseApplication, MoreResultsAvailable @@ -345,7 +347,7 @@ class ConsoleApplication(BaseApplication): continue config[key].set(value) config.save(edit=edit) - print 'Backend "%s" successfully added.' % name + print 'Backend "%s" successfully %s.' % (name, 'edited' if edit else 'added') return name except BackendAlreadyExists: print >>sys.stderr, 'Backend "%s" already exists.' % name @@ -366,15 +368,14 @@ class ConsoleApplication(BaseApplication): if isinstance(question, Value): v = copy(question) - if default: + if default is not None: v.default = default - if masked: - v.masked = masked - if regexp: + v.masked = masked + if regexp is not None: v.regexp = regexp - if choices: + if choices is not None: v.choices = choices - if tiny: + if tiny is not None: v.tiny = tiny else: if isinstance(default, bool): @@ -392,6 +393,38 @@ class ConsoleApplication(BaseApplication): if v.id: 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 = {} if isinstance(v, ValueBool): 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) aliases[str(n + 1)] = key 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: 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 += ': ' while True: @@ -451,6 +484,7 @@ class ConsoleApplication(BaseApplication): else: break + v.noprompt = True return v.get() def acquire_input(self, content=None, editor_params=None): diff --git a/weboob/tools/value.py b/weboob/tools/value.py index a52b8e32..20ac2f6c 100644 --- a/weboob/tools/value.py +++ b/weboob/tools/value.py @@ -20,7 +20,6 @@ import re import subprocess -import sys from .ordereddict import OrderedDict from .misc import to_unicode @@ -130,7 +129,7 @@ class Value(object): Test if a value begin with ` and end with ` (`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'`')