17
17
__version__ = '0.1.2'
18
18
19
19
20
+ DEFAULT_COLOR = '#a4a61d'
21
+ COLORS = {
22
+ 'brightgreen' : '#4c1' ,
23
+ 'green' : '#97CA00' ,
24
+ 'yellowgreen' : '#a4a61d' ,
25
+ 'yellow' : '#dfb317' ,
26
+ 'orange' : '#fe7d37' ,
27
+ 'red' : '#e05d44' ,
28
+ 'lightgrey' : '#9f9f9f' ,
29
+ }
30
+
31
+ COLOR_RANGES = [
32
+ (95 , 'brightgreen' ),
33
+ (90 , 'green' ),
34
+ (75 , 'yellowgreen' ),
35
+ (60 , 'yellow' ),
36
+ (40 , 'orange' ),
37
+ (0 , 'red' ),
38
+ ]
39
+
40
+
20
41
class Devnull (object ):
21
42
"""
22
43
A file like object that does nothing.
@@ -35,14 +56,27 @@ def get_total():
35
56
return '{0:.0f}' .format (total )
36
57
37
58
38
- def get_badge (total ):
59
+ def get_color (total ):
60
+ """
61
+ Return color for current coverage precent
62
+ """
63
+ try :
64
+ xtotal = int (total )
65
+ except ValueError :
66
+ return COLORS ['lightgrey' ]
67
+ for range_ , color in COLOR_RANGES :
68
+ if xtotal >= range_ :
69
+ return COLORS [color ]
70
+
71
+
72
+ def get_badge (total , color = DEFAULT_COLOR ):
39
73
"""
40
74
Read the SVG template from the package, update total, return SVG as a
41
75
string.
42
76
"""
43
77
template_path = os .path .join ('templates' , 'flat.svg' )
44
78
template = pkg_resources .resource_string (__name__ , template_path ).decode ('utf8' )
45
- return template .replace ('{{ total }}' , total )
79
+ return template .replace ('{{ total }}' , total ). replace ( '{{ color }}' , color )
46
80
47
81
48
82
def parse_args (argv = None ):
@@ -52,6 +86,10 @@ def parse_args(argv=None):
52
86
parser = argparse .ArgumentParser (description = __doc__ )
53
87
parser .add_argument ('-o' , dest = 'filepath' ,
54
88
help = 'Save the file to the specified path.' )
89
+ parser .add_argument ('-p' , dest = 'plain_color' , action = 'store_true' ,
90
+ help = 'Plain color mode. Standard green badge.' )
91
+ parser .add_argument ('-f' , dest = 'force' , action = 'store_true' ,
92
+ help = 'Force overwrite image, use with -o key.' )
55
93
parser .add_argument ('-q' , dest = 'quiet' , action = 'store_true' ,
56
94
help = 'Don\' t output any non-error messages.' )
57
95
parser .add_argument ('-v' , dest = 'print_version' , action = 'store_true' ,
@@ -66,7 +104,7 @@ def parse_args(argv=None):
66
104
return parser .parse_args ()
67
105
68
106
69
- def save_badge (badge , filepath ):
107
+ def save_badge (badge , filepath , force = False ):
70
108
"""
71
109
Save badge to the specified path.
72
110
"""
@@ -81,7 +119,7 @@ def save_badge(badge, filepath):
81
119
path += '.svg'
82
120
83
121
# Validate path (part 2)
84
- if os .path .exists (path ):
122
+ if not force and os .path .exists (path ):
85
123
print ('Error: "{}" already exists.' .format (path ))
86
124
sys .exit (1 )
87
125
@@ -114,11 +152,13 @@ def main(argv=None):
114
152
except coverage .misc .CoverageException as e :
115
153
print ('Error: {} Did you run coverage first?' .format (e ))
116
154
sys .exit (1 )
117
- badge = get_badge (total )
155
+
156
+ color = DEFAULT_COLOR if args .plain_color else get_color (total )
157
+ badge = get_badge (total , color )
118
158
119
159
# Show or save output
120
160
if args .filepath :
121
- path = save_badge (badge , args .filepath )
161
+ path = save_badge (badge , args .filepath , args . force )
122
162
if not args .quiet :
123
163
print ('Saved badge to {}' .format (path ))
124
164
else :
0 commit comments