|
1 """Summary reporting""" |
|
2 |
|
3 import sys |
|
4 |
|
5 from report import Reporter |
|
6 |
|
7 |
|
8 class SummaryReporter(Reporter): |
|
9 """A reporter for writing the summary report.""" |
|
10 |
|
11 def __init__(self, coverage, show_missing=True, ignore_errors=False): |
|
12 super(SummaryReporter, self).__init__(coverage, ignore_errors) |
|
13 self.show_missing = show_missing |
|
14 |
|
15 def report(self, morfs, omit_prefixes=None, outfile=None): |
|
16 """Writes a report summarizing coverage statistics per module.""" |
|
17 |
|
18 self.find_code_units(morfs, omit_prefixes) |
|
19 |
|
20 # Prepare the formatting strings |
|
21 max_name = max([len(cu.name) for cu in self.code_units] + [5]) |
|
22 fmt_name = "%%- %ds " % max_name |
|
23 fmt_err = "%s %s: %s\n" |
|
24 header = fmt_name % "Name" + " Stmts Exec Cover\n" |
|
25 fmt_coverage = fmt_name + "% 6d % 6d % 5d%%\n" |
|
26 if self.show_missing: |
|
27 header = header.replace("\n", " Missing\n") |
|
28 fmt_coverage = fmt_coverage.replace("\n", " %s\n") |
|
29 rule = "-" * (len(header)-1) + "\n" |
|
30 |
|
31 if not outfile: |
|
32 outfile = sys.stdout |
|
33 |
|
34 # Write the header |
|
35 outfile.write(header) |
|
36 outfile.write(rule) |
|
37 |
|
38 total_statements = 0 |
|
39 total_executed = 0 |
|
40 total_units = 0 |
|
41 |
|
42 for cu in self.code_units: |
|
43 try: |
|
44 statements, _, missing, readable = self.coverage._analyze(cu) |
|
45 n = len(statements) |
|
46 m = n - len(missing) |
|
47 if n > 0: |
|
48 pc = 100.0 * m / n |
|
49 else: |
|
50 pc = 100.0 |
|
51 args = (cu.name, n, m, pc) |
|
52 if self.show_missing: |
|
53 args = args + (readable,) |
|
54 outfile.write(fmt_coverage % args) |
|
55 total_units += 1 |
|
56 total_statements = total_statements + n |
|
57 total_executed = total_executed + m |
|
58 except KeyboardInterrupt: #pragma: no cover |
|
59 raise |
|
60 except: |
|
61 if not self.ignore_errors: |
|
62 typ, msg = sys.exc_info()[:2] |
|
63 outfile.write(fmt_err % (cu.name, typ.__name__, msg)) |
|
64 |
|
65 if total_units > 1: |
|
66 outfile.write(rule) |
|
67 if total_statements > 0: |
|
68 pc = 100.0 * total_executed / total_statements |
|
69 else: |
|
70 pc = 100.0 |
|
71 args = ("TOTAL", total_statements, total_executed, pc) |
|
72 if self.show_missing: |
|
73 args = args + ("",) |
|
74 outfile.write(fmt_coverage % args) |