summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2009-11-28 14:01:13 -0500
committerNed Batchelder <ned@nedbatchelder.com>2009-11-28 14:01:13 -0500
commitbe8afd0bc3f173926fc751c50f9975543f301a91 (patch)
tree6bb3ce8ba6c4e20c75871f06786113e515cc0a1c
parent0fd1a4a46c59ddf5c018364226c5d8a1525aff8c (diff)
downloadpython-coveragepy-git-be8afd0bc3f173926fc751c50f9975543f301a91.tar.gz
Read a config file to get some of our configuration.
--HG-- branch : config
-rw-r--r--coverage/__init__.py2
-rw-r--r--coverage/config.py45
-rw-r--r--coverage/control.py39
-rw-r--r--test/test_api.py2
4 files changed, 67 insertions, 21 deletions
diff --git a/coverage/__init__.py b/coverage/__init__.py
index a41dafd3..9dea8004 100644
--- a/coverage/__init__.py
+++ b/coverage/__init__.py
@@ -5,7 +5,7 @@ http://nedbatchelder.com/code/coverage
"""
-__version__ = "3.2b4" # see detailed history in CHANGES.txt
+__version__ = "3.3-config" # see detailed history in CHANGES.txt
__url__ = "http://nedbatchelder.com/code/coverage"
diff --git a/coverage/config.py b/coverage/config.py
new file mode 100644
index 00000000..ca7bab61
--- /dev/null
+++ b/coverage/config.py
@@ -0,0 +1,45 @@
+"""Config file for coverage.py"""
+
+import ConfigParser, os
+
+
+class CoverageConfig(object):
+ def __init__(self):
+ # Defaults.
+ self.cover_pylib = False
+ self.timid = False
+ self.branch = False
+ self.exclude_list = ['# *pragma[: ]*[nN][oO] *[cC][oO][vV][eE][rR]']
+
+ def from_environment(self, env_var):
+ # Timidity: for nose users, read an environment variable. This is a
+ # cheap hack, since the rest of the command line arguments aren't
+ # recognized, but it solves some users' problems.
+ env = os.environ.get(env_var, '')
+ if env:
+ self.timid = ('--timid' in env)
+
+ def from_args(self, **kwargs):
+ for k, v in kwargs.items():
+ if v is not None:
+ setattr(self, k, v)
+
+ def from_file(self, *files):
+ cp = ConfigParser.RawConfigParser()
+ cp.read(files)
+
+ if cp.has_option('run', 'timid'):
+ self.timid = cp.getboolean('run', 'timid')
+ if cp.has_option('run', 'cover_pylib'):
+ self.cover_pylib = cp.getboolean('run', 'cover_pylib')
+ if cp.has_option('run', 'branch'):
+ self.branch = cp.getboolean('run', 'branch')
+ if cp.has_option('report', 'exclude'):
+ self.exclude_list = filter(None, cp.get('report', 'exclude').split('\n'))
+
+
+if __name__ == '__main__':
+ cc = CoverageConfig()
+ cc.from_file(".coveragerc", ".coverage.ini")
+ import pdb;pdb.set_trace()
+ print cc
diff --git a/coverage/control.py b/coverage/control.py
index 24fed7b9..349ff307 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -6,6 +6,7 @@ from coverage.annotate import AnnotateReporter
from coverage.backward import string_class # pylint: disable-msg=W0622
from coverage.codeunit import code_unit_factory, CodeUnit
from coverage.collector import Collector
+from coverage.config import CoverageConfig
from coverage.data import CoverageData
from coverage.files import FileLocator
from coverage.html import HtmlReporter
@@ -28,8 +29,8 @@ class coverage(object):
"""
- def __init__(self, data_file=None, data_suffix=False, cover_pylib=False,
- auto_data=False, timid=False, branch=False):
+ def __init__(self, data_file=None, data_suffix=False, cover_pylib=None,
+ auto_data=False, timid=None, branch=None):
"""
`data_file` is the base name of the data file to use, defaulting to
".coverage". `data_suffix` is appended to `data_file` to create the
@@ -54,20 +55,19 @@ class coverage(object):
"""
from coverage import __version__
- self.cover_pylib = cover_pylib
+ self.config = CoverageConfig()
+ self.config.from_environment('COVERAGE_OPTIONS')
+ self.config.from_args(cover_pylib=cover_pylib, timid=timid, branch=branch)
+
self.auto_data = auto_data
self.exclude_re = ""
- self.exclude_list = []
-
+ self._compile_exclude()
+
self.file_locator = FileLocator()
- # Timidity: for nose users, read an environment variable. This is a
- # cheap hack, since the rest of the command line arguments aren't
- # recognized, but it solves some users' problems.
- timid = timid or ('--timid' in os.environ.get('COVERAGE_OPTIONS', ''))
self.collector = Collector(
- self._should_trace, timid=timid, branch=branch
+ self._should_trace, timid=self.config.timid, branch=self.config.branch
)
# Create the data file.
@@ -83,11 +83,8 @@ class coverage(object):
collector="coverage v%s" % __version__
)
- # The default exclude pattern.
- self.exclude('# *pragma[: ]*[nN][oO] *[cC][oO][vV][eE][rR]')
-
# The prefix for files considered "installed with the interpreter".
- if not self.cover_pylib:
+ if not self.config.cover_pylib:
os_file = self.file_locator.canonical_filename(os.__file__)
self.pylib_prefix = os.path.split(os_file)[0]
@@ -123,7 +120,7 @@ class coverage(object):
# If we aren't supposed to trace installed code, then check if this is
# near the Python standard library and skip it if so.
- if not self.cover_pylib:
+ if not self.config.cover_pylib:
if canonical.startswith(self.pylib_prefix):
return False
@@ -182,7 +179,7 @@ class coverage(object):
def clear_exclude(self):
"""Clear the exclude list."""
- self.exclude_list = []
+ self.config.exclude_list = []
self.exclude_re = ""
def exclude(self, regex):
@@ -194,12 +191,16 @@ class coverage(object):
Matching any of the regexes excludes a source line.
"""
- self.exclude_list.append(regex)
- self.exclude_re = "(" + ")|(".join(self.exclude_list) + ")"
+ self.config.exclude_list.append(regex)
+ self._compile_exclude()
+
+ def _compile_exclude(self):
+ """Build the internal usable form of the exclude list."""
+ self.exclude_re = "(" + ")|(".join(self.config.exclude_list) + ")"
def get_exclude_list(self):
"""Return the list of excluded regex patterns."""
- return self.exclude_list
+ return self.config.exclude_list
def save(self):
"""Save the collected coverage data to the data file."""
diff --git a/test/test_api.py b/test/test_api.py
index 932606fd..270c7235 100644
--- a/test/test_api.py
+++ b/test/test_api.py
@@ -161,7 +161,7 @@ class ApiTest(CoverageTest):
# Measure without the stdlib.
cov1 = coverage.coverage()
- self.assertEqual(cov1.cover_pylib, False)
+ self.assertEqual(cov1.config.cover_pylib, False)
cov1.start()
self.import_module("mymain")
cov1.stop()