4 new commits in tox: https://bitbucket.org/hpk42/tox/commits/cbcde59447f5/ Changeset: cbcde59447f5 User: morgan_fainberg Date: 2014-03-10 21:39:48 Summary: Support optional ENV variable substitution in tox.ini
Add in support for optional ENV variable substitutions where instead of raising an error if the environmental variable does not exist in os.environ, the value is substituted with an empty string. Affected #: 3 files diff -r b0360a54ab368ef428c7f83601ba6b64f6fec64f -r cbcde59447f5e025f6ab08a38cbac7ac4dcfa27a doc/config.txt --- a/doc/config.txt +++ b/doc/config.txt @@ -292,6 +292,18 @@ and raise an Error if the environment variable does not exist. + +optional environment variable substitutions +++++++++++++++++++++++++++++++++++ + +If you specify a substitution string like this:: + + {optionalenv:KEY} + +then the value will be retrieved as ``os.environ['KEY']`` +and replace with '' if the environment variable does not +exist + .. _`command positional substitution`: .. _`positional substitution`: diff -r b0360a54ab368ef428c7f83601ba6b64f6fec64f -r cbcde59447f5e025f6ab08a38cbac7ac4dcfa27a tests/test_config.py --- a/tests/test_config.py +++ b/tests/test_config.py @@ -242,6 +242,19 @@ py.test.raises(tox.exception.ConfigError, 'reader.getdefault("section", "key2")') + def test_getdefault_environment_optional_sub(self, monkeypatch, newconfig): + monkeypatch.setenv("KEY1", "hello") + config = newconfig(""" + [section] + key1={optionalenv:KEY1} + key2={optionalenv:KEY2} + """) + reader = IniReader(config._cfg) + x = reader.getdefault("section", "key1") + assert x == "hello" + x = reader.getdefault("section", "key2") + assert x == "" + def test_getdefault_other_section_substitution(self, newconfig): config = newconfig(""" [section] diff -r b0360a54ab368ef428c7f83601ba6b64f6fec64f -r cbcde59447f5e025f6ab08a38cbac7ac4dcfa27a tox/_config.py --- a/tox/_config.py +++ b/tox/_config.py @@ -595,18 +595,25 @@ #print "getdefault", section, name, "returned", repr(x) return x - def _replace_env(self, match): + def _do_replace_env(self, match, error=True): envkey = match.group('substitution_value') if not envkey: raise tox.exception.ConfigError( 'env: requires an environment variable name') if not envkey in os.environ: - raise tox.exception.ConfigError( - "substitution env:%r: unkown environment variable %r" % - (envkey, envkey)) + if error: + raise tox.exception.ConfigError( + "substitution env:%r: unkown environment variable %r" % + (envkey, envkey)) - return os.environ[envkey] + return os.environ.get(envkey, '') + + def _replace_env(self, match): + return self._do_replace_env(match) + + def _replace_env_no_error(self, match): + return self._do_replace_env(match, error=False) def _substitute_from_other_section(self, key): if key.startswith("[") and "]" in key: @@ -647,6 +654,7 @@ handlers = { 'env' : self._replace_env, + 'optionalenv' : self._replace_env_no_error, None : self._replace_substitution, } try: https://bitbucket.org/hpk42/tox/commits/29b0c53fc041/ Changeset: 29b0c53fc041 User: morgan_fainberg Date: 2014-03-19 08:18:30 Summary: Make optionalenv defenv to support default values Change the 'optionalenv' substitution to 'defenv' and support providing a default substitution value. Format is "defenv:DEFAULTVALUE:KEY". Affected #: 3 files diff -r cbcde59447f5e025f6ab08a38cbac7ac4dcfa27a -r 29b0c53fc0411d9894643ce4d7294e3dbeb15727 doc/config.txt --- a/doc/config.txt +++ b/doc/config.txt @@ -293,16 +293,24 @@ does not exist. -optional environment variable substitutions -++++++++++++++++++++++++++++++++++ +environment variable substitutions with default values +++++++++++++++++++++++++++++++++++++++++++++++++++++++ If you specify a substitution string like this:: - {optionalenv:KEY} + {defenv:DEFAULTVALUE:KEY} then the value will be retrieved as ``os.environ['KEY']`` -and replace with '' if the environment variable does not -exist +and replace with DEFAULTVALUE if the environment variable does not +exist. + +If you specify a substitution string like this:: + + {defenv::KEY} + +then the value will be retrieved as ``os.environ['KEY']`` +and replace with and empty string if the environment variable does not +exist. .. _`command positional substitution`: .. _`positional substitution`: diff -r cbcde59447f5e025f6ab08a38cbac7ac4dcfa27a -r 29b0c53fc0411d9894643ce4d7294e3dbeb15727 tests/test_config.py --- a/tests/test_config.py +++ b/tests/test_config.py @@ -242,17 +242,20 @@ py.test.raises(tox.exception.ConfigError, 'reader.getdefault("section", "key2")') - def test_getdefault_environment_optional_sub(self, monkeypatch, newconfig): + def test_getdefault_environment_substitution_with_default(self, monkeypatch, newconfig): monkeypatch.setenv("KEY1", "hello") config = newconfig(""" [section] - key1={optionalenv:KEY1} - key2={optionalenv:KEY2} + key1={defenv:DEFAULT_VALUE:KEY1} + key2={defenv:DEFAULT_VALUE:KEY2} + key3={defenv::KEY3} """) reader = IniReader(config._cfg) x = reader.getdefault("section", "key1") assert x == "hello" x = reader.getdefault("section", "key2") + assert x == "DEFAULT_VALUE" + x = reader.getdefault("section", "key3") assert x == "" def test_getdefault_other_section_substitution(self, newconfig): diff -r cbcde59447f5e025f6ab08a38cbac7ac4dcfa27a -r 29b0c53fc0411d9894643ce4d7294e3dbeb15727 tox/_config.py --- a/tox/_config.py +++ b/tox/_config.py @@ -595,25 +595,32 @@ #print "getdefault", section, name, "returned", repr(x) return x - def _do_replace_env(self, match, error=True): - envkey = match.group('substitution_value') + def _do_replace_env(self, envkey, default=None): if not envkey: raise tox.exception.ConfigError( 'env: requires an environment variable name') - if not envkey in os.environ: - if error: - raise tox.exception.ConfigError( - "substitution env:%r: unkown environment variable %r" % - (envkey, envkey)) + if not envkey in os.environ and default is None: + raise tox.exception.ConfigError( + "substitution env:%r: unkown environment variable %r" % + (envkey, envkey)) - return os.environ.get(envkey, '') + return os.environ.get(envkey, default) def _replace_env(self, match): - return self._do_replace_env(match) + envkey = match.group('substitution_value') + return self._do_replace_env(envkey) - def _replace_env_no_error(self, match): - return self._do_replace_env(match, error=False) + 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: @@ -654,7 +661,7 @@ handlers = { 'env' : self._replace_env, - 'optionalenv' : self._replace_env_no_error, + 'defenv' : self._replace_env_with_default, None : self._replace_substitution, } try: https://bitbucket.org/hpk42/tox/commits/08f051499562/ Changeset: 08f051499562 User: morgan_fainberg Date: 2014-03-25 23:29:38 Summary: 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. Affected #: 3 files diff -r 29b0c53fc0411d9894643ce4d7294e3dbeb15727 -r 08f05149956265b2bd85cb9609ce4153c1b8a39a doc/config.txt --- a/doc/config.txt +++ b/doc/config.txt @@ -298,7 +298,7 @@ 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 @@ 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 -r 29b0c53fc0411d9894643ce4d7294e3dbeb15727 -r 08f05149956265b2bd85cb9609ce4153c1b8a39a tests/test_config.py --- a/tests/test_config.py +++ b/tests/test_config.py @@ -246,9 +246,9 @@ 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 -r 29b0c53fc0411d9894643ce4d7294e3dbeb15727 -r 08f05149956265b2bd85cb9609ce4153c1b8a39a tox/_config.py --- a/tox/_config.py +++ b/tox/_config.py @@ -595,11 +595,20 @@ #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 @@ 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 @@ handlers = { 'env' : self._replace_env, - 'defenv' : self._replace_env_with_default, None : self._replace_substitution, } try: https://bitbucket.org/hpk42/tox/commits/0f763647663b/ Changeset: 0f763647663b User: hpk42 Date: 2014-05-10 12:08:50 Summary: Merged in morgan_fainberg/tox (pull request #86) Support optional ENV variable substitution in tox.ini Affected #: 3 files diff -r c07badfc0f02b5816403d3b5dc324c146708efb5 -r 0f763647663b72f85174897ac1fdbfd5026e97d2 doc/config.txt --- a/doc/config.txt +++ b/doc/config.txt @@ -284,6 +284,26 @@ and raise an Error if the environment variable does not exist. + +environment variable substitutions with default values +++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +If you specify a substitution string like this:: + + {env:KEY:DEFAULTVALUE} + +then the value will be retrieved as ``os.environ['KEY']`` +and replace with DEFAULTVALUE if the environment variable does not +exist. + +If you specify a substitution string like this:: + + {env:KEY:} + +then the value will be retrieved as ``os.environ['KEY']`` +and replace with and empty string if the environment variable does not +exist. + .. _`command positional substitution`: .. _`positional substitution`: diff -r c07badfc0f02b5816403d3b5dc324c146708efb5 -r 0f763647663b72f85174897ac1fdbfd5026e97d2 tests/test_config.py --- a/tests/test_config.py +++ b/tests/test_config.py @@ -241,6 +241,22 @@ py.test.raises(tox.exception.ConfigError, 'reader.getdefault("section", "key2")') + def test_getdefault_environment_substitution_with_default(self, monkeypatch, newconfig): + monkeypatch.setenv("KEY1", "hello") + config = newconfig(""" + [section] + key1={env:KEY1:DEFAULT_VALUE} + key2={env:KEY2:DEFAULT_VALUE} + key3={env:KEY3:} + """) + reader = IniReader(config._cfg) + x = reader.getdefault("section", "key1") + assert x == "hello" + x = reader.getdefault("section", "key2") + assert x == "DEFAULT_VALUE" + x = reader.getdefault("section", "key3") + assert x == "" + def test_getdefault_other_section_substitution(self, newconfig): config = newconfig(""" [section] diff -r c07badfc0f02b5816403d3b5dc324c146708efb5 -r 0f763647663b72f85174897ac1fdbfd5026e97d2 tox/_config.py --- a/tox/_config.py +++ b/tox/_config.py @@ -595,17 +595,25 @@ return x def _replace_env(self, match): - envkey = match.group('substitution_value') - if not envkey: + match_value = match.group('substitution_value') + if not match_value: raise tox.exception.ConfigError( 'env: requires an environment variable name') - if not envkey in os.environ: + 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" % (envkey, envkey)) - return os.environ[envkey] + return os.environ.get(envkey, default) def _substitute_from_other_section(self, key): if key.startswith("[") and "]" in key: Repository URL: https://bitbucket.org/hpk42/tox/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email. _______________________________________________ pytest-commit mailing list pytest-commit@python.org https://mail.python.org/mailman/listinfo/pytest-commit