author | Tero Marttila <terom@fixme.fi> |
Tue, 05 May 2009 19:39:44 +0300 | |
changeset 15 | 707ddd7a7912 |
parent 13 | a0cb32f3de3d |
child 16 | 9234f5ae765b |
permissions | -rwxr-xr-x |
0 | 1 |
#!/usr/bin/python2.5 |
3 | 2 |
import werkzeug |
3 |
from werkzeug.exceptions import HTTPException |
|
0 | 4 |
|
1
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
5 |
from PIL import Image, ImageDraw, ImageFont, ImageEnhance |
0 | 6 |
from cStringIO import StringIO |
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
7 |
import random, itertools, time, os.path |
0 | 8 |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
9 |
if not hasattr(itertools, 'izip_longest') : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
10 |
|
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
11 |
def izip_longest(*args, **kwds): |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
12 |
# izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
13 |
fillvalue = kwds.get('fillvalue') |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
14 |
def sentinel(counter = ([fillvalue]*(len(args)-1)).pop): |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
15 |
yield counter() # yields the fillvalue, or raises IndexError |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
16 |
fillers = itertools.repeat(fillvalue) |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
17 |
iters = [itertools.chain(it, sentinel(), fillers) for it in args] |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
18 |
try: |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
19 |
for tup in itertools.izip(*iters): |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
20 |
yield tup |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
21 |
except IndexError: |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
22 |
pass |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
23 |
|
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
24 |
itertools.izip_longest = izip_longest |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
25 |
|
3 | 26 |
class Defaults : |
27 |
# settings |
|
8 | 28 |
|
29 |
text_lang = 'en' |
|
0 | 30 |
|
7 | 31 |
chars = [ u'"', u'!', u'?' ] |
0 | 32 |
|
8 | 33 |
colors = [ |
3 | 34 |
"#0469af", |
35 |
"#fbc614", |
|
36 |
"#e1313b", |
|
37 |
] |
|
38 |
||
39 |
font_name = 'helvetica' |
|
40 |
font_size = 30 |
|
41 |
||
8 | 42 |
bg_color = "#ffffff" |
3 | 43 |
line_spacing = -10 |
44 |
sharpness = 0.6 |
|
0 | 45 |
|
7 | 46 |
img_format = 'png' |
47 |
||
8 | 48 |
TEXT_BY_LANG = dict( |
49 |
en = [ |
|
50 |
u"aalto", |
|
51 |
u"unive", |
|
52 |
u"rsity" |
|
53 |
], |
|
54 |
fi = [ |
|
55 |
u"aalto", |
|
56 |
u"yliop", |
|
57 |
u"isto" |
|
58 |
], |
|
59 |
se = [ |
|
60 |
u"aalto", |
|
61 |
u"univer", |
|
62 |
u"sitetet", |
|
63 |
], |
|
64 |
) |
|
65 |
||
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
66 |
STATIC_PATH = "static" |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
67 |
|
8 | 68 |
FONTS = { |
3 | 69 |
'dejavu-sans-bold': "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf", |
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
70 |
'helvetica': "fonts/HELR65W.TTF", |
3 | 71 |
} |
0 | 72 |
|
8 | 73 |
IMAGE_FORMATS = { |
74 |
'jpeg': 'jpeg', |
|
75 |
'jpg': 'jpeg', |
|
76 |
'png': 'png', |
|
77 |
'bmp': 'bmp' |
|
78 |
} |
|
79 |
||
6 | 80 |
FONT_SIZE_MAX = 1024 |
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
81 |
IMG_SIZE_MAX = 1024 |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
82 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
83 |
TILE_SIZE = (100, 100) |
3 | 84 |
|
85 |
# enable debugging |
|
6 | 86 |
DEBUG = True |
87 |
||
0 | 88 |
|
89 |
def randomize (seq) : |
|
90 |
""" |
|
91 |
Returns the given sequence in random order as a list |
|
92 |
""" |
|
93 |
||
94 |
# copy |
|
95 |
l = list(seq) |
|
96 |
||
97 |
# rearrange |
|
98 |
random.shuffle(l) |
|
99 |
||
100 |
return l |
|
101 |
||
11 | 102 |
def randomize_str_char (str) : |
103 |
""" |
|
104 |
Randomize the given string by moving one char around |
|
105 |
""" |
|
106 |
||
107 |
l = list(str) |
|
108 |
||
109 |
c = l.pop(random.randint(0, len(l) - 1)) |
|
110 |
l.insert(random.randint(0, len(l)), c) |
|
111 |
||
112 |
return ''.join(l) |
|
113 |
||
114 |
def build_data (text, chars, line_colors, random_chars=True, random_text=False, random_text_char=False) : |
|
0 | 115 |
""" |
116 |
Returns a matrix of (text, color) tuples representing the data to render |
|
117 |
||
118 |
[ [ (str, str) ] ] |
|
119 |
||
120 |
text - list of lines |
|
121 |
chars - list of random chars to interpse |
|
122 |
line_colors - list of colors to draw the chars in |
|
11 | 123 |
random_chars - randomize the lines the chars go in |
124 |
random_text - randomize the chars in each line |
|
125 |
random_text_char - randomize each line by moving one char around |
|
0 | 126 |
""" |
127 |
||
128 |
data = [] |
|
9 | 129 |
|
130 |
if random_chars : |
|
131 |
chars = randomize(chars) |
|
0 | 132 |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
133 |
for line, char, color in itertools.izip_longest(text, chars, line_colors, fillvalue=None) : |
15 | 134 |
if not line : |
135 |
continue |
|
136 |
||
0 | 137 |
# pick position to place char |
138 |
pos = random.randint(1, len(line) - 1) |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
139 |
|
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
140 |
if not color : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
141 |
color = "#000000" |
9 | 142 |
|
143 |
if random_text : |
|
144 |
line = ''.join(randomize(line)) |
|
11 | 145 |
|
146 |
if random_text_char : |
|
147 |
line = randomize_str_char(line) |
|
0 | 148 |
|
149 |
# split into three parts |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
150 |
if char : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
151 |
data.append([ |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
152 |
(line[:pos], "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
153 |
(char, color), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
154 |
(line[pos:], "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
155 |
]) |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
156 |
else : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
157 |
data.append([ |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
158 |
(line, "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
159 |
]) |
0 | 160 |
|
161 |
return data |
|
162 |
||
163 |
def load_font (font_name, font_size) : |
|
164 |
""" |
|
165 |
Load a font by name |
|
166 |
""" |
|
167 |
||
168 |
# load font |
|
8 | 169 |
font_path = FONTS[font_name] |
0 | 170 |
font = ImageFont.truetype(font_path, font_size) |
171 |
||
172 |
return font |
|
173 |
||
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
174 |
def render_img (data, font, background_color="#ffffff", line_spacing=0, img_size=None) : |
0 | 175 |
""" |
176 |
Render the data (as from build_data) as an image, using the given PIL.ImageFont, and return the PIL Image object |
|
177 |
""" |
|
178 |
||
179 |
img_width = img_height = 0 |
|
180 |
||
181 |
img_data = [] |
|
182 |
||
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
183 |
# compute image/segment width/height |
0 | 184 |
for segments in data : |
185 |
line_width = line_height = 0 |
|
186 |
||
187 |
# build a new list of segments with additional info |
|
188 |
line_segments = [] |
|
189 |
||
190 |
for seg_text, seg_color in segments : |
|
191 |
# compute rendered text size |
|
192 |
seg_width, seg_height = font.getsize(seg_text) |
|
193 |
||
194 |
# update line_* |
|
195 |
line_width += seg_width |
|
196 |
line_height = max(line_height, seg_height) |
|
197 |
||
198 |
# build the new segments list |
|
199 |
line_segments.append((seg_text, seg_color, seg_width)) |
|
200 |
||
201 |
# update img_* |
|
202 |
img_width = max(img_width, line_width) |
|
203 |
img_height += line_height |
|
204 |
img_data.append((line_segments, line_height)) |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
205 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
206 |
if img_size : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
207 |
# override size |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
208 |
img_width, img_height = img_size |
0 | 209 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
210 |
else : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
211 |
# calculate height needed for line spacing |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
212 |
img_height += (len(img_data) - 1) * line_spacing |
0 | 213 |
|
214 |
# create image |
|
215 |
img = Image.new("RGB", (img_width, img_height), background_color) |
|
216 |
draw = ImageDraw.Draw(img) |
|
217 |
||
218 |
# draw text |
|
219 |
img_y = 0 |
|
220 |
for segments, line_height in img_data : |
|
221 |
img_x = 0 |
|
222 |
||
223 |
# draw each segment build above, incremeing along img_x |
|
224 |
for seg_text, seg_color, seg_width in segments : |
|
225 |
draw.text((img_x, img_y), seg_text, font=font, fill=seg_color) |
|
226 |
||
227 |
img_x += seg_width |
|
228 |
||
229 |
img_y += line_height + line_spacing |
|
230 |
||
231 |
return img |
|
232 |
||
3 | 233 |
def effect_sharpness (img, factor) : |
1
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
234 |
""" |
3 | 235 |
Sharpen the image by the given factor |
1
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
236 |
""" |
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
237 |
|
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
238 |
return ImageEnhance.Sharpness(img).enhance(factor) |
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
239 |
|
7 | 240 |
def build_img (img, format='png') : |
0 | 241 |
""" |
242 |
Write the given PIL.Image as a string, returning the raw binary data |
|
7 | 243 |
|
244 |
Format should be one of the PIL-supported image foarts |
|
0 | 245 |
""" |
246 |
||
247 |
# render PNG output |
|
248 |
buf = StringIO() |
|
7 | 249 |
img.save(buf, format) |
0 | 250 |
data = buf.getvalue() |
251 |
||
252 |
return data |
|
253 |
||
3 | 254 |
def arg_bool (val) : |
255 |
if val.lower() in ('true', 't', '1', 'yes', 'y') : |
|
256 |
return True |
|
257 |
elif val.lower() in ('false', 'f', '0', 'no', 'n') : |
|
258 |
return False |
|
259 |
else : |
|
260 |
raise ValueError(val) |
|
261 |
||
262 |
def arg_color (val) : |
|
263 |
if val.beginswith('#') : |
|
264 |
int(val[1:], 16) |
|
265 |
||
266 |
return val |
|
267 |
else : |
|
268 |
raise ValueError(val) |
|
269 |
||
8 | 270 |
class Option (object) : |
271 |
def __init__ (self, name, is_list, type, default, range) : |
|
272 |
self.name = name |
|
273 |
self.is_list = is_list |
|
274 |
self.type = type |
|
275 |
self.default = default |
|
276 |
self.range = range |
|
3 | 277 |
|
8 | 278 |
def parse (self, args) : |
279 |
if self.is_list : |
|
280 |
if self.name in args : |
|
281 |
return args.getlist(self.name, self.type) |
|
282 |
else : |
|
283 |
return self.default |
|
284 |
else : |
|
10
6add80d3993b
enable arg_bools by just giving an empty opt
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
285 |
if self.type == arg_bool and not self.default and self.name in args : |
6add80d3993b
enable arg_bools by just giving an empty opt
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
286 |
return True |
6add80d3993b
enable arg_bools by just giving an empty opt
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
287 |
|
6add80d3993b
enable arg_bools by just giving an empty opt
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
288 |
else : |
6add80d3993b
enable arg_bools by just giving an empty opt
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
289 |
return args.get(self.name, self.default, self.type) |
3 | 290 |
|
8 | 291 |
class Options (object) : |
292 |
def __init__ (self, *options) : |
|
293 |
self.options = options |
|
6 | 294 |
|
8 | 295 |
def parse (self, args) : |
296 |
return dict((opt.name, opt.parse(args)) for opt in self.options) |
|
297 |
||
298 |
OPTIONS = Options( |
|
299 |
Option('lang', False, str, Defaults.text_lang, TEXT_BY_LANG.keys()), |
|
300 |
Option('text', True, unicode, None, None), |
|
9 | 301 |
Option('random-text', False, arg_bool, False, None), |
11 | 302 |
Option('random-text-char', False, arg_bool, False, None), |
8 | 303 |
Option('chars', True, unicode, Defaults.chars, None), |
304 |
Option('random-chars', False, arg_bool, True, None), |
|
305 |
Option('colors', True, arg_color, Defaults.colors, None), |
|
306 |
Option('font', False, str, Defaults.font_name, FONTS.keys()), |
|
307 |
Option('font-size', False, int, Defaults.font_size, None), |
|
308 |
Option('bg-color', False, arg_color, Defaults.bg_color, None), |
|
309 |
Option('line-spacing', False, int, Defaults.line_spacing, None), |
|
310 |
Option('sharpness', False, float, Defaults.sharpness, None), |
|
311 |
Option('image-format', False, str, Defaults.img_format, IMAGE_FORMATS.keys()), |
|
12 | 312 |
Option('seed', False, int, None, None), |
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
313 |
Option('img_width', False, int, None, None), |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
314 |
Option('img_height', False, int, None, None), |
8 | 315 |
) |
316 |
||
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
317 |
def handle_generic (req, img_size=None) : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
318 |
# parse options |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
319 |
opts = OPTIONS.parse(req.args) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
320 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
321 |
# postprocess |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
322 |
if opts['text'] is None : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
323 |
opts['text'] = TEXT_BY_LANG[opts['lang']] |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
324 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
325 |
if opts['font-size'] > FONT_SIZE_MAX : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
326 |
raise ValueError(opts['font-size']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
327 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
328 |
if opts['seed'] is None : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
329 |
opts['seed'] = time.time() |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
330 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
331 |
if opts['img_width'] and opts['img_height'] : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
332 |
img_size = (opts['img_width'], opts['img_height']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
333 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
334 |
if opts['img_width'] > IMG_SIZE_MAX or opts['img_height'] > IMG_SIZE_MAX : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
335 |
raise ValueError(img_size) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
336 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
337 |
# load/prep resources |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
338 |
random.seed(opts['seed']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
339 |
data = build_data(opts['text'], opts['chars'], opts['colors'], opts['random-chars'], opts['random-text'], opts['random-text-char']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
340 |
font = load_font(opts['font'], opts['font-size']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
341 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
342 |
# render the image |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
343 |
img = render_img(data, font, opts['bg-color'], opts['line-spacing'], img_size) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
344 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
345 |
img = effect_sharpness(img, opts['sharpness']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
346 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
347 |
png_data = build_img(img, opts['image-format']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
348 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
349 |
# build the response |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
350 |
response = werkzeug.Response(png_data, mimetype='image/%s' % opts['image-format']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
351 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
352 |
return response |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
353 |
|
8 | 354 |
def handle_help (req) : |
355 |
return werkzeug.Response('\n'.join( |
|
356 |
"%-15s %4s %-10s %-20s %s" % data for data in [ |
|
357 |
("name", "", "type", "default", "range"), |
|
358 |
("", "", "", "", ""), |
|
359 |
] + [( |
|
360 |
opt.name, |
|
361 |
'list' if opt.is_list else 'item', |
|
362 |
opt.type.__name__, |
|
363 |
repr(opt.default), |
|
364 |
opt.range if opt.range else "" |
|
365 |
) for opt in OPTIONS.options] |
|
366 |
), mimetype='text/plain') |
|
367 |
||
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
368 |
def handle_logo (req) : |
8 | 369 |
if 'help' in req.args : |
370 |
return handle_help(req) |
|
12 | 371 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
372 |
return handle_generic(req) |
3 | 373 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
374 |
def handle_tile (req) : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
375 |
return handle_generic(req, img_size=TILE_SIZE) |
3 | 376 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
377 |
def handle_request (req) : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
378 |
if req.path == '/' or req.path.startswith('/logo.') : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
379 |
return handle_logo(req) |
3 | 380 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
381 |
elif req.path == '/tile' : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
382 |
return handle_tile(req) |
3 | 383 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
384 |
else : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
385 |
raise ValueError(req) |
3 | 386 |
|
387 |
@werkzeug.Request.application |
|
388 |
def wsgi_application (request) : |
|
0 | 389 |
""" |
3 | 390 |
Our werkzeug WSGI handler |
0 | 391 |
""" |
392 |
||
3 | 393 |
try : |
394 |
# request -> response |
|
395 |
response = handle_request(request) |
|
0 | 396 |
|
3 | 397 |
return response |
398 |
||
399 |
except HTTPException, e : |
|
400 |
# return as HTTP response |
|
401 |
return e |
|
0 | 402 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
403 |
def build_app () : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
404 |
""" |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
405 |
Build and return a WSGI application |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
406 |
""" |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
407 |
|
3 | 408 |
app = wsgi_application |
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
409 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
410 |
# add other middleware |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
411 |
app = werkzeug.SharedDataMiddleware(app, { |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
412 |
'/static': STATIC_PATH, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
413 |
}) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
414 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
415 |
|
6 | 416 |
if DEBUG : |
3 | 417 |
# enable debugging |
418 |
app = werkzeug.DebuggedApplication(app, evalex=False) |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
419 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
420 |
return app |
0 | 421 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
422 |
def main_cgi (app) : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
423 |
import wsgiref.handlers |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
424 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
425 |
handler = wsgiref.handlers.CGIHandler() |
3 | 426 |
handler.run(app) |
0 | 427 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
428 |
def main_fastcgi (app) : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
429 |
import flup.server.fcgi |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
430 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
431 |
server = flup.server.fcgi.WSGIServer(app, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
432 |
multithreaded = False, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
433 |
multiprocess = False, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
434 |
multiplexed = False, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
435 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
436 |
bindAddress = None, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
437 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
438 |
umask = None, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
439 |
debug = True, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
440 |
) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
441 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
442 |
server.run() |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
443 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
444 |
def main () : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
445 |
from sys import argv |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
446 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
447 |
# get our file extension |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
448 |
root, ext = os.path.splitext(argv[0]) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
449 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
450 |
# get our handler |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
451 |
handler = { |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
452 |
'.cgi': main_cgi, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
453 |
'.fcgi': main_fastcgi, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
454 |
}[ext] |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
455 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
456 |
# get our app |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
457 |
app = build_app() |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
458 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
459 |
# run |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
460 |
handler(app) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
461 |
|
0 | 462 |
if __name__ == '__main__' : |
463 |
main() |
|
464 |