summaryrefslogtreecommitdiff
path: root/coverage/plugin.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-01-09 21:03:09 -0500
committerNed Batchelder <ned@nedbatchelder.com>2015-01-09 21:03:09 -0500
commit2d16670aeeb6588aac8a260bc1b24f4035b149cb (patch)
tree2fc6721b55325d24469a0bf7122f6f97c68ad8e7 /coverage/plugin.py
parent35fda609e4dc07d0b6d47deb8746eec3ac3729b4 (diff)
downloadpython-coveragepy-2d16670aeeb6588aac8a260bc1b24f4035b149cb.tar.gz
Move base-class logic from CodeUnit to FileReporter
Diffstat (limited to 'coverage/plugin.py')
-rw-r--r--coverage/plugin.py52
1 files changed, 52 insertions, 0 deletions
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('.', '_')