322 |
322 |
323 dollar_groups = ('dollar', 'word1', 'word2') |
323 dollar_groups = ('dollar', 'word1', 'word2') |
324 |
324 |
325 def dollar_replace(match): |
325 def dollar_replace(match): |
326 """Called for each $replacement.""" |
326 """Called for each $replacement.""" |
327 # Only one of the groups will have matched, just get its text. |
327 # Only one of the dollar_groups will have matched, just get its text. |
328 word = next(g for g in match.group(*dollar_groups) if g) # pragma: always breaks |
328 word = next(g for g in match.group(*dollar_groups) if g) # pragma: always breaks |
329 if word == "$": |
329 if word == "$": |
330 return "$" |
330 return "$" |
331 elif word in variables: |
331 elif word in variables: |
332 return variables[word] |
332 return variables[word] |
333 elif match.group('strict'): |
333 elif match['strict']: |
334 msg = f"Variable {word} is undefined: {text!r}" |
334 msg = f"Variable {word} is undefined: {text!r}" |
335 raise CoverageException(msg) |
335 raise CoverageException(msg) |
336 else: |
336 else: |
337 return match.group('defval') |
337 return match['defval'] |
338 |
338 |
339 text = re.sub(dollar_pattern, dollar_replace, text) |
339 text = re.sub(dollar_pattern, dollar_replace, text) |
340 return text |
340 return text |
341 |
341 |
342 |
342 |