Rev 4887: Allows ui factories to query users for an integer. in file:///home/vila/src/bzr/experimental/ui-get-integer/

Vincent Ladeuil v.ladeuil+lp at free.fr
Thu Dec 10 16:54:29 GMT 2009


At file:///home/vila/src/bzr/experimental/ui-get-integer/

------------------------------------------------------------
revno: 4887
revision-id: v.ladeuil+lp at free.fr-20091210165428-n9nysxcuz0vwfm9o
parent: pqm at pqm.ubuntu.com-20091210164716-e18k2to740e9eq7s
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: ui-get-integer
timestamp: Thu 2009-12-10 17:54:28 +0100
message:
  Allows ui factories to query users for an integer.
  
  * bzrlib/tests/test_ui.py:
  (TestTextUIFactory.test_text_ui_get_integer,
  CannedInputUIFactoryTests.test_canned_input_get_input): Tests.
  
  * bzrlib/ui/__init__.py:
  (UIFactory.get_integer): New definition.
  (CannedInputUIFactory.get_integer): Implementation.
  
  * bzrlib/ui/text.py:
  (TextUIFactory.get_integer): Implementation.
-------------- next part --------------
=== 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