Rev 4505: Get a bool or none from a config file. in file:///home/vila/src/bzr/experimental/bool-config-option/

Vincent Ladeuil v.ladeuil+lp at free.fr
Thu Jul 2 09:59:16 BST 2009


At file:///home/vila/src/bzr/experimental/bool-config-option/

------------------------------------------------------------
revno: 4505
revision-id: v.ladeuil+lp at free.fr-20090702085916-8mmk6twh14oeng5u
parent: v.ladeuil+lp at free.fr-20090702085235-85fqq4q5d3bt5w7u
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: bool-config-option
timestamp: Thu 2009-07-02 10:59:16 +0200
message:
  Get a bool or none from a config file.
  
  * bzrlib/tests/test_config.py:
  (TestIniConfig.test_get_user_option_as_bool): Tests
  true/false/none cases.
  
  * bzrlib/config.py:
  (Config.__init__, IniBasedConfig.__init__): Moved to be the first
  defined method in the class, we do that everywhere else, it's
  really misleading otherwise.
  
  (Config.get_user_option_as_bool): Mimic get_user_option but
  returns either a bool or None.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2009-06-11 06:49:21 +0000
+++ b/bzrlib/config.py	2009-07-02 08:59:16 +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 as a boolean - 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.bool_from_string(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:14:09 +0000
+++ b/bzrlib/tests/test_config.py	2009-07-02 08:59:16 +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):
 



More information about the bazaar-commits mailing list