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

Vincent Ladeuil v.ladeuil+lp at free.fr
Tue Oct 27 10:28:22 GMT 2009


At file:///home/vila/src/bzr/experimental/conflict-manager/

------------------------------------------------------------
revno: 4635
revision-id: v.ladeuil+lp at free.fr-20091027102821-u3f2l4tjee925rii
parent: v.ladeuil+lp at free.fr-20091026144007-5vptd9794mbjdzzc
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: description
timestamp: Tue 2009-10-27 11:28:21 +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 'NEWS'
--- a/NEWS	2009-10-15 04:06:32 +0000
+++ b/NEWS	2009-10-27 10:28:21 +0000
@@ -19,6 +19,9 @@
 New Features
 ************
 
+* ``UIFactory`` can now query for signed integers.
+  (Vincent Ladeuil)
+
 Bug Fixes
 *********
 

=== modified file 'bzrlib/tests/test_ui.py'
--- a/bzrlib/tests/test_ui.py	2009-10-26 14:40:07 +0000
+++ b/bzrlib/tests/test_ui.py	2009-10-27 10:28:21 +0000
@@ -160,6 +160,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
@@ -342,12 +354,13 @@
 class CannedInputUIFactoryTests(tests.TestCase):
 
     def test_canned_input_get_input(self):
-        uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password'])
-        self.assertEqual(uif.get_boolean('Extra cheese?'), True)
-        self.assertEqual(uif.get_username('Enter your user name'), 'mbp')
-        self.assertEqual(uif.get_password('Password for %(host)s',
-                                          host='example.com'),
-                         'password')
+        uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password', 42])
+        self.assertEqual(True, uif.get_boolean('Extra cheese?'))
+        self.assertEqual('mbp', uif.get_username('Enter your user name'))
+        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-09-23 06:29:46 +0000
+++ b/bzrlib/ui/__init__.py	2009-10-27 10:28:21 +0000
@@ -179,6 +179,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.
 
@@ -356,12 +366,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-09-23 06:29:46 +0000
+++ b/bzrlib/ui/text.py	2009-10-27 10:28:21 +0000
@@ -82,6 +82,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