diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2018-09-08 16:56:08 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2018-09-08 20:12:20 -0400 |
commit | 69d106fec5ac2e9aba1146c0004d961e8cb903f5 (patch) | |
tree | 25e385c4ed07dcf0b7d73e6c81bedc939b916baf /coverage/misc.py | |
parent | c001d1676de46aef12f80a21675937756e594acf (diff) | |
download | python-coveragepy-git-69d106fec5ac2e9aba1146c0004d961e8cb903f5.tar.gz |
Defaultable variable substitution
Diffstat (limited to 'coverage/misc.py')
-rw-r--r-- | coverage/misc.py | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/coverage/misc.py b/coverage/misc.py index 037332f5..7b8fbb93 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -258,7 +258,8 @@ def substitute_variables(text, variables=os.environ): $VAR ${VAR} - ${VAR?} strict: an error if VAR isn't defined. + ${VAR?} strict: an error if VAR isn't defined. + ${VAR-missing} defaulted: "missing" if VAR isn't defined. A dollar can be inserted with ``$$``. @@ -280,16 +281,20 @@ def substitute_variables(text, variables=os.environ): if word not in variables: msg = "Variable {} is undefined: {}".format(word, text) raise CoverageException(msg) - return variables.get(word, '') + return variables.get(word, m.group('defval') or '') dollar_pattern = r"""(?x) # Use extended regex syntax \$(?: # A dollar sign, then (?P<v1>\w+) | # a plain word, + (?P<char>\$) | # or a dollar sign. { # or a {-wrapped word, (?P<v2>\w+) - (?P<strict>\??) # with maybe a strict marker - } | - (?P<char>[$]) # or a dollar sign. + (?: + (?P<strict>\?) # with a strict marker + | + -(?P<defval>[^}]*) # or a default value + )? + } ) """ text = re.sub(dollar_pattern, dollar_replace, text) |