From 69d106fec5ac2e9aba1146c0004d961e8cb903f5 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 8 Sep 2018 16:56:08 -0400 Subject: Defaultable variable substitution --- coverage/misc.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'coverage/misc.py') 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\w+) | # a plain word, + (?P\$) | # or a dollar sign. { # or a {-wrapped word, (?P\w+) - (?P\??) # with maybe a strict marker - } | - (?P[$]) # or a dollar sign. + (?: + (?P\?) # with a strict marker + | + -(?P[^}]*) # or a default value + )? + } ) """ text = re.sub(dollar_pattern, dollar_replace, text) -- cgit v1.2.1