summaryrefslogtreecommitdiff
path: root/coverage
diff options
context:
space:
mode:
Diffstat (limited to 'coverage')
-rw-r--r--coverage/config.py36
-rw-r--r--coverage/control.py9
2 files changed, 37 insertions, 8 deletions
diff --git a/coverage/config.py b/coverage/config.py
index e5e35856..064bc1ca 100644
--- a/coverage/config.py
+++ b/coverage/config.py
@@ -13,6 +13,11 @@ except ImportError:
class HandyConfigParser(configparser.RawConfigParser):
"""Our specialization of ConfigParser."""
+ def __init__(self, section_prefix):
+ # pylint: disable=super-init-not-called
+ configparser.RawConfigParser.__init__(self)
+ self.section_prefix = section_prefix
+
def read(self, filename):
"""Read a filename as UTF-8 configuration data."""
kwargs = {}
@@ -20,7 +25,19 @@ class HandyConfigParser(configparser.RawConfigParser):
kwargs['encoding'] = "utf-8"
return configparser.RawConfigParser.read(self, filename, **kwargs)
- def get(self, *args, **kwargs):
+ def has_option(self, section, option):
+ section = self.section_prefix + section
+ return configparser.RawConfigParser.has_option(self, section, option)
+
+ def has_section(self, section):
+ section = self.section_prefix + section
+ return configparser.RawConfigParser.has_section(self, section)
+
+ def options(self, section):
+ section = self.section_prefix + section
+ return configparser.RawConfigParser.options(self, section)
+
+ def get(self, section, *args, **kwargs):
"""Get a value, replacing environment variables also.
The arguments are the same as `RawConfigParser.get`, but in the found
@@ -30,7 +47,8 @@ class HandyConfigParser(configparser.RawConfigParser):
Returns the finished value.
"""
- v = configparser.RawConfigParser.get(self, *args, **kwargs)
+ section = self.section_prefix + section
+ v = configparser.RawConfigParser.get(self, section, *args, **kwargs)
def dollar_replace(m):
"""Called for each $replacement."""
# Only one of the groups will have matched, just get its text.
@@ -164,18 +182,22 @@ class CoverageConfig(object):
v = [v]
setattr(self, k, v)
- def from_file(self, filename):
+ def from_file(self, filename, section_prefix=""):
"""Read configuration from a .rc file.
`filename` is a file name to read.
+ Returns True or False, whether the file could be read.
+
"""
self.attempted_config_files.append(filename)
- cp = HandyConfigParser()
+ cp = HandyConfigParser(section_prefix)
files_read = cp.read(filename)
- if files_read is not None: # return value changed in 2.4
- self.config_files.extend(files_read)
+ if not files_read:
+ return False
+
+ self.config_files.extend(files_read)
for option_spec in self.CONFIG_FILE_OPTIONS:
self.set_attr_from_config_option(cp, *option_spec)
@@ -185,6 +207,8 @@ class CoverageConfig(object):
for option in cp.options('paths'):
self.paths[option] = cp.getlist('paths', option)
+ return True
+
CONFIG_FILE_OPTIONS = [
# These are *args for set_attr_from_config_option:
# (attr, where, type_="")
diff --git a/coverage/control.py b/coverage/control.py
index 19b68ca0..cb917e52 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -99,17 +99,22 @@ class coverage(object):
# 1: defaults:
self.config = CoverageConfig()
- # 2: from the coveragerc file:
+ # 2: from the .coveragerc or setup.cfg file:
if config_file:
+ did_read_rc = should_read_setupcfg = False
if config_file is True:
config_file = ".coveragerc"
+ should_read_setupcfg = True
try:
- self.config.from_file(config_file)
+ did_read_rc = self.config.from_file(config_file)
except ValueError as err:
raise CoverageException(
"Couldn't read config file %s: %s" % (config_file, err)
)
+ if not did_read_rc and should_read_setupcfg:
+ self.config.from_file("setup.cfg", section_prefix="coverage:")
+
# 3: from environment variables:
self.config.from_environment('COVERAGE_OPTIONS')
env_data_file = os.environ.get('COVERAGE_FILE')