diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2018-09-08 16:17:09 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2018-09-08 16:17:09 -0400 |
commit | dde0a3ef3b88eb79bff8a36943cf934452eb8c26 (patch) | |
tree | da7de4d5c688bc1b1ee5e85f9371de032d6661d4 /coverage/misc.py | |
parent | f4a99853c8dc38a2feafcd0d575a99633786d22d (diff) | |
download | python-coveragepy-git-dde0a3ef3b88eb79bff8a36943cf934452eb8c26.tar.gz |
Move variable substitution to be independent
Diffstat (limited to 'coverage/misc.py')
-rw-r--r-- | coverage/misc.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/coverage/misc.py b/coverage/misc.py index ab950846..e2031852 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -8,6 +8,7 @@ import hashlib import inspect import locale import os +import re import sys import types @@ -250,6 +251,42 @@ def _needs_to_implement(that, func_name): ) +def substitute_variables(text, variables=os.environ): + """Substitute ``${VAR}`` variables in `text` with their values. + + Variables in the text can take a number of shell-inspired forms:: + + $VAR + ${VAR} + + A dollar can be inserted with ``$$``. + + `variables` is a dictionary of variable values, defaulting to the + environment variables. + + Returns the resulting text with values substituted. + + """ + def dollar_replace(m): + """Called for each $replacement.""" + # Only one of the groups will have matched, just get its text. + word = next(w for w in m.groups() if w is not None) # pragma: part covered + if word == "$": + return "$" + else: + return variables.get(word, '') + + dollar_pattern = r"""(?x) # Use extended regex syntax + \$(?: # A dollar sign, then + (?P<v1>\w+) | # a plain word, + {(?P<v2>\w+)} | # or a {-wrapped word, + (?P<char>[$]) # or a dollar sign. + ) + """ + text = re.sub(dollar_pattern, dollar_replace, text) + return text + + class BaseCoverageException(Exception): """The base of all Coverage exceptions.""" pass |