This repository has been archived by the owner on Nov 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservices.py
102 lines (78 loc) · 2.56 KB
/
services.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
"""This module contains service definions for Shields.io.
Service functions should take three strings (first, second, color) and return
three strings (first, second, and color).
first - the first path part => the first word on the shield
second - the second path part => the second word on the shield
color - a color name, the background behind the second word on the shield
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import os
import time
from urllib import quote, urlopen
from aspen import json
from shields_io.colors import RED, YELLOW, YELLOWGREEN, GREEN, LIGHTGRAY
def _test(first, second, color):
"""For testing.
"""
time.sleep(2)
if second == "random":
base = os.environ['CANONICAL_HOST']
url = base + "/random.txt"
second = urlopen(url).read().strip()
return first, second, color
def noop(first, second, color):
return first, second, color
def coveralls(first, second, color):
first = "coverage"
url = "https://coveralls.io/repos/%s/badge.png?branch=master"
fp = urlopen(url % second)
try:
# We get a redirect to an S3 URL.
score = fp.url.split('_')[1].split('.')[0]
second = score + '%'
as_number = int(score)
if as_number < 80:
color = RED
elif as_number < 90:
color = YELLOW
else:
color = GREEN
except (IndexError, ValueError):
second = 'n/a'
color = LIGHTGRAY
return first, second, color
def gittip(first, second, color):
first = "tips"
fp = urlopen("https://www.gittip.com/%s/public.json" % second)
receiving = float(json.loads(fp.read())['receiving'])
second = "$%d / week" % receiving
if receiving == 0:
color = RED
elif receiving < 10:
color = YELLOW
elif receiving < 100:
color = YELLOWGREEN
else:
color = GREEN
return first, second, color
def travis_ci(first, second, color):
first = "build"
url = 'https://api.travis-ci.org/repos/%s/branches/master' % quote(second)
fp = urlopen(url)
repo = json.loads(fp.read())
try:
second = repo['branch']['state']
except KeyError:
second = 'n/a'
color = { 'failed': RED
, 'passed': GREEN
, 'started': YELLOW
}.get(second, LIGHTGRAY)
return first, second, color
services = {}
services['_test'] = _test
services['coveralls'] = coveralls
services['gittip'] = gittip
services['travis-ci'] = travis_ci
def get(first):
return services.get(first, noop)