Skip to content

Commit

Permalink
Merge pull request ceph#25324 from votdev/fix_raw_input
Browse files Browse the repository at this point in the history
ceph-volume: Adapt code to support Python3

Reviewed-by: Alfredo Deza <[email protected]>
  • Loading branch information
alfredodeza authored Jan 18, 2019
2 parents ad20b27 + fe25a0e commit 73aafe5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/ceph-volume/ceph_volume/tests/util/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,22 @@ class TestPromptBool(object):
def test_trueish(self, response):
fake_input = lambda x: response
qx = 'what the what?'
assert util.prompt_bool(qx, _raw_input=fake_input) is True
assert util.prompt_bool(qx, input_=fake_input) is True

@pytest.mark.parametrize('response', false_responses())
def test_falseish(self, response):
fake_input = lambda x: response
qx = 'what the what?'
assert util.prompt_bool(qx, _raw_input=fake_input) is False
assert util.prompt_bool(qx, input_=fake_input) is False

def test_try_again_true(self):
responses = ['g', 'h', 'y']
fake_input = lambda x: responses.pop(0)
qx = 'what the what?'
assert util.prompt_bool(qx, _raw_input=fake_input) is True
assert util.prompt_bool(qx, input_=fake_input) is True

def test_try_again_false(self):
responses = ['g', 'h', 'n']
fake_input = lambda x: responses.pop(0)
qx = 'what the what?'
assert util.prompt_bool(qx, _raw_input=fake_input) is False
assert util.prompt_bool(qx, input_=fake_input) is False
11 changes: 8 additions & 3 deletions src/ceph-volume/ceph_volume/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
from math import floor
from ceph_volume import terminal

try:
input = raw_input # pylint: disable=redefined-builtin
except NameError:
pass

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -78,12 +83,12 @@ def str_to_bool(val):
raise ValueError("Invalid input value: %s" % val)


def prompt_bool(question, _raw_input=None):
def prompt_bool(question, input_=None):
"""
Interface to prompt a boolean (or boolean-like) response from a user.
Usually a confirmation.
"""
input_prompt = _raw_input or raw_input
input_prompt = input_ or input
prompt_format = '--> {question} '.format(question=question)
response = input_prompt(prompt_format)
try:
Expand All @@ -92,4 +97,4 @@ def prompt_bool(question, _raw_input=None):
terminal.error('Valid true responses are: y, yes, <Enter>')
terminal.error('Valid false responses are: n, no')
terminal.error('That response was invalid, please try again')
return prompt_bool(question, _raw_input=input_prompt)
return prompt_bool(question, input_=input_prompt)

0 comments on commit 73aafe5

Please sign in to comment.