Rev 6083: Implement default values from environment for config options in file:///home/vila/src/bzr/experimental/default-value-from-env/

Vincent Ladeuil v.ladeuil+lp at free.fr
Fri Aug 19 12:54:18 UTC 2011


At file:///home/vila/src/bzr/experimental/default-value-from-env/

------------------------------------------------------------
revno: 6083
revision-id: v.ladeuil+lp at free.fr-20110819125418-975b2j4qfpgfcr6m
parent: pqm at pqm.ubuntu.com-20110818042306-neji85pljf86z885
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: default-value-from-env
timestamp: Fri 2011-08-19 14:54:18 +0200
message:
  Implement default values from environment for config options
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-08-16 15:12:39 +0000
+++ b/bzrlib/config.py	2011-08-19 12:54:18 +0000
@@ -2274,8 +2274,9 @@
     encoutered, in which config files it can be stored.
     """
 
-    def __init__(self, name, default=None, help=None, from_unicode=None,
-                 invalid=None):
+    def __init__(self, name, default=None, default_from_env=None,
+                 help=None,
+                 from_unicode=None, invalid=None):
         """Build an option definition.
 
         :param name: the name used to refer to the option.
@@ -2283,6 +2284,10 @@
         :param default: the default value to use when none exist in the config
             stores.
 
+        :param default_from_env: A list of environment variables which can
+           provide a default value. 'default' will be used only if none of the
+           variables specified here are set in the environment.
+
         :param help: a doc string to explain the option to the user.
 
         :param from_unicode: a callable to convert the unicode string
@@ -2296,8 +2301,11 @@
             'warning' (emit a warning), 'error' (emit an error message and
             terminates).
         """
+        if default_from_env is None:
+            default_from_env = []
         self.name = name
         self.default = default
+        self.default_from_env = default_from_env
         self.help = help
         self.from_unicode = from_unicode
         if invalid and invalid not in ('warning', 'error'):
@@ -2305,6 +2313,11 @@
         self.invalid = invalid
 
     def get_default(self):
+        for var in self.default_from_env:
+            try:
+                return os.environ[var]
+            except KeyError:
+                continue
         return self.default
 
     def get_help_text(self, additional_see_also=None, plain=True):

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2011-08-12 14:07:21 +0000
+++ b/bzrlib/tests/test_config.py	2011-08-19 12:54:18 +0000
@@ -2213,6 +2213,20 @@
         opt = config.Option('foo', default='bar')
         self.assertEquals('bar', opt.get_default())
 
+    def test_default_value_from_env(self):
+        opt = config.Option('foo', default='bar', default_from_env=['FOO'])
+        self.overrideEnv('FOO', 'quux')
+        # Env variable provides a default taking over the option one
+        self.assertEquals('quux', opt.get_default())
+        
+    def test_first_default_value_from_env_wins(self):
+        opt = config.Option('foo', default='bar',
+                            default_from_env=['NO_VALUE', 'FOO', 'BAZ'])
+        self.overrideEnv('FOO', 'foo')
+        self.overrideEnv('BAZ', 'baz')
+        # The first env var set wins
+        self.assertEquals('foo', opt.get_default())
+
 
 class TestOptionRegistry(tests.TestCase):
 

=== modified file 'doc/developers/configuration.txt'
--- a/doc/developers/configuration.txt	2011-08-10 13:26:27 +0000
+++ b/doc/developers/configuration.txt	2011-08-19 12:54:18 +0000
@@ -6,7 +6,7 @@
 * a name: a valid python identifier (even if it's not used as an
   identifier in python itself)
 
-* a value: a unicode string
+* a value: a unicode string or a list of unicode strings.
 
 Option
 ------
@@ -19,6 +19,10 @@
 * default: the default value that Stack.get() should return if no
   value can be found for the option.
 
+* default_from_env: a list of environment variables. The first variable set
+  will provide a default value overriding 'default' which remains the
+  default value is *no* environment variable is set.
+
 * help: a doc string describing the option, the first line should be a
   summary and can be followed by a blank line and a more detailed
   explanation.

=== modified file 'doc/en/release-notes/bzr-2.5.txt'
--- a/doc/en/release-notes/bzr-2.5.txt	2011-08-18 04:23:06 +0000
+++ b/doc/en/release-notes/bzr-2.5.txt	2011-08-19 12:54:18 +0000
@@ -72,6 +72,9 @@
   while --match-message, --match-author, --match-committer and
   --match-bugs match each of those fields.
 
+* ``config.Option`` can now declares ``default_from_env``, a list of
+  environment variables to get a default value from. (Vincent Ladeuil)
+
 * New builtin ``bzr branches`` command, which lists all colocated branches
   in a directory. (Jelmer Vernooij, #826820)
 



More information about the bazaar-commits mailing list