Rev 4492: The ui can interpret strings as booleans. in file:///home/vila/src/bzr/experimental/cleanup-strict/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Mon Jun 29 13:48:34 BST 2009
At file:///home/vila/src/bzr/experimental/cleanup-strict/
------------------------------------------------------------
revno: 4492
revision-id: v.ladeuil+lp at free.fr-20090629124834-aytaov2q72pad3p7
parent: v.ladeuil+lp at free.fr-20090629120604-s1mhzj8llocamvzr
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: cleanup-strict
timestamp: Mon 2009-06-29 14:48:34 +0200
message:
The ui can interpret strings as booleans.
* bzrlib/tests/test_ui.py:
(TestStringAsBoolean): Test how we interpret strings as booleans.
* bzrlib/ui/__init__.py:
(string_as_boolean): Interpret string as a boolean, if possible.
-------------- next part --------------
=== modified file 'bzrlib/tests/test_ui.py'
--- a/bzrlib/tests/test_ui.py 2009-06-29 12:06:04 +0000
+++ b/bzrlib/tests/test_ui.py 2009-06-29 12:48:34 +0000
@@ -350,3 +350,45 @@
r'[####| ] a:b:c 1/2'
, uif._progress_view._render_line())
+
+class TestStringAsBoolean(tests.TestCase):
+
+ def assertIsTrue(self, s):
+ res = _mod_ui.string_as_boolean(s)
+ self.assertEquals(True, res)
+
+ def assertIsFalse(self, s):
+ res = _mod_ui.string_as_boolean(s)
+ self.assertEquals(False, res)
+
+ def assertIsNone(self, s):
+ res = _mod_ui.string_as_boolean(s)
+ self.assertIs(None, res)
+
+ def test_know_valid_values(self):
+ self.assertIsTrue('true')
+ self.assertIsFalse('false')
+ self.assertIsTrue('1')
+ self.assertIsFalse('0')
+ self.assertIsTrue('on')
+ self.assertIsFalse('off')
+ self.assertIsTrue('yes')
+ self.assertIsFalse('no')
+ self.assertIsTrue('y')
+ self.assertIsFalse('n')
+ # Also try some case variations
+ self.assertIsTrue('True')
+ self.assertIsFalse('False')
+ self.assertIsTrue('On')
+ self.assertIsFalse('Off')
+ self.assertIsTrue('ON')
+ self.assertIsFalse('OFF')
+ self.assertIsTrue('oN')
+ self.assertIsFalse('oFf')
+
+ def test_invalid_values(self):
+ self.assertIsNone(None)
+ self.assertIsNone('doubt')
+ self.assertIsNone('frue')
+ self.assertIsNone('talse')
+ self.assertIsNone('42')
=== modified file 'bzrlib/ui/__init__.py'
--- a/bzrlib/ui/__init__.py 2009-06-10 03:56:49 +0000
+++ b/bzrlib/ui/__init__.py 2009-06-29 12:48:34 +0000
@@ -43,6 +43,33 @@
""")
+_known_boolean_strings = dict(yes=True, no=False,
+ y=True, n=False,
+ on=True, off=False,
+ true=True, false=False)
+_known_boolean_strings['1'] = True
+_known_boolean_strings['0'] = False
+
+
+def string_as_boolean(s):
+ """Returns a boolean if the string can be interpreted as such.
+
+ Interprets case insensitive strings as booleans, possible values: 'true',
+ 'false', '0', '1', 'on', 'off', 'yes', 'no.
+
+ :param s: A string that should be interpreted as a boolean.
+
+ :return: True or False for some known strings, None otherwise.
+ """
+ val = None
+ if s is not None:
+ try:
+ val = _known_boolean_strings[s.lower()]
+ except KeyError:
+ pass
+ return val
+
+
class UIFactory(object):
"""UI abstraction.
More information about the bazaar-commits
mailing list