Rev 5554: Implement a basic interpolation. in file:///home/vila/src/bzr/experimental/config/

Vincent Ladeuil v.ladeuil+lp at free.fr
Wed Dec 22 14:20:28 GMT 2010


At file:///home/vila/src/bzr/experimental/config/

------------------------------------------------------------
revno: 5554
revision-id: v.ladeuil+lp at free.fr-20101222142028-vh6df9cnna7keuaq
parent: v.ladeuil+lp at free.fr-20101221115257-dudhviutreh76mxf
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: interpolate
timestamp: Wed 2010-12-22 15:20:28 +0100
message:
  Implement a basic interpolation.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2010-12-20 17:40:30 +0000
+++ b/bzrlib/config.py	2010-12-22 14:20:28 +0000
@@ -143,6 +143,30 @@
                 pass
         return self[section][name]
 
+    option_ref_re = None
+
+    def interpolate(self, string, env=None):
+        if self.option_ref_re is None:
+            # We want to match the most embedded reference first (i.e. for
+            # '{{foo}}' we will get '{foo}',
+            # for '{bar{baz}}' we will get '{baz}'
+            self.option_ref_re = re.compile('({[^{}]+})')
+        while True:
+            found = self.option_ref_re.search(string)
+            if found is None:
+                # No more references, interpolation is done
+                break
+            ref = found.group()
+            name = ref[1:-1]
+            if env is not None and name in env:
+                value = env[name]
+            elif name in self:
+                # Search for an option
+                value = self[name]
+            else:
+                raise UnkownOption(name)
+            string = string.replace(ref, value)
+        return string
 
 class Config(object):
     """A configuration policy - what username, editor, gpg needs etc."""

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2010-12-16 13:15:42 +0000
+++ b/bzrlib/tests/test_config.py	2010-12-22 14:20:28 +0000
@@ -334,6 +334,38 @@
             self.fail('Error in config file not detected')
 
 
+
+class TestConfigObjInterpolation(tests.TestCase):
+
+    def get_config(self, string=None):
+        if string is None:
+            string = ''
+        string = StringIO(string.encode('utf-8'))
+        c = config.ConfigObj(string, encoding='utf-8')
+        return c
+
+    def assertInterpolate(self, expected, conf, string, env=None):
+        self.assertEquals(expected, conf.interpolate(string, env))
+
+    def test_no_interpolation(self):
+        c = self.get_config('')
+        self.assertInterpolate('foo', c, 'foo')
+
+    def test_interpolate_in_env(self):
+        c = self.get_config('')
+        self.assertInterpolate('bar', c, '{foo}', {'foo': 'bar'})
+
+    def test_interpolate_simple_ref(self):
+        c = self.get_config('foo=xxx')
+        self.assertInterpolate('xxx', c, '{foo}')
+
+    def test_interpolate_indirect_ref(self):
+        c = self.get_config("""foo=xxx
+bar={foo}
+""")
+        self.assertInterpolate('xxx', c, '{bar}')
+
+
 class TestConfig(tests.TestCase):
 
     def test_constructs(self):



More information about the bazaar-commits mailing list