diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-01-09 21:03:09 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-01-09 21:03:09 -0500 |
commit | 2d16670aeeb6588aac8a260bc1b24f4035b149cb (patch) | |
tree | 2fc6721b55325d24469a0bf7122f6f97c68ad8e7 | |
parent | 35fda609e4dc07d0b6d47deb8746eec3ac3729b4 (diff) | |
download | python-coveragepy-2d16670aeeb6588aac8a260bc1b24f4035b149cb.tar.gz |
Move base-class logic from CodeUnit to FileReporter
-rw-r--r-- | coverage/codeunit.py | 50 | ||||
-rw-r--r-- | coverage/control.py | 4 | ||||
-rw-r--r-- | coverage/html.py | 5 | ||||
-rw-r--r-- | coverage/plugin.py | 52 |
4 files changed, 59 insertions, 52 deletions
diff --git a/coverage/codeunit.py b/coverage/codeunit.py index b2c9a71..4e64bb0 100644 --- a/coverage/codeunit.py +++ b/coverage/codeunit.py @@ -4,9 +4,10 @@ import os from coverage.backward import unicode_class from coverage.files import FileLocator +from coverage.plugin import FileReporter -class CodeUnit(object): +class CodeUnit(FileReporter): """Code unit: a filename or module. Instance attributes: @@ -42,38 +43,10 @@ class CodeUnit(object): self.name = n self.modname = modname - def __repr__(self): - return ( - "<{self.__class__.__name__}" - " name={self.name!r}" - " filename={self.filename!r}>".format(self=self) - ) - def _adjust_filename(self, f): # TODO: This shouldn't be in the base class, right? return f - # Annoying comparison operators. Py3k wants __lt__ etc, and Py2k needs all - # of them defined. - - def __lt__(self, other): - return self.name < other.name - - def __le__(self, other): - return self.name <= other.name - - def __eq__(self, other): - return self.name == other.name - - def __ne__(self, other): - return self.name != other.name - - def __gt__(self, other): - return self.name > other.name - - def __ge__(self, other): - return self.name >= other.name - def flat_rootname(self): """A base for a flat filename to correspond to this code unit. @@ -89,22 +62,3 @@ class CodeUnit(object): else: root = os.path.splitdrive(self.name)[1] return root.replace('\\', '_').replace('/', '_').replace('.', '_') - - def source(self): - """Return the source for the code, a Unicode string.""" - return unicode_class("???") - - def source_token_lines(self): - """Return the 'tokenized' text for the code.""" - # A generic implementation, each line is one "txt" token. - for line in self.source().splitlines(): - yield [('txt', line)] - - def should_be_python(self): - """Does it seem like this file should contain Python? - - This is used to decide if a file reported as part of the execution of - a program was really likely to have contained Python in the first - place. - """ - return False diff --git a/coverage/control.py b/coverage/control.py index 59d9e89..63c9d38 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -10,7 +10,6 @@ import sys from coverage.annotate import AnnotateReporter from coverage.backward import string_class, iitems -from coverage.codeunit import CodeUnit from coverage.collector import Collector from coverage.config import CoverageConfig from coverage.data import CoverageData @@ -22,6 +21,7 @@ from coverage.files import ModuleMatcher from coverage.html import HtmlReporter from coverage.misc import CoverageException, bool_or_none, join_regex from coverage.misc import file_be_gone, overrides +from coverage.plugin import FileReporter from coverage.python import PythonCodeUnit from coverage.results import Analysis, Numbers from coverage.summary import SummaryReporter @@ -752,7 +752,7 @@ class Coverage(object): """ self._harvest_data() - if not isinstance(it, CodeUnit): + if not isinstance(it, FileReporter): it = self._get_file_reporter(it) return Analysis(self, it) diff --git a/coverage/html.py b/coverage/html.py index 9872708..ec151b4 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -229,11 +229,12 @@ class HtmlReporter(Reporter): }) # Write the HTML page for this file. - html = spaceless(self.source_tmpl.render({ + template_values = { 'c_exc': c_exc, 'c_mis': c_mis, 'c_par': c_par, 'c_run': c_run, 'arcs': self.arcs, 'extra_css': self.extra_css, 'cu': cu, 'nums': nums, 'lines': lines, - })) + } + html = spaceless(self.source_tmpl.render(template_values)) html_filename = flat_rootname + ".html" html_path = os.path.join(self.directory, html_filename) diff --git a/coverage/plugin.py b/coverage/plugin.py index dd4ebfb..0e741a9 100644 --- a/coverage/plugin.py +++ b/coverage/plugin.py @@ -143,6 +143,33 @@ class FileReporter(object): def __init__(self, filename): self.filename = filename + def __repr__(self): + return ( + "<{self.__class__.__name__}" + " filename={self.filename!r}>".format(self=self) + ) + + # Annoying comparison operators. Py3k wants __lt__ etc, and Py2k needs all + # of them defined. + + def __lt__(self, other): + return self.filename < other.filename + + def __le__(self, other): + return self.filename <= other.filename + + def __eq__(self, other): + return self.filename == other.filename + + def __ne__(self, other): + return self.filename != other.filename + + def __gt__(self, other): + return self.filename > other.filename + + def __ge__(self, other): + return self.filename >= other.filename + def statements(self): _needs_to_implement(self, "statements") @@ -160,3 +187,28 @@ class FileReporter(object): def arcs(self): return [] + + def source(self): + """Return the source for the code, a Unicode string.""" + # A generic implementation: read the text of self.filename + with open(self.filename) as f: + return f.read() + + def source_token_lines(self): + """Return the 'tokenized' text for the code.""" + # A generic implementation, each line is one "txt" token. + for line in self.source().splitlines(): + yield [('txt', line)] + + def should_be_python(self): + """Does it seem like this file should contain Python? + + This is used to decide if a file reported as part of the execution of + a program was really likely to have contained Python in the first + place. + """ + return False + + def flat_rootname(self): + # TODO: a better generic implementation? + return self.filename.replace('\\', '_').replace('/', '_').replace('.', '_') |