ColorSummaryReporter   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 23

4 Methods

Rating   Name   Duplication   Size   Complexity  
F report() 0 58 14
A header() 0 8 2
A fmt_name() 0 5 2
B fmt_coverage() 0 17 5
1
'''
2
Created on Sep 23, 2013
3
4
@author: sean
5
'''
6
from coverage.summary import SummaryReporter
7
from coverage.results import Numbers
8
from coverage.misc import NotPython
9
import sys
10
11
WARNING = '\033[33m'
12
OKBLUE = '\033[34m'
13
OKGREEN = '\033[32m'
14
FAIL = '\033[31m'
15
ENDC = '\033[0m'
16
BOLD = "\033[1m"
17
18
def green(text):
19
    return BOLD + OKGREEN + text + ENDC
20
21
def red(text):
22
    return FAIL + text + ENDC
23
24
def orange(text):
25
    return WARNING + text + ENDC
26
27
def blue(text):
28
    return OKBLUE + text + ENDC
29
30
class ColorSummaryReporter(SummaryReporter):
31
    
32
    @property    
33
    def fmt_name(self):
34
        max_name = max([len(cu.name) for cu in self.code_units] + [5])
35
        fmt_name = "%%- %ds  " % max_name
36
        return fmt_name
37
38
    def fmt_coverage(self, perc):
39
        
40
        fmt_coverage = self.fmt_name + "%6d %6d"
41
        if self.branches:
42
            fmt_coverage += " %6d %6d"
43
        width100 = Numbers.pc_str_width()
44
        color = green
45
        if perc < 80:
46
            color = blue
47
        if perc < 50:
48
            color = red
49
        
50
        fmt_coverage += color("%%%ds%%%%" % (width100+3,))
51
        if self.config.show_missing:
52
            fmt_coverage += "   %s"
53
        fmt_coverage += "\n"
54
        return fmt_coverage
55
56
    def header(self):
57
        header = (self.fmt_name % "Name") + " Stmts   Miss"
58
        if self.branches:
59
            header += " Branch BrMiss"
60
        width100 = Numbers.pc_str_width()
61
        header += "%*s" % (width100+4, "Cover")
62
        header += "\n"
63
        return header
64
    
65
    def report(self, morfs, outfile=None):
66
        """Writes a report summarizing coverage statistics per module.
67
68
        `outfile` is a file object to write the summary to.
69
70
        """
71
        self.find_code_units(morfs)
72
73
        # Prepare the formatting strings
74
        
75
        fmt_err = "%s   %s: %s\n"
76
        
77
        header = self.header()
78
#         fmt_coverage = self.fmt_coverage()
79
        rule = "-" * len(header) + "\n"
80
81
        if not outfile:
82
            outfile = sys.stdout
83
84
        # Write the header
85
        outfile.write(header)
86
        outfile.write(rule)
87
88
        total = Numbers()
89
90
        for cu in self.code_units:
91
            try:
92
                analysis = self.coverage._analyze(cu)
93
                nums = analysis.numbers
94
                args = (cu.name, nums.n_statements, nums.n_missing)
95
                if self.branches:
96
                    args += (nums.n_branches, nums.n_missing_branches)
97
                args += (nums.pc_covered_str,)
98
                if self.config.show_missing:
99
                    args += (analysis.missing_formatted(),)
100
                outfile.write(self.fmt_coverage(nums.pc_covered) % args)
101
                total += nums
102
            except KeyboardInterrupt:                   # pragma: not covered
103
                raise
104
            except:
105
                report_it = not self.config.ignore_errors
106
                if report_it:
107
                    typ, msg = sys.exc_info()[:2]
108
                    if typ is NotPython and not cu.should_be_python():
109
                        report_it = False
110
                if report_it:
111
                    outfile.write(fmt_err % (cu.name, typ.__name__, msg))
112
113
        if total.n_files > 1:
114
            outfile.write(rule)
115
            args = ("TOTAL", total.n_statements, total.n_missing)
116
            if self.branches:
117
                args += (total.n_branches, total.n_missing_branches)
118
            args += (total.pc_covered_str,)
119
            if self.config.show_missing:
120
                args += ("",)
121
#             outfile.write(self.fmt_coverage(total.pc_covered) % args)
122
        return total
123
124
125
def report(cov, morfs=None, show_missing=False, ignore_errors=None,
126
                file=None,                          # pylint: disable=W0622
127
                omit=None, include=None
128
                ):
129
        cov._harvest_data()
130
        cov.config.from_args(
131
            ignore_errors=ignore_errors, omit=omit, include=include,
132
            show_missing=show_missing,
133
            )
134
        reporter = ColorSummaryReporter(cov, cov.config)
135
        return reporter.report(morfs, outfile=file)
136