-
Notifications
You must be signed in to change notification settings - Fork 2
/
yanc.py
104 lines (85 loc) · 3.81 KB
/
yanc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import re
from nose.plugins import Plugin
import termcolor
class ColorStream(object):
_colors = {
"green" : ("OK", "ok", "."),
"red" : ("ERROR", "FAILED", "errors", "E"),
"yellow" : ("FAILURE", "FAIL", "failures", "F"),
"magenta" : ("SKIP", "S"),
"blue" : ("-" * 70, "=" * 70),
}
def __init__(self, stream):
self._stream = stream
self._color_map = {}
self._patten_map = {}
for color, labels in self._colors.items():
for label in labels:
self._color_map[label] = color
if len(label) > 1:
self._patten_map[label] = re.compile("%s=\d+" % label)
def __getattr__(self, key):
return getattr(self._stream, key)
def _colorize(self, string, color=None):
if string:
if color is None:
color = self._color_map.get(string)
if color is None:
for key in self._color_map:
# looking for a test failure as LABEL: str(test)
if string.startswith(key + ":"):
segments = string.split(":")
label = self._colorize(segments[0] + ":",
self._color_map[key])
desc = ":".join(segments[1:])
if desc.startswith(" Failure: "):
desc = termcolor.colored(desc, self._color_map[key])
return label + desc
for key, key_color in self._color_map.items():
# looking for label=number in the summary
pattern = self._patten_map.get(key)
if pattern is not None:
for match in pattern.findall(string):
string = string.replace(match,
self._colorize(match, key_color))
if color is not None:
string = termcolor.colored(string, color, attrs=("bold",))
return string
def write(self, string):
self._stream.write(self._colorize(string))
def writeln(self, string=""):
self._stream.writeln(self._colorize(string))
class YANC(Plugin):
"""Yet another nose colorer"""
name = "yanc"
_options = (
("color", "YANC color override - one of on,off [%s]", "store"),
)
def options(self, parser, env):
super(YANC, self).options(parser, env)
for name, help, action in self._options:
env_opt = "NOSE_YANC_%s" % name.upper()
parser.add_option("--yanc-%s" % name.replace("_", "-"),
action=action,
dest="yanc_%s" % name,
default=env.get(env_opt),
help=help % env_opt)
def configure(self, options, conf):
super(YANC, self).configure(options, conf)
for name, help, dummy in self._options:
name = "yanc_%s" % name
setattr(self, name, getattr(options, name))
self.color = self.yanc_color != "off" \
and (self.yanc_color == "on" \
or (hasattr(self.conf, "stream")
and hasattr(self.conf.stream, "isatty") \
and self.conf.stream.isatty()))
def begin(self):
if self.color:
import sys
if hasattr(self.conf, "stream"):
self.conf.stream = ColorStream(self.conf.stream)
def finalize(self, result):
if self.color:
if hasattr(self.conf, "stream") and hasattr(self.conf.stream, "_stream"):
self.conf.stream = self.conf.stream._stream