forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 1
/
badgeserver.py
executable file
·322 lines (275 loc) · 10.5 KB
/
badgeserver.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/python
"""
Script to dynamically generate a status shield.
Needs python-lxml and python-cairosvg (sudo apt-get install it).
ImageMagick and Inkscape renderers need imagemagick and inkscape (duh).
Librsvg renderer needs librsvg2-bin.
Be sure to install the Open Sans font where cairo will find it
(i.e. cp font/Open_Sans/*.ttf ~/.local/share/fonts/).
"""
import cgi
import os
import re
import shutil
import subprocess
import tempfile
from cStringIO import StringIO
from contextlib import contextmanager
from functools import partial
import cairo
from cairosvg import svg2png as svg2png_cairosvg
from lxml import etree
HERE = os.path.dirname(__file__)
SVG_TEMPLATE_FILE = os.path.join(HERE, "shield.svg")
@contextmanager
def TemporaryDirectory():
tmpdir = tempfile.mkdtemp(prefix='badgeserver-')
orig_dir = os.getcwd()
os.chdir(tmpdir)
try:
yield
finally:
os.chdir(orig_dir)
shutil.rmtree(tmpdir)
def svg2png_command(svg_data, command):
with TemporaryDirectory():
with open('badge.svg', 'wb') as f:
f.write(svg_data)
subprocess.check_call(command)
with open('badge.png', 'rb') as f:
return f.read()
def svg2png_inkscape(svg_data):
return svg2png_command(svg_data, ['inkscape', '-f', 'badge.svg', '-e', 'badge.png'])
def svg2png_imagemagick(svg_data):
return svg2png_command(svg_data, ['convert', 'badge.svg', 'badge.png'])
def svg2png_imagemagick_msvg(svg_data):
return svg2png_command(svg_data, ['convert', 'msvg:badge.svg', 'badge.png'])
def svg2png_rsvg(svg_data):
return svg2png_command(svg_data, ['rsvg-convert', 'badge.svg', '-o', 'badge.png'])
def load_svg_template():
with open(SVG_TEMPLATE_FILE) as f:
return f.read()
def list_color_options():
tree = etree.fromstring(load_svg_template())
nsmap = dict(svg='http://www.w3.org/2000/svg')
xpath = partial(tree.xpath, namespaces=nsmap)
return [node.attrib['id'] for node in xpath('//svg:linearGradient')]
def text_width(text, font_family="Open Sans", font_size=10,
font_style=cairo.FONT_SLANT_NORMAL,
font_weight=cairo.FONT_WEIGHT_NORMAL):
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
context = cairo.Context(surface)
context.select_font_face(font_family, font_style, font_weight)
context.set_font_size(font_size)
return context.text_extents(text)[2]
def svg_text_node_width(node):
# XXX: requires that the node have font-family and font-size attributes
# (I believe SVG defaults to sans-serif 12pt)
# XXX: ignores font-style and font-weight attributes
# (see cairosvg/surface/text.py about handling those)
font_family = node.attrib['font-family']
font_size = int(node.attrib['font-size'])
return text_width(node.text, font_family, font_size)
def make_badge_svg(vendor="vendor", status="status", color="lightgray",
vendor_width=40, status_width=37, converter=None):
tree = etree.fromstring(load_svg_template())
nsmap = dict(svg='http://www.w3.org/2000/svg')
xpath = partial(tree.xpath, namespaces=nsmap)
# change status background gradient
if not xpath('//svg:linearGradient[@id="%s"]' % color):
print("There's no gradient for %s" % color)
for node in xpath('//svg:g[@id="status"]/*[@fill="url(#lightgray)"]'):
node.attrib['fill'] = "url(#%s)" % color
# change the vendor text
for node in xpath('//svg:g[@id="vendor"]/svg:text'):
node.text = vendor
if vendor_width < 0:
vendor_width = svg_text_node_width(node) + 7
# change the status text
for node in xpath('//svg:g[@id="status"]/svg:text'):
node.text = status
if status_width < 0:
status_width = svg_text_node_width(node) + 7
# change the vendor and status widths
for node in xpath('//svg:g[@id="vendor"]/svg:rect'):
node.attrib['width'] = str(vendor_width)
for node in xpath('//svg:g[@id="status"]/svg:rect'):
node.attrib['x'] = str(vendor_width + 3)
node.attrib['width'] = str(status_width)
for node in xpath('//svg:g[@id="status"]/svg:text'):
node.attrib['x'] = str(vendor_width + 7)
for node in xpath('//svg:g[@id="status"]/svg:path'):
node.attrib['d'] = re.sub(r'^M\d+',
'M%d' % (vendor_width + status_width + 6),
node.attrib['d'])
for node in xpath('/svg:svg'):
node.attrib['width'] = str(vendor_width + status_width + 6)
return etree.tostring(tree)
def make_badge_png(**kw):
converter = kw.pop('converter', None) or 'cairosvg'
svg = make_badge_svg(**kw)
svg2png = {
'cairosvg': svg2png_cairosvg,
'inkscape': svg2png_inkscape,
'imagemagick': svg2png_imagemagick,
'imagemagick-msvg': svg2png_imagemagick_msvg,
'rsvg': svg2png_rsvg,
}[converter]
return svg2png(svg)
def render_html(html):
status = '200 OK'
headers = [('Content-type', 'text/html; charset=UTF-8')]
return status, headers, html
def render_error(code, message):
status = '%d %s' % (code, message)
headers = [('Content-type', 'text/plain; charset=UTF-8')]
return status, headers, message
def render_image(form, format):
vendor = form.getfirst("vendor", "badgeserver").decode('UTF-8')
vendor_width = int(form.getfirst("vendor_width", "-1"))
status = form.getfirst("status", "okay").decode('UTF-8')
status_width = int(form.getfirst("status_width", "-1"))
color = form.getfirst("color", "lightgray")
converter = form.getfirst("converter", "")
factory, content_type = dict(
png=(make_badge_png, "image/png"),
svg=(make_badge_svg, "image/svg+xml"),
)[format]
image_data = factory(vendor=vendor, status=status, color=color,
vendor_width=vendor_width, status_width=status_width,
converter=converter)
status = '200 OK'
headers = [('Content-type', content_type)]
return status, headers, image_data
INDEX_HTML = '''\
<!DOCTYPE html>
<html>
<head>
<title>Status shield generator</title>
<style type="text/css">
body {
padding: 2em;
}
label {
display: inline-block;
text-align: right;
width: 3.5em;
margin-right: 1ex;
}
p {
margin: 1ex;
}
form, p.result-image {
margin-bottom: 2em;
}
p.result-image {
line-height: 0;
}
input[type="number"] {
width: 4em;
text-align: right;
}
</style>
<script type="text/javascript">
function update() {
var vendor = document.getElementById("vendor").value;
var vendor_width = document.getElementById("vendor_width").value;
var status = document.getElementById("status").value;
var status_width = document.getElementById("status_width").value;
var color = document.getElementById("color").value;
var format = document.getElementById("format").value;
var converter = document.getElementById("converter").value;
var img = document.getElementById("result");
var url = "/image." + format + "?vendor=" + encodeURIComponent(vendor) + "&status=" + encodeURIComponent(status) + "&color=" + color;
if (vendor_width >= 0) url += "&vendor_width=" + vendor_width;
if (status_width >= 0) url += "&status_width=" + status_width;
if (format != "svg") url += "&converter=" + converter;
img.src = url;
var tt = document.getElementById("url");
tt.innerHTML = img.src;
}
</script>
</head>
<body onload="update()">
<form>
<p>
<label for="vendor">Vendor</label>
<input id="vendor" type="text" name="vendor" value="vendor" onchange="update()" onkeyup="update()">,
width <input id="vendor_width" type="number" name="vendor_width" value="-1" onchange="update()"> px (-1 = auto)
</p>
<p>
<label for="status">Status</label>
<input id="status" type="text" name="status" value="status" onchange="update()" onkeyup="update()">,
width <input id="status_width" type="number" name="status_width" value="-1" onchange="update()"> px (-1 = auto)
</p>
</p>
<p>
<label for="status">Color</label>
<select id="color" name="color" onchange="update()">
%(color_options)s
</select>
</p>
<p>
<label for="status">Format</label>
<select id="format" name="format" onchange="update()">
<option value="png">PNG</option>
<option value="svg">SVG</option>
</select>
</p>
<p>
<label for="status">Renderer</label>
<select id="converter" name="converter" onchange="update()">
<option value="cairosvg">CairoSVG</option>
<option value="inkscape">Inkscape</option>
<option value="rsvg">librsvg</option>
<option value="imagemagick">ImageMagick (rsvg)</option>
<option value="imagemagick-msvg">ImageMagick (msvg)</option>
</select>
</p>
<p>
<input type="submit" value="Update" onclick="update(); return false">
</p>
</form>
<p class="result-image">
<img id="result" src="/image.png?vendor=vendor&status=status&color=lightgray">
</p>
<p>
Dynamic image URL:
<br>
<tt id="url">http://localhost:8000/image.png?vendor=vendor&status=status&color=lightgray</tt>
</p>
</body>
</html>
'''
def wsgi_app(environ, start_response):
# Parse the request
path = environ['PATH_INFO']
form = cgi.FieldStorage(environ=environ, fp=StringIO())
# Dispatch to the right handler
if path == '/':
status, headers, body = render_html(INDEX_HTML % dict(
color_options=''.join(
'<option>%s</option>' % color
for color in list_color_options()
if color != 'gray'),
))
elif path == '/image.png':
status, headers, body = render_image(form, "png")
elif path == '/image.svg':
status, headers, body = render_image(form, "svg")
else:
status, headers, body = render_error(404, "Not found")
start_response(status, headers)
return [body]
def serve(port=8000, listen_on='localhost'):
from wsgiref.simple_server import make_server
server = make_server(listen_on, port, wsgi_app)
print("Listening on http://%s:%d/" % (listen_on or '*', port))
try:
server.serve_forever()
except KeyboardInterrupt:
pass
def main():
serve()
if __name__ == '__main__':
main()