Rev 4494: Add easier access to boolean values in configuration files. in file:///home/vila/src/bzr/experimental/cleanup-strict/

Vincent Ladeuil v.ladeuil+lp at free.fr
Mon Jun 29 14:52:34 BST 2009


At file:///home/vila/src/bzr/experimental/cleanup-strict/

------------------------------------------------------------
revno: 4494
revision-id: v.ladeuil+lp at free.fr-20090629135234-o9yl5hirlss1vg5r
parent: v.ladeuil+lp at free.fr-20090629130120-l3zssq0ra1n7y4wz
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: cleanup-strict
timestamp: Mon 2009-06-29 15:52:34 +0200
message:
  Add easier access to boolean values in configuration files.
  
  * bzrlib/tests/test_config.py:
  (TestIniConfig.test_get_user_option_as_bool): Test valid and
  invalid booleans values.
  
  * bzrlib/config.py:
  (Config.get_user_option_as_bool): Provide access to configuration
  variables as booleans if they are defined.
  (Config.__init__): I couldn't find that constructor one time too
  much.
  (IniBasedConfig.__init__): Ditto.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2009-06-11 06:49:21 +0000
+++ b/bzrlib/config.py	2009-06-29 13:52:34 +0000
@@ -146,6 +146,9 @@
 class Config(object):
     """A configuration policy - what username, editor, gpg needs etc."""
 
+    def __init__(self):
+        super(Config, self).__init__()
+
     def get_editor(self):
         """Get the users pop up editor."""
         raise NotImplementedError
@@ -174,6 +177,15 @@
         """Get a generic option - no special process, no default."""
         return self._get_user_option(option_name)
 
+    def get_user_option_as_bool(self, option_name):
+        """Get a generic option - no special process, no default.
+
+        :return None if the option doesn't exist or its value can't be
+            interpreted as a boolean. Returns True or False ortherwise.
+        """
+        s = self._get_user_option(option_name)
+        return ui.string_as_boolean(s)
+
     def gpg_signing_command(self):
         """What program should be used to sign signatures?"""
         result = self._gpg_signing_command()
@@ -196,9 +208,6 @@
         """See log_format()."""
         return None
 
-    def __init__(self):
-        super(Config, self).__init__()
-
     def post_commit(self):
         """An ordered list of python functions to call.
 
@@ -299,6 +308,11 @@
 class IniBasedConfig(Config):
     """A configuration policy that draws from ini files."""
 
+    def __init__(self, get_filename):
+        super(IniBasedConfig, self).__init__()
+        self._get_filename = get_filename
+        self._parser = None
+
     def _get_parser(self, file=None):
         if self._parser is not None:
             return self._parser
@@ -381,11 +395,6 @@
         """See Config.log_format."""
         return self._get_user_option('log_format')
 
-    def __init__(self, get_filename):
-        super(IniBasedConfig, self).__init__()
-        self._get_filename = get_filename
-        self._parser = None
-
     def _post_commit(self):
         """See Config.post_commit."""
         return self._get_user_option('post_commit')

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2009-06-29 12:06:04 +0000
+++ b/bzrlib/tests/test_config.py	2009-06-29 13:52:34 +0000
@@ -367,6 +367,20 @@
         parser = my_config._get_parser(file=config_file)
         self.failUnless(my_config._get_parser() is parser)
 
+    def test_get_user_option_as_bool(self):
+        config_file = StringIO("""
+a_true_bool = true
+a_false_bool = 0
+an_invalid_bool = maybe
+a_list = hmm, who knows ? # This interpreted as a list !
+""".encode('utf-8'))
+        my_config = config.IniBasedConfig(None)
+        parser = my_config._get_parser(file=config_file)
+        get_option = my_config.get_user_option_as_bool
+        self.assertEqual(True, get_option('a_true_bool'))
+        self.assertEqual(False, get_option('a_false_bool'))
+        self.assertIs(None, get_option('an_invalid_bool'))
+        self.assertIs(None, get_option('not_defined_in_this_config'))
 
 class TestGetConfig(tests.TestCase):
 

=== modified file 'bzrlib/ui/__init__.py'
--- a/bzrlib/ui/__init__.py	2009-06-29 13:01:20 +0000
+++ b/bzrlib/ui/__init__.py	2009-06-29 13:52:34 +0000
@@ -62,7 +62,7 @@
     :return: True or False for some known strings, None otherwise.
     """
     val = None
-    if s is not None:
+    if type(s) in (str, unicode):
         try:
             val = _known_boolean_strings[s.lower()]
         except KeyError:



More information about the bazaar-commits mailing list