Rev 4888: (vila) UI can query integers in file:///home/pqm/archives/thelove/bzr/%2Btrunk/
Canonical.com Patch Queue Manager
pqm at pqm.ubuntu.com
Thu Dec 10 21:56:27 GMT 2009
At file:///home/pqm/archives/thelove/bzr/%2Btrunk/
------------------------------------------------------------
revno: 4888 [merge]
revision-id: pqm at pqm.ubuntu.com-20091210215626-qzr3coh4bgh71lb6
parent: pqm at pqm.ubuntu.com-20091210173537-7gui2z64ki7nioit
parent: v.ladeuil+lp at free.fr-20091210211101-9iitk95oqtczhf6s
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Thu 2009-12-10 21:56:26 +0000
message:
(vila) UI can query integers
modified:
bzrlib/tests/test_ui.py test_ui.py-20051130162854-458e667a7414af09
bzrlib/ui/__init__.py ui.py-20050824083933-8cf663c763ba53a9
bzrlib/ui/text.py text.py-20051130153916-2e438cffc8afc478
=== modified file 'bzrlib/tests/test_ui.py'
--- a/bzrlib/tests/test_ui.py 2009-12-10 15:40:21 +0000
+++ b/bzrlib/tests/test_ui.py 2009-12-10 16:54:28 +0000
@@ -159,6 +159,18 @@
# stdin should be empty
self.assertEqual('', factory.stdin.readline())
+ def test_text_ui_get_integer(self):
+ stdin = tests.StringIOWrapper(
+ "1\n"
+ " -2 \n"
+ "hmmm\nwhat else ?\nCome on\nok 42\n4.24\n42\n")
+ stdout = tests.StringIOWrapper()
+ stderr = tests.StringIOWrapper()
+ factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
+ self.assertEqual(1, factory.get_integer(""))
+ self.assertEqual(-2, factory.get_integer(""))
+ self.assertEqual(42, factory.get_integer(""))
+
def test_text_factory_prompt(self):
# see <https://launchpad.net/bugs/365891>
StringIO = tests.StringIOWrapper
@@ -363,6 +375,7 @@
self.assertEqual('password',
uif.get_password('Password for %(host)s',
host='example.com'))
+ self.assertEqual(42, uif.get_integer('And all that jazz ?'))
class TestBoolFromString(tests.TestCase):
=== modified file 'bzrlib/ui/__init__.py'
--- a/bzrlib/ui/__init__.py 2009-12-09 05:47:32 +0000
+++ b/bzrlib/ui/__init__.py 2009-12-10 16:54:28 +0000
@@ -207,6 +207,16 @@
"""
raise NotImplementedError(self.get_boolean)
+ def get_integer(self, prompt):
+ """Get an integer from the user.
+
+ :param prompt: a message to prompt the user with. Could be a multi-line
+ prompt but without a terminating \n.
+
+ :return: A signed integer.
+ """
+ raise NotImplementedError(self.get_integer)
+
def make_progress_view(self):
"""Construct a new ProgressView object for this UI.
@@ -295,12 +305,15 @@
def get_boolean(self, prompt):
return self.responses.pop(0)
+ def get_integer(self, prompt):
+ return self.responses.pop(0)
+
def get_password(self, prompt='', **kwargs):
return self.responses.pop(0)
def get_username(self, prompt, **kwargs):
return self.responses.pop(0)
-
+
def assert_all_input_consumed(self):
if self.responses:
raise AssertionError("expected all input in %r to be consumed"
=== modified file 'bzrlib/ui/text.py'
--- a/bzrlib/ui/text.py 2009-12-09 05:47:32 +0000
+++ b/bzrlib/ui/text.py 2009-12-10 16:54:28 +0000
@@ -83,6 +83,15 @@
# end-of-file; possibly should raise an error here instead
return None
+ def get_integer(self, prompt):
+ while True:
+ self.prompt(prompt)
+ line = self.stdin.readline()
+ try:
+ return int(line)
+ except ValueError:
+ pass
+
def get_non_echoed_password(self):
isatty = getattr(self.stdin, 'isatty', None)
if isatty is not None and isatty():
More information about the bazaar-commits
mailing list