diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2012-11-25 17:16:59 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2012-11-25 17:16:59 -0500 |
commit | 86eb1db2365143d025fa64a67587c1723977e787 (patch) | |
tree | c05a2692889c132ce9a7eaea0fec2d3f29873fe3 /coverage/config.py | |
parent | 34fdddd0dae03ab57289dfe5807158e672f7f9ef (diff) | |
download | python-coveragepy-86eb1db2365143d025fa64a67587c1723977e787.tar.gz |
Config files now can use environment variables. #97.
Diffstat (limited to 'coverage/config.py')
-rw-r--r-- | coverage/config.py | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/coverage/config.py b/coverage/config.py index 8f1f671..8edd6c6 100644 --- a/coverage/config.py +++ b/coverage/config.py @@ -1,6 +1,6 @@ """Config file for coverage.py""" -import os, sys +import os, re, sys from coverage.backward import string_class, iitems # In py3, # ConfigParser was renamed to the more-standard configparser @@ -10,7 +10,7 @@ except ImportError: import ConfigParser as configparser -class HandyConfigParser(configparser.ConfigParser): +class HandyConfigParser(configparser.RawConfigParser): """Our specialization of ConfigParser.""" def read(self, filename): @@ -18,7 +18,28 @@ class HandyConfigParser(configparser.ConfigParser): kwargs = {} if sys.version_info >= (3, 2): kwargs['encoding'] = "utf-8" - configparser.ConfigParser.read(self, filename, **kwargs) + configparser.RawConfigParser.read(self, filename, **kwargs) + + def get(self, *args, **kwargs): + v = configparser.RawConfigParser.get(self, *args, **kwargs) + def dollar_replace(m): + """Called for each $replacement.""" + # Only one of the groups will have matched, just get its text. + word = [w for w in m.groups() if w is not None][0] + if word == "$": + return "$" + else: + return os.environ.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. + ) + """ + v = re.sub(dollar_pattern, dollar_replace, v) + return v def getlist(self, section, option): """Read a list of strings. |