From dd1f7f556b3181d992b607cd8d1f9bd8760cf5a6 Mon Sep 17 00:00:00 2001 From: Lele Gaifax Date: Sat, 23 Apr 2016 18:10:19 +0200 Subject: [PATCH 1/3] Honor the answers passed to the prompt() function This allow passing a set of answers to override the default values of the questions. --- examples/pre_answers.py | 22 ++++++++++ inquirer/questions.py | 5 ++- inquirer/render/console/_password.py | 29 +------------ tests/acceptance/test_pre_answers.py | 61 ++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 examples/pre_answers.py create mode 100644 tests/acceptance/test_pre_answers.py diff --git a/examples/pre_answers.py b/examples/pre_answers.py new file mode 100644 index 00000000..e8f64120 --- /dev/null +++ b/examples/pre_answers.py @@ -0,0 +1,22 @@ +from pprint import pprint + +import inquirer + +if __name__ == '__main__': + + questions = [ + inquirer.Text('user', message='Please enter your github username', validate=lambda _, x: x != '.'), + inquirer.Password('password', message='Please enter your password'), + inquirer.Text('repo', message='Please enter the repo name', default='default'), + inquirer.Checkbox('topics', message='Please define your type of project?', choices=['common', 'backend', 'frontend'], ), + inquirer.Text('organization', message='If this is a repo from a organization please enter the organization name, if not just leave this blank'), + inquirer.Confirm('correct', message='This will delete all your current labels and create a new ones. Continue?', default=False), + ] + + answers = inquirer.prompt(questions) + + # Ask again, using previous values as defaults + + answers = inquirer.prompt(questions, answers=answers) + + pprint(answers) diff --git a/inquirer/questions.py b/inquirer/questions.py index f00d8a2f..02d6a573 100644 --- a/inquirer/questions.py +++ b/inquirer/questions.py @@ -97,7 +97,10 @@ def message(self): @property def default(self): - return self._solve(self._default) + value = self.answers.get(self.name) + if value is None: + value = self._solve(self._default) + return value @property def choices_generator(self): diff --git a/inquirer/render/console/_password.py b/inquirer/render/console/_password.py index 4fd2d7b0..04757a6d 100644 --- a/inquirer/render/console/_password.py +++ b/inquirer/render/console/_password.py @@ -1,33 +1,8 @@ # -*- coding: utf-8 -*- -from readchar import key -from inquirer import errors -from .base import BaseConsoleRender +from ._text import Text -class Password(BaseConsoleRender): - title_inline = True - - def __init__(self, *args, **kwargs): - super(Password, self).__init__(*args, **kwargs) - self.current = '' - +class Password(Text): def get_current_value(self): return '*' * len(self.current) - - def process_input(self, pressed): - if pressed == key.CTRL_C: - raise KeyboardInterrupt() - - if pressed == key.ENTER: - raise errors.EndOfInput(self.current) - - if pressed == key.BACKSPACE: - if len(self.current): - self.current = self.current[:-1] - return - - if len(pressed) != 1: - return - - self.current += pressed diff --git a/tests/acceptance/test_pre_answers.py b/tests/acceptance/test_pre_answers.py new file mode 100644 index 00000000..6d7d99ae --- /dev/null +++ b/tests/acceptance/test_pre_answers.py @@ -0,0 +1,61 @@ +import unittest +import pexpect +from readchar import key + +expected_result = r"""\ +{'correct': True,\r\n + 'organization': '',\r\n + 'password': 'edcba',\r\n + 'repo': 'default',\r\n + 'topics': \['common'\],\r\n + 'user': 'abcde'}\r\n""" + +class PreAnswersTest(unittest.TestCase): + def setUp(self): + self.sut = pexpect.spawn('python examples/pre_answers.py') + + def test_minimal_input(self): + # user + self.sut.expect("Please enter", timeout=1) + self.sut.send('abcde') + self.sut.send(key.ENTER) + # password + self.sut.expect("Please enter", timeout=1) + self.sut.send('edcba') + self.sut.send(key.ENTER) + # repo + self.sut.expect("Please enter", timeout=1) + self.sut.send(key.ENTER) + # topics + self.sut.expect("Please define", timeout=1) + self.sut.send(key.SPACE) + self.sut.send(key.ENTER) + # organization + self.sut.expect("If this is", timeout=1) + self.sut.send(key.ENTER) + # correct + self.sut.expect("This will delete", timeout=1) + self.sut.send('y') + + # again + + # user + self.sut.expect("Please enter", timeout=1) + self.sut.send(key.ENTER) + # password + self.sut.expect("Please enter", timeout=1) + self.sut.send(key.ENTER) + # repo + self.sut.expect("Please enter", timeout=1) + self.sut.send(key.ENTER) + # topics + self.sut.expect("Please define", timeout=1) + self.sut.send(key.ENTER) + # organization + self.sut.expect("If this is", timeout=1) + self.sut.send(key.ENTER) + # correct + self.sut.expect("This will delete", timeout=1) + self.sut.send(key.ENTER) + + self.sut.expect([expected_result, pexpect.EOF], timeout=1) From 0d78e23321178901b05e0d147e54d4ebb397a4d8 Mon Sep 17 00:00:00 2001 From: Lele Gaifax Date: Sat, 23 Apr 2016 18:51:55 +0200 Subject: [PATCH 2/3] Cosmetic, reformat code as suggested by Miguel --- inquirer/questions.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/inquirer/questions.py b/inquirer/questions.py index 02d6a573..a49d2782 100644 --- a/inquirer/questions.py +++ b/inquirer/questions.py @@ -97,10 +97,7 @@ def message(self): @property def default(self): - value = self.answers.get(self.name) - if value is None: - value = self._solve(self._default) - return value + return self.answers.get(self.name) or self._solve(self._default) @property def choices_generator(self): From a7b73e14619d1379004f41bd5a48c8dae784daac Mon Sep 17 00:00:00 2001 From: Lele Gaifax Date: Sat, 23 Apr 2016 18:59:44 +0200 Subject: [PATCH 3/3] Make flake8 happy --- tests/acceptance/test_pre_answers.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/acceptance/test_pre_answers.py b/tests/acceptance/test_pre_answers.py index 6d7d99ae..c9d84bcd 100644 --- a/tests/acceptance/test_pre_answers.py +++ b/tests/acceptance/test_pre_answers.py @@ -2,6 +2,7 @@ import pexpect from readchar import key + expected_result = r"""\ {'correct': True,\r\n 'organization': '',\r\n @@ -10,6 +11,7 @@ 'topics': \['common'\],\r\n 'user': 'abcde'}\r\n""" + class PreAnswersTest(unittest.TestCase): def setUp(self): self.sut = pexpect.spawn('python examples/pre_answers.py')