From be8afd0bc3f173926fc751c50f9975543f301a91 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 28 Nov 2009 14:01:13 -0500 Subject: Read a config file to get some of our configuration. --HG-- branch : config --- coverage/control.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'coverage/control.py') 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.""" -- cgit v1.2.1 From 81a363edf04f614af736ce32d79791a210e1e1fd Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 28 Nov 2009 16:59:58 -0500 Subject: Read .coveragerc by default. --HG-- branch : config --- coverage/control.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'coverage/control.py') diff --git a/coverage/control.py b/coverage/control.py index 349ff307..6e18b058 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -30,7 +30,7 @@ class coverage(object): """ def __init__(self, data_file=None, data_suffix=False, cover_pylib=None, - auto_data=False, timid=None, branch=None): + auto_data=False, timid=None, branch=None, config_file=True): """ `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 @@ -51,14 +51,26 @@ class coverage(object): If `branch` is true, then branch coverage will be measured in addition to the usual statement coverage. + + `config_file` determines what config file to read. If it is a string, + it is the name of the config file to read. If it is True, then a + standard file is read (".coveragerc"). If it is False, then no file is + read. """ from coverage import __version__ - + + # Build our configuration from a number of sources. self.config = CoverageConfig() + if config_file: + if config_file is True: + config_file = ".coveragerc" + self.config.from_file(config_file) self.config.from_environment('COVERAGE_OPTIONS') - self.config.from_args(cover_pylib=cover_pylib, timid=timid, branch=branch) - + self.config.from_args( + cover_pylib=cover_pylib, timid=timid, branch=branch + ) + self.auto_data = auto_data self.exclude_re = "" @@ -67,7 +79,8 @@ class coverage(object): self.file_locator = FileLocator() self.collector = Collector( - self._should_trace, timid=self.config.timid, branch=self.config.branch + self._should_trace, timid=self.config.timid, + branch=self.config.branch ) # Create the data file. -- cgit v1.2.1 From 49ede2bdf54a12e4eaa9472785cc5544040c077a Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 28 Nov 2009 17:30:59 -0500 Subject: Test the config file support. --HG-- branch : config --- coverage/control.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'coverage/control.py') diff --git a/coverage/control.py b/coverage/control.py index f947816b..121f52b7 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -3,7 +3,7 @@ import os, socket from coverage.annotate import AnnotateReporter -from coverage.backward import string_class # pylint: disable-msg=W0622 +from coverage.backward import string_class from coverage.codeunit import code_unit_factory, CodeUnit from coverage.collector import Collector from coverage.config import CoverageConfig -- cgit v1.2.1 From c72bfe70f9f8ac6d573689798b364bd664b93444 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 28 Nov 2009 20:24:23 -0500 Subject: Add data_file to the .coveragerc file. --HG-- branch : config --- coverage/control.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'coverage/control.py') diff --git a/coverage/control.py b/coverage/control.py index 121f52b7..77678543 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -68,7 +68,8 @@ class coverage(object): self.config.from_file(config_file) self.config.from_environment('COVERAGE_OPTIONS') self.config.from_args( - cover_pylib=cover_pylib, timid=timid, branch=branch + data_file=data_file, cover_pylib=cover_pylib, timid=timid, + branch=branch ) self.auto_data = auto_data @@ -92,7 +93,7 @@ class coverage(object): data_suffix = None self.data = CoverageData( - basename=data_file, suffix=data_suffix, + basename=self.config.data_file, suffix=data_suffix, collector="coverage v%s" % __version__ ) -- cgit v1.2.1 From b8b1aaed4d6eaeb8cab1afa2449863fe2bd91313 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 28 Nov 2009 21:10:24 -0500 Subject: Read the data file from the COVERAGE_FILE environment variable. --HG-- branch : config --- coverage/control.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'coverage/control.py') diff --git a/coverage/control.py b/coverage/control.py index 77678543..eb134450 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -60,13 +60,23 @@ class coverage(object): """ from coverage import __version__ - # Build our configuration from a number of sources. + # Build our configuration from a number of sources: + # 1: defaults: self.config = CoverageConfig() + + # 2: from the coveragerc file: if config_file: if config_file is True: config_file = ".coveragerc" self.config.from_file(config_file) + + # 3: from environment variables: self.config.from_environment('COVERAGE_OPTIONS') + env_data_file = os.environ.get('COVERAGE_FILE') + if env_data_file: + self.config.data_file = env_data_file + + # 4: from constructor arguments: self.config.from_args( data_file=data_file, cover_pylib=cover_pylib, timid=timid, branch=branch -- cgit v1.2.1 From 36e40f58a476e6d7ddf3746a31d0e96667bfd66e Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Thu, 3 Dec 2009 08:59:29 -0500 Subject: Clean trailing whitespace. --HG-- branch : config --- coverage/control.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'coverage/control.py') diff --git a/coverage/control.py b/coverage/control.py index 39e3dfbb..c1d32e9d 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -3,7 +3,7 @@ import atexit, os, socket from coverage.annotate import AnnotateReporter -from coverage.backward import string_class +from coverage.backward import string_class from coverage.codeunit import code_unit_factory, CodeUnit from coverage.collector import Collector from coverage.config import CoverageConfig @@ -51,7 +51,7 @@ class coverage(object): If `branch` is true, then branch coverage will be measured in addition to the usual statement coverage. - + `config_file` determines what config file to read. If it is a string, it is the name of the config file to read. If it is True, then a standard file is read (".coveragerc"). If it is False, then no file is @@ -69,13 +69,13 @@ class coverage(object): if config_file is True: config_file = ".coveragerc" self.config.from_file(config_file) - + # 3: from environment variables: self.config.from_environment('COVERAGE_OPTIONS') env_data_file = os.environ.get('COVERAGE_FILE') if env_data_file: self.config.data_file = env_data_file - + # 4: from constructor arguments: self.config.from_args( data_file=data_file, cover_pylib=cover_pylib, timid=timid, @@ -226,7 +226,7 @@ class coverage(object): """ 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) + ")" -- cgit v1.2.1