← Back to team overview

configglue team mailing list archive

[Merge] lp:~bloodearnest/configglue/envvar-defaults into lp:configglue

 

Simon Davy has proposed merging lp:~bloodearnest/configglue/envvar-defaults into lp:configglue.

Requested reviews:
  Configglue developers (configglue)

For more details, see:
https://code.launchpad.net/~bloodearnest/configglue/envvar-defaults/+merge/162378

Add support for default arguments in env vars.

e.g.

foo = ${bar:-some default}
-- 
https://code.launchpad.net/~bloodearnest/configglue/envvar-defaults/+merge/162378
Your team Configglue developers is requested to review the proposed merge of lp:~bloodearnest/configglue/envvar-defaults into lp:configglue.
=== modified file 'configglue/parser.py'
--- configglue/parser.py	2012-06-15 20:07:15 +0000
+++ configglue/parser.py	2013-05-03 15:41:37 +0000
@@ -437,16 +437,33 @@
             return rawval
 
         # interpolate environment variables
-        pattern = re.sub(r'\${([A-Z_]+)}', r'%(\1)s', rawval)
-        pattern = re.sub(r'\$([A-Z_]+)', r'%(\1)s', pattern)
+        default = None
+        pattern = name = rawval
+        match = re.match(r'\${([A-Z_]+)(:-(.+))?}', rawval)
+        if match:
+            groups = match.groups()
+            name = groups[0]
+            pattern = '%(' + name + ')s'
+            if len(groups) > 2:
+                default = groups[2]
+        else:
+            match = re.match(r'\$([A-Z_]+)', rawval)
+            if match:
+                name = match.group(1)
+                pattern = '%(' + name + ')s'
 
         keys = self._extract_interpolation_keys(pattern)
         if not keys:
             # interpolation keys are not valid
             return rawval
 
-        interpolated = pattern % os.environ
-        return interpolated
+        try:
+            return pattern % os.environ
+        except KeyError:
+            if default is None:
+                raise
+            else:
+                return default
 
     def _get_default(self, section, option):
         # cater for 'special' sections

=== modified file 'configglue/tests/test_parser.py'
--- configglue/tests/test_parser.py	2012-06-15 14:04:51 +0000
+++ configglue/tests/test_parser.py	2013-05-03 15:41:37 +0000
@@ -283,6 +283,20 @@
         self.assertEqual(result, 'foo')
 
     @patch('configglue.parser.os')
+    def test_interpolate_environment_with_default_uses_env(self, mock_os):
+        mock_os.environ = {'PATH': 'foo'}
+        parser = SchemaConfigParser(Schema())
+        result = parser.interpolate_environment("${PATH:-bar}")
+        self.assertEqual(result, 'foo')
+
+    @patch('configglue.parser.os')
+    def test_interpolate_environment_with_default_uses_default(self, mock_os):
+        mock_os.environ = {}
+        parser = SchemaConfigParser(Schema())
+        result = parser.interpolate_environment("${PATH:-bar}")
+        self.assertEqual(result, 'bar')
+
+    @patch('configglue.parser.os')
     def test_interpolate_environment_in_config(self, mock_os):
         mock_os.environ = {'PYTHONPATH': 'foo', 'PATH': 'bar'}
 


Follow ups