Added rudimentary type conversions for env vars.
Extendible to support other formats.
@@ -98,7 +98,7 @@ class ConfigParser:
TOKENS = [
'#.*$' # comments
- '|(?P<env_var>env\([A-Z_][0-9A-Z_]*\))' # environment variable
+ '|(?P<env_var>env\([A-Z_][0-9A-Z_]*(, *[a-z]+)?\))' # environment var
'|(?P<operator>[()[\]{},:]|\+?=)' # operators
'|(?P<literal>'
r'("(?:[^"\\]|\\.)*")' # double quoted string
@@ -296,8 +296,22 @@ class ConfigParser:
return value
def make_value_from_env(self, value):
- # Extract the environment variable
- env_var = re.match('env\(([0-9A-Z_]+)\)', value).group(1)
+ # Extract the environment variable and type
+ match = re.match('env\(([0-9A-Z_]+)(, *[a-z]+)?\)', value)
+ env_var, env_type = match.groups()
+ if not env_type:
+ env_type = 'str'
+ else:
+ # Remove comma and whitespace from match
+ env_type = env_type[1:].lstrip()
+
+ conversion_options = {
+ 'str': str,
+ 'int': int,
+ 'float': float,
+ }
+ if env_type not in conversion_options:
+ self.error('Invalid conversion')
# Check the environment variable
# TODO: dynamic support, (lambda: os.getenv(env_var))
@@ -305,7 +319,9 @@ class ConfigParser:
if value is None:
self.error('Environment variable undefined')
- return value
+ try: return conversion_options[env_type](value)
+ except ValueError:
+ self.error('Bad conversion')
def separator(self, separator=',', end_token=None):
'''
