Commits

Ned Batchelder  committed 71e07f1

Refactor the reporting function into the SummaryReporter class.

  • Participants
  • Parent commits c71d462

Comments (0)

Files changed (3)

File coverage/cmdline.py

 
 import getopt, os, sys
 
+from coverage.summary import SummaryReporter
+
 USAGE = r"""
 Coverage version %(__version__)s
 
             omit = omit.split(',')
         
         if settings.get('report'):
-            self.coverage.report_engine(args, show_missing, ignore_errors, omit_prefixes=omit)
+            reporter = SummaryReporter(self.coverage, show_missing, ignore_errors)
+            reporter.report(args, omit_prefixes=omit)
         if settings.get('annotate'):
             self.coverage.annotate(args, directory, ignore_errors, omit_prefixes=omit)
 

File coverage/control.py

 
 import os, re, sys
 
+from coverage.codeunit import code_unit_factory
 from coverage.data import CoverageData
+from coverage.files import FileLocator
 from coverage.misc import format_lines, CoverageException
-from coverage.codeunit import code_unit_factory
-from coverage.files import FileLocator
+from coverage.summary import SummaryReporter
 
 class coverage:
     def __init__(self):
 
     # Programmatic entry point
     def report(self, morfs, show_missing=True, ignore_errors=False, file=None):
-        self.report_engine(morfs, show_missing=show_missing, ignore_errors=ignore_errors, outfile=file)
-
-    def report_engine(self, morfs, show_missing=True, ignore_errors=False, outfile=None, omit_prefixes=None):
-        morfs = morfs or self.data.executed_files()
-        code_units = code_unit_factory(morfs, self.file_locator, omit_prefixes)
-        code_units.sort()
-
-        max_name = max(5, max(map(lambda cu: len(cu.name), code_units)))
-        fmt_name = "%%- %ds  " % max_name
-        fmt_err = fmt_name + "%s: %s"
-        header = fmt_name % "Name" + " Stmts   Exec  Cover"
-        fmt_coverage = fmt_name + "% 6d % 6d % 5d%%"
-        if show_missing:
-            header = header + "   Missing"
-            fmt_coverage = fmt_coverage + "   %s"
-        if not outfile:
-            outfile = sys.stdout
-        print >>outfile, header
-        print >>outfile, "-" * len(header)
-        total_statements = 0
-        total_executed = 0
-        for cu in code_units:
-            try:
-                _, statements, _, missing, readable = self.analyze(cu)
-                n = len(statements)
-                m = n - len(missing)
-                if n > 0:
-                    pc = 100.0 * m / n
-                else:
-                    pc = 100.0
-                args = (cu.name, n, m, pc)
-                if show_missing:
-                    args = args + (readable,)
-                print >>outfile, fmt_coverage % args
-                total_statements = total_statements + n
-                total_executed = total_executed + m
-            except KeyboardInterrupt:                       #pragma: no cover
-                raise
-            except:
-                if not ignore_errors:
-                    typ, msg = sys.exc_info()[:2]
-                    print >>outfile, fmt_err % (cu.name, typ, msg)
-        if len(code_units) > 1:
-            print >>outfile, "-" * len(header)
-            if total_statements > 0:
-                pc = 100.0 * total_executed / total_statements
-            else:
-                pc = 100.0
-            args = ("TOTAL", total_statements, total_executed, pc)
-            if show_missing:
-                args = args + ("",)
-            print >>outfile, fmt_coverage % args
+        reporter = SummaryReporter(self, show_missing, ignore_errors)
+        reporter.report(morfs, outfile=file)
 
     # annotate(morfs, ignore_errors).
 

File coverage/summary.py

+"""Summary reporting"""
+
+import sys
+
+from coverage.codeunit import code_unit_factory
+
+class SummaryReporter:
+    """A reporter for writing the summary report."""
+    
+    def __init__(self, coverage, show_missing=True, ignore_errors=False):
+        self.cov = coverage
+        self.show_missing = show_missing
+        self.ignore_errors = ignore_errors
+
+    def report(self, morfs, omit_prefixes=None, outfile=None):
+        """Writes a report summarizing coverage statistics per module."""
+        
+        morfs = morfs or self.cov.data.executed_files()
+        code_units = code_unit_factory(morfs, self.cov.file_locator, omit_prefixes)
+        code_units.sort()
+
+        max_name = max(5, max(map(lambda cu: len(cu.name), code_units)))
+        fmt_name = "%%- %ds  " % max_name
+        fmt_err = fmt_name + "%s: %s"
+        header = fmt_name % "Name" + " Stmts   Exec  Cover"
+        fmt_coverage = fmt_name + "% 6d % 6d % 5d%%"
+        if self.show_missing:
+            header = header + "   Missing"
+            fmt_coverage = fmt_coverage + "   %s"
+        if not outfile:
+            outfile = sys.stdout
+        print >>outfile, header
+        print >>outfile, "-" * len(header)
+        total_statements = 0
+        total_executed = 0
+        for cu in code_units:
+            try:
+                _, statements, _, missing, readable = self.cov.analyze(cu)
+                n = len(statements)
+                m = n - len(missing)
+                if n > 0:
+                    pc = 100.0 * m / n
+                else:
+                    pc = 100.0
+                args = (cu.name, n, m, pc)
+                if self.show_missing:
+                    args = args + (readable,)
+                print >>outfile, fmt_coverage % args
+                total_statements = total_statements + n
+                total_executed = total_executed + m
+            except KeyboardInterrupt:                       #pragma: no cover
+                raise
+            except:
+                if not self.ignore_errors:
+                    typ, msg = sys.exc_info()[:2]
+                    print >>outfile, fmt_err % (cu.name, typ, msg)
+        if len(code_units) > 1:
+            print >>outfile, "-" * len(header)
+            if total_statements > 0:
+                pc = 100.0 * total_executed / total_statements
+            else:
+                pc = 100.0
+            args = ("TOTAL", total_statements, total_executed, pc)
+            if self.show_missing:
+                args = args + ("",)
+            print >>outfile, fmt_coverage % args