summaryrefslogtreecommitdiff
path: root/coverage
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-01-01 12:16:30 -0500
committerNed Batchelder <ned@nedbatchelder.com>2015-01-01 12:16:30 -0500
commitea231af0bbb3aa1eed780ece6c567857845d7c96 (patch)
tree5eacdef7a96b5a3063b75009346a4de8cc99ec52 /coverage
parentb88a6b2d44ef513503547fe2908fb41d298e87ec (diff)
downloadpython-coveragepy-git-ea231af0bbb3aa1eed780ece6c567857845d7c96.tar.gz
Move code_unit_factory into Coverage
Diffstat (limited to 'coverage')
-rw-r--r--coverage/codeunit.py62
-rw-r--r--coverage/control.py59
-rw-r--r--coverage/report.py6
3 files changed, 51 insertions, 76 deletions
diff --git a/coverage/codeunit.py b/coverage/codeunit.py
index a607ae1a..e9efa396 100644
--- a/coverage/codeunit.py
+++ b/coverage/codeunit.py
@@ -3,72 +3,12 @@
import os
import sys
-from coverage.backward import string_class, unicode_class
+from coverage.backward import unicode_class
from coverage.files import get_python_source, FileLocator
-from coverage.misc import CoverageException
from coverage.parser import PythonParser
from coverage.phystokens import source_token_lines, source_encoding
-def code_units_factory(morfs, file_locator=None, get_plugin=None):
- """Construct a list of CodeUnits from modules or filenames.
-
- `morfs` is a module or filename, or a list of the same.
-
- `file_locator` is a FileLocator that can help resolve filenames.
-
- `get_plugin` is a function taking a filename, and returning a plugin
- responsible for the file. It can also return None if there is no plugin
- claiming the file.
-
- Returns a list of CodeUnit objects.
-
- """
- # Be sure we have a list.
- if not isinstance(morfs, (list, tuple)):
- morfs = [morfs]
-
- code_units = []
- for morf in morfs:
- file_reporter = code_unit_factory(morf, file_locator, get_plugin)
- code_units.append(file_reporter)
-
- return code_units
-
-
-def code_unit_factory(morf, file_locator=None, get_plugin=None):
- """Construct a CodeUnit from a module or filename.
-
- `morfs` is a module or a filename.
-
- `file_locator` is a FileLocator that can help resolve filenames.
-
- `get_plugin` is a function taking a filename, and returning a plugin
- responsible for the file. It can also return None if there is no plugin
- claiming the file.
-
- Returns a CodeUnit object.
-
- """
- plugin = None
-
- if isinstance(morf, string_class) and get_plugin:
- plugin = get_plugin(morf)
-
- if plugin:
- file_reporter = plugin.file_reporter(morf)
- if file_reporter is None:
- raise CoverageException(
- "Plugin %r did not provide a file reporter for %r." % (
- plugin.plugin_name, morf
- )
- )
- else:
- file_reporter = PythonCodeUnit(morf, file_locator)
-
- return file_reporter
-
-
class CodeUnit(object):
"""Code unit: a filename or module.
diff --git a/coverage/control.py b/coverage/control.py
index 95592508..0ca1e95c 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -10,7 +10,7 @@ import sys
from coverage.annotate import AnnotateReporter
from coverage.backward import string_class, iitems
-from coverage.codeunit import code_unit_factory, CodeUnit, PythonCodeUnit
+from coverage.codeunit import CodeUnit, PythonCodeUnit
from coverage.collector import Collector
from coverage.config import CoverageConfig
from coverage.data import CoverageData
@@ -750,20 +750,59 @@ class Coverage(object):
Returns an `Analysis` object.
"""
- def get_plugin(filename):
- """For code_unit_factory to use to find the plugin for a file."""
- plugin = None
- plugin_name = self.data.plugin_data().get(filename)
- if plugin_name:
- plugin = self.plugins.get(plugin_name)
- return plugin
-
self._harvest_data()
if not isinstance(it, CodeUnit):
- it = code_unit_factory(it, self.file_locator, get_plugin)
+ it = self._get_file_reporter(it)
return Analysis(self, it)
+ def _get_file_reporter(self, morf):
+ """Get a FileReporter for a module or filename."""
+ plugin = None
+
+ if isinstance(morf, string_class):
+ plugin_name = self.data.plugin_data().get(morf)
+ if plugin_name:
+ plugin = self.plugins.get(plugin_name)
+
+ if plugin:
+ file_reporter = plugin.file_reporter(morf)
+ if file_reporter is None:
+ raise CoverageException(
+ "Plugin %r did not provide a file reporter for %r." % (
+ plugin.plugin_name, morf
+ )
+ )
+ else:
+ file_reporter = PythonCodeUnit(morf, self.file_locator)
+
+ return file_reporter
+
+ def _get_file_reporters(self, morfs=None):
+ """Get a list of FileReporters for a list of modules or filenames.
+
+ For each module or filename in `morfs`, find a FileReporter. Return
+ the list of FileReporters.
+
+ If `morfs` is a single module or filename, this returns a list of one
+ FileReporter. If `morfs` is empty or None, then the list of all files
+ measured is used to find the FileReporters.
+
+ """
+ if not morfs:
+ morfs = self.data.measured_files()
+
+ # Be sure we have a list.
+ if not isinstance(morfs, (list, tuple)):
+ morfs = [morfs]
+
+ file_reporters = []
+ for morf in morfs:
+ file_reporter = self._get_file_reporter(morf)
+ file_reporters.append(file_reporter)
+
+ return file_reporters
+
def report(
self, morfs=None, show_missing=True, ignore_errors=None,
file=None, # pylint: disable=redefined-builtin
diff --git a/coverage/report.py b/coverage/report.py
index 85d552e1..93b6c928 100644
--- a/coverage/report.py
+++ b/coverage/report.py
@@ -2,7 +2,6 @@
import os
-from coverage.codeunit import code_units_factory
from coverage.files import prep_patterns, FnmatchMatcher
from coverage.misc import CoverageException, NoSource, NotPython
@@ -33,10 +32,7 @@ class Reporter(object):
`morfs` is a list of modules or filenames.
"""
- morfs = morfs or self.coverage.data.measured_files()
- file_locator = self.coverage.file_locator
- get_plugin = self.coverage.data.plugin_data().get
- self.code_units = code_units_factory(morfs, file_locator, get_plugin)
+ self.code_units = self.coverage._get_file_reporters(morfs)
if self.config.include:
patterns = prep_patterns(self.config.include)