diff options
author | Morgan Fainberg <m@metacloud.com> | 2014-03-25 15:29:38 -0700 |
---|---|---|
committer | Morgan Fainberg <m@metacloud.com> | 2014-03-25 15:29:38 -0700 |
commit | 7a61fdad07cd778602f687f085eb15e235d06198 (patch) | |
tree | a22873ca0b77d42a63843505be21015580584613 | |
parent | ae737eac6e792cbc6f05a527f9398876f84170c6 (diff) | |
download | tox-7a61fdad07cd778602f687f085eb15e235d06198.tar.gz |
Make optional env replacements use the ``env`` keyword
The ':' character is a reserved character in shell, meaning it cannot
be used as part of the ENV variable name. If the ENV key has a ':'
in it split on the ':' and use the second value as the default
instead of raising an exception.
-rw-r--r-- | doc/config.txt | 4 | ||||
-rw-r--r-- | tests/test_config.py | 6 | ||||
-rw-r--r-- | tox/_config.py | 29 |
3 files changed, 16 insertions, 23 deletions
diff --git a/doc/config.txt b/doc/config.txt index 5a9134d..c53df68 100644 --- a/doc/config.txt +++ b/doc/config.txt @@ -298,7 +298,7 @@ environment variable substitutions with default values If you specify a substitution string like this:: - {defenv:DEFAULTVALUE:KEY} + {env:KEY:DEFAULTVALUE} then the value will be retrieved as ``os.environ['KEY']`` and replace with DEFAULTVALUE if the environment variable does not @@ -306,7 +306,7 @@ exist. If you specify a substitution string like this:: - {defenv::KEY} + {env:KEY:} then the value will be retrieved as ``os.environ['KEY']`` and replace with and empty string if the environment variable does not diff --git a/tests/test_config.py b/tests/test_config.py index 3084001..3647673 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -246,9 +246,9 @@ class TestIniParser: monkeypatch.setenv("KEY1", "hello") config = newconfig(""" [section] - key1={defenv:DEFAULT_VALUE:KEY1} - key2={defenv:DEFAULT_VALUE:KEY2} - key3={defenv::KEY3} + key1={env:KEY1:DEFAULT_VALUE} + key2={env:KEY2:DEFAULT_VALUE} + key3={env:KEY3:} """) reader = IniReader(config._cfg) x = reader.getdefault("section", "key1") diff --git a/tox/_config.py b/tox/_config.py index 468af5f..86ab3a0 100644 --- a/tox/_config.py +++ b/tox/_config.py @@ -595,11 +595,20 @@ class IniReader: #print "getdefault", section, name, "returned", repr(x) return x - def _do_replace_env(self, envkey, default=None): - if not envkey: + def _replace_env(self, match): + match_value = match.group('substitution_value') + if not match_value: raise tox.exception.ConfigError( 'env: requires an environment variable name') + default = None + envkey_split = match_value.split(':', 1) + + if len(envkey_split) is 2: + envkey, default = envkey_split + else: + envkey = match_value + if not envkey in os.environ and default is None: raise tox.exception.ConfigError( "substitution env:%r: unkown environment variable %r" % @@ -607,21 +616,6 @@ class IniReader: return os.environ.get(envkey, default) - def _replace_env(self, match): - envkey = match.group('substitution_value') - return self._do_replace_env(envkey) - - def _replace_env_with_default(self, match): - envkey = match.group('substitution_value') - try: - default, envkey = envkey.split(':', 1) - except ValueError: - raise tox.exception.ConfigError( - "substitution 'defenv:%r': malformed, expected " - "'defenv:DEFAULTVALUE:KEY'" % match) - - return self._do_replace_env(envkey, default=default) - def _substitute_from_other_section(self, key): if key.startswith("[") and "]" in key: i = key.find("]") @@ -661,7 +655,6 @@ class IniReader: handlers = { 'env' : self._replace_env, - 'defenv' : self._replace_env_with_default, None : self._replace_substitution, } try: |