author | Tero Marttila <terom@fixme.fi> |
Tue, 05 May 2009 21:49:56 +0300 | |
changeset 16 | 9234f5ae765b |
parent 15 | 707ddd7a7912 |
child 17 | 301725d72e29 |
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 = [] |
|
16 | 129 |
|
130 |
# if no chars given, don't insert any |
|
131 |
if not chars : |
|
132 |
chars = [] |
|
133 |
||
134 |
# randomize char order across lines? |
|
9 | 135 |
if random_chars : |
136 |
chars = randomize(chars) |
|
0 | 137 |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
138 |
for line, char, color in itertools.izip_longest(text, chars, line_colors, fillvalue=None) : |
15 | 139 |
if not line : |
140 |
continue |
|
141 |
||
0 | 142 |
# pick position to place char |
16 | 143 |
if len(line) >= 2 : |
144 |
pos = random.randint(1, len(line) - 1) |
|
145 |
else : |
|
146 |
pos = random.randint(0, 1) |
|
147 |
||
148 |
# default color |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
149 |
if not color : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
150 |
color = "#000000" |
16 | 151 |
|
152 |
# randomize text in some way? |
|
9 | 153 |
if random_text : |
154 |
line = ''.join(randomize(line)) |
|
11 | 155 |
|
156 |
if random_text_char : |
|
157 |
line = randomize_str_char(line) |
|
0 | 158 |
|
159 |
# split into three parts |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
160 |
if char : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
161 |
data.append([ |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
162 |
(line[:pos], "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
163 |
(char, color), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
164 |
(line[pos:], "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
165 |
]) |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
166 |
else : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
167 |
data.append([ |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
168 |
(line, "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
169 |
]) |
0 | 170 |
|
171 |
return data |
|
172 |
||
173 |
def load_font (font_name, font_size) : |
|
174 |
""" |
|
175 |
Load a font by name |
|
176 |
""" |
|
177 |
||
178 |
# load font |
|
8 | 179 |
font_path = FONTS[font_name] |
0 | 180 |
font = ImageFont.truetype(font_path, font_size) |
181 |
||
182 |
return font |
|
183 |
||
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
184 |
def render_img (data, font, background_color="#ffffff", line_spacing=0, img_size=None) : |
0 | 185 |
""" |
186 |
Render the data (as from build_data) as an image, using the given PIL.ImageFont, and return the PIL Image object |
|
187 |
""" |
|
188 |
||
189 |
img_width = img_height = 0 |
|
190 |
||
191 |
img_data = [] |
|
192 |
||
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
193 |
# compute image/segment width/height |
0 | 194 |
for segments in data : |
195 |
line_width = line_height = 0 |
|
196 |
||
197 |
# build a new list of segments with additional info |
|
198 |
line_segments = [] |
|
199 |
||
200 |
for seg_text, seg_color in segments : |
|
201 |
# compute rendered text size |
|
202 |
seg_width, seg_height = font.getsize(seg_text) |
|
203 |
||
204 |
# update line_* |
|
205 |
line_width += seg_width |
|
206 |
line_height = max(line_height, seg_height) |
|
207 |
||
208 |
# build the new segments list |
|
209 |
line_segments.append((seg_text, seg_color, seg_width)) |
|
210 |
||
211 |
# update img_* |
|
212 |
img_width = max(img_width, line_width) |
|
213 |
img_height += line_height |
|
214 |
img_data.append((line_segments, line_height)) |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
215 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
216 |
if img_size : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
217 |
# override size |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
218 |
img_width, img_height = img_size |
0 | 219 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
220 |
else : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
221 |
# calculate height needed for line spacing |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
222 |
img_height += (len(img_data) - 1) * line_spacing |
0 | 223 |
|
224 |
# create image |
|
225 |
img = Image.new("RGB", (img_width, img_height), background_color) |
|
226 |
draw = ImageDraw.Draw(img) |
|
227 |
||
228 |
# draw text |
|
229 |
img_y = 0 |
|
230 |
for segments, line_height in img_data : |
|
231 |
img_x = 0 |
|
232 |
||
233 |
# draw each segment build above, incremeing along img_x |
|
234 |
for seg_text, seg_color, seg_width in segments : |
|
235 |
draw.text((img_x, img_y), seg_text, font=font, fill=seg_color) |
|
236 |
||
237 |
img_x += seg_width |
|
238 |
||
239 |
img_y += line_height + line_spacing |
|
240 |
||
241 |
return img |
|
242 |
||
3 | 243 |
def effect_sharpness (img, factor) : |
1
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
244 |
""" |
3 | 245 |
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
|
246 |
""" |
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
247 |
|
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
248 |
return ImageEnhance.Sharpness(img).enhance(factor) |
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
249 |
|
7 | 250 |
def build_img (img, format='png') : |
0 | 251 |
""" |
252 |
Write the given PIL.Image as a string, returning the raw binary data |
|
7 | 253 |
|
254 |
Format should be one of the PIL-supported image foarts |
|
0 | 255 |
""" |
256 |
||
257 |
# render PNG output |
|
258 |
buf = StringIO() |
|
7 | 259 |
img.save(buf, format) |
0 | 260 |
data = buf.getvalue() |
261 |
||
262 |
return data |
|
263 |
||
16 | 264 |
class OptionType (object) : |
265 |
def parse (self, val) : |
|
266 |
""" |
|
267 |
Unicode value -> object |
|
268 |
""" |
|
3 | 269 |
|
16 | 270 |
abstract |
3 | 271 |
|
16 | 272 |
def build (self, val) : |
273 |
""" |
|
274 |
object -> unicode value |
|
275 |
""" |
|
276 |
||
277 |
return unicode(val) |
|
278 |
||
279 |
def input (self, val) : |
|
280 |
""" |
|
281 |
HTML input item |
|
282 |
""" |
|
283 |
||
284 |
abstract |
|
285 |
||
286 |
class StringOption (OptionType) : |
|
287 |
def parse (self, val) : |
|
288 |
return unicode(val) |
|
289 |
||
290 |
def build (self, val) : |
|
291 |
if val is None : |
|
292 |
return "" |
|
293 |
||
294 |
else : |
|
295 |
return val |
|
296 |
||
297 |
def input (self, opt, val) : |
|
298 |
return """<input type="text" name="%(name)s" id="%(name)s" value="%(value)s" />""" % dict( |
|
299 |
name = opt.name, |
|
300 |
value = self.build(val), |
|
301 |
) |
|
302 |
||
303 |
def select (self, opt, val) : |
|
304 |
return """<select name="%(name)s"> |
|
305 |
%(options)s |
|
306 |
</select>""" % dict( |
|
307 |
name = opt.name, |
|
308 |
options = '\n'.join( |
|
309 |
"\t<option %(selected)s>%(value)s</option>" % dict( |
|
310 |
value = self.build(optval), |
|
311 |
selected = 'selected="selected"' if val == optval else "", |
|
312 |
) for optval in opt.range |
|
313 |
), |
|
314 |
) |
|
315 |
||
316 |
class BoolOption (OptionType) : |
|
317 |
def parse (self, val) : |
|
318 |
if val.lower() in ('true', 't', '1', 'yes', 'y') : |
|
319 |
return True |
|
320 |
||
321 |
elif val.lower() in ('false', 'f', '0', 'no', 'n') : |
|
322 |
return False |
|
323 |
||
324 |
else : |
|
325 |
raise ValueError(val) |
|
326 |
||
327 |
def input (self, opt, val) : |
|
328 |
return """<input type="checkbox" name="%(name)s" id="%(name)s" %(checked)s/>""" % dict( |
|
329 |
name = opt.name, |
|
330 |
checked = 'checked="checked"' if val else '', |
|
331 |
) |
|
332 |
||
333 |
class IntOption (StringOption) : |
|
334 |
def parse (self, val) : |
|
335 |
return int(val) |
|
336 |
||
337 |
class FloatOption (StringOption) : |
|
338 |
def parse (self, val) : |
|
339 |
return float(val) |
|
340 |
||
341 |
class ColorOption (StringOption) : |
|
342 |
def _parse (self, val) : |
|
343 |
if val.startswith('#') : |
|
344 |
int(val[1:], 16) |
|
345 |
||
346 |
return val |
|
347 |
else : |
|
348 |
raise ValueError(val) |
|
349 |
||
3 | 350 |
|
8 | 351 |
class Option (object) : |
352 |
def __init__ (self, name, is_list, type, default, range) : |
|
353 |
self.name = name |
|
354 |
self.is_list = is_list |
|
355 |
self.type = type |
|
356 |
self.default = default |
|
357 |
self.range = range |
|
3 | 358 |
|
16 | 359 |
def parse (self, args, force_bool=False) : |
8 | 360 |
if self.is_list : |
361 |
if self.name in args : |
|
16 | 362 |
l = args.getlist(self.name, self.type.parse) |
363 |
||
364 |
# special-case to handle a single param with a newline-separtated list |
|
365 |
if len(l) == 1 : |
|
366 |
if not l[0] : |
|
367 |
return None |
|
368 |
||
369 |
else : |
|
370 |
return l[0].split('\r\n') |
|
371 |
||
372 |
else : |
|
373 |
return l |
|
374 |
||
8 | 375 |
else : |
376 |
return self.default |
|
16 | 377 |
|
8 | 378 |
else : |
16 | 379 |
if isinstance(self.type, BoolOption) and force_bool : |
380 |
return self.name in args |
|
381 |
||
382 |
elif isinstance(self.type, BoolOption) and not self.default and self.name in args : |
|
10
6add80d3993b
enable arg_bools by just giving an empty opt
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
383 |
return True |
6add80d3993b
enable arg_bools by just giving an empty opt
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
384 |
|
6add80d3993b
enable arg_bools by just giving an empty opt
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
385 |
else : |
16 | 386 |
return args.get(self.name, self.default, self.type.parse) |
387 |
||
388 |
def build_list (self, value) : |
|
389 |
if self.is_list and value : |
|
390 |
return [self.type.build(val) for val in value] |
|
391 |
||
392 |
else : |
|
393 |
return [self.type.build(value)] |
|
394 |
||
395 |
def _build_input (self, value) : |
|
396 |
if self.is_list : |
|
397 |
return """\ |
|
398 |
<textarea name="%(name)s" cols="30">\ |
|
399 |
%(data)s\ |
|
400 |
</textarea>""" % dict( |
|
401 |
name = self.name, |
|
402 |
data = '\n'.join(self.type.build(val) for val in value) if value else '', |
|
403 |
) |
|
404 |
||
405 |
elif self.range : |
|
406 |
return self.type.select(self, value) |
|
407 |
||
408 |
else : |
|
409 |
return self.type.input(self, value) |
|
410 |
||
411 |
def build_form (self, opts) : |
|
412 |
value = opts[self.name] |
|
413 |
||
414 |
return """\ |
|
415 |
<div class="param"><label for="%(name)s">%(title)s</label>%(input)s</div>\ |
|
416 |
""" % dict( |
|
417 |
name = self.name, |
|
418 |
title = self.name.title().replace('-', ' '), |
|
419 |
input = self._build_input(value) |
|
420 |
) |
|
3 | 421 |
|
8 | 422 |
class Options (object) : |
423 |
def __init__ (self, *options) : |
|
16 | 424 |
self.options = list(options) |
425 |
self.options_by_name = dict((opt.name, opt) for opt in options) |
|
6 | 426 |
|
16 | 427 |
def parse (self, args, **kwargs) : |
428 |
return dict((opt.name, opt.parse(args, **kwargs)) for opt in self.options) |
|
8 | 429 |
|
430 |
OPTIONS = Options( |
|
16 | 431 |
Option('lang', False, StringOption(), Defaults.text_lang, TEXT_BY_LANG.keys()), |
432 |
Option('text', True, StringOption(), None, None), |
|
433 |
Option('random-text', False, BoolOption(), False, None), |
|
434 |
Option('random-text-char',False,BoolOption(), False, None), |
|
435 |
Option('chars', True, StringOption(), Defaults.chars, None), |
|
436 |
Option('random-chars', False, BoolOption(), True, None), |
|
437 |
Option('colors', True, ColorOption(), Defaults.colors, None), |
|
438 |
Option('font', False, StringOption(), Defaults.font_name, FONTS.keys()), |
|
439 |
Option('font-size', False, IntOption(), Defaults.font_size, None), |
|
440 |
Option('bg-color', False, ColorOption(), Defaults.bg_color, None), |
|
441 |
Option('line-spacing', False, IntOption(), Defaults.line_spacing, None), |
|
442 |
Option('sharpness', False, FloatOption(), Defaults.sharpness, None), |
|
443 |
Option('image-format', False, StringOption(), Defaults.img_format, IMAGE_FORMATS.keys()), |
|
444 |
Option('seed', False, IntOption(), None, None), |
|
445 |
Option('img_width', False, IntOption(), None, None), |
|
446 |
Option('img_height', False, IntOption(), None, None), |
|
8 | 447 |
) |
448 |
||
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
449 |
def handle_generic (req, img_size=None) : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
450 |
# parse options |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
451 |
opts = OPTIONS.parse(req.args) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
452 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
453 |
# postprocess |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
454 |
if opts['text'] is None : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
455 |
opts['text'] = TEXT_BY_LANG[opts['lang']] |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
456 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
457 |
if opts['font-size'] > FONT_SIZE_MAX : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
458 |
raise ValueError(opts['font-size']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
459 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
460 |
if opts['seed'] is None : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
461 |
opts['seed'] = time.time() |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
462 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
463 |
if opts['img_width'] and opts['img_height'] : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
464 |
img_size = (opts['img_width'], opts['img_height']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
465 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
466 |
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
|
467 |
raise ValueError(img_size) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
468 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
469 |
# load/prep resources |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
470 |
random.seed(opts['seed']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
471 |
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
|
472 |
font = load_font(opts['font'], opts['font-size']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
473 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
474 |
# render the image |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
475 |
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
|
476 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
477 |
img = effect_sharpness(img, opts['sharpness']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
478 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
479 |
png_data = build_img(img, opts['image-format']) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
480 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
481 |
# build the response |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
482 |
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
|
483 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
484 |
return response |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
485 |
|
8 | 486 |
def handle_help (req) : |
487 |
return werkzeug.Response('\n'.join( |
|
488 |
"%-15s %4s %-10s %-20s %s" % data for data in [ |
|
489 |
("name", "", "type", "default", "range"), |
|
490 |
("", "", "", "", ""), |
|
491 |
] + [( |
|
492 |
opt.name, |
|
493 |
'list' if opt.is_list else 'item', |
|
494 |
opt.type.__name__, |
|
495 |
repr(opt.default), |
|
496 |
opt.range if opt.range else "" |
|
497 |
) for opt in OPTIONS.options] |
|
498 |
), mimetype='text/plain') |
|
499 |
||
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
500 |
def handle_logo (req) : |
8 | 501 |
if 'help' in req.args : |
502 |
return handle_help(req) |
|
12 | 503 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
504 |
return handle_generic(req) |
3 | 505 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
506 |
def handle_tile (req) : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
507 |
return handle_generic(req, img_size=TILE_SIZE) |
3 | 508 |
|
16 | 509 |
def handle_index (options, req) : |
510 |
# parse options, force booleans if any form data was submitted, as checkboxes work that way |
|
511 |
opts = options.parse(req.values, force_bool=bool(req.form)) |
|
512 |
||
513 |
# build query string of req things |
|
514 |
qargs = [ |
|
515 |
(opt.name, opt.build_list(val)) for opt, val in ( |
|
516 |
(options.options_by_name[opt_name], val) for opt_name, val in opts.iteritems() |
|
517 |
) if val != opt.default |
|
518 |
] |
|
519 |
||
520 |
img_url = req.url_root + "logo" + ("?%s" % werkzeug.url_encode(qargs) if qargs else '') |
|
521 |
||
522 |
return werkzeug.Response("""\ |
|
523 |
<html> |
|
524 |
<head> |
|
525 |
<title>Aaltologotin</title> |
|
526 |
<style type="text/css"> |
|
527 |
div#logo { |
|
528 |
text-align: center; |
|
529 |
padding: 100px; |
|
530 |
} |
|
531 |
||
532 |
img { |
|
533 |
border: none; |
|
534 |
} |
|
535 |
||
536 |
div#info { |
|
537 |
font-size: small; |
|
538 |
padding-left: 25px; |
|
539 |
margin-top: 50px; |
|
540 |
color: grey; |
|
541 |
} |
|
542 |
||
543 |
label { |
|
544 |
display: block; |
|
545 |
float: left; |
|
546 |
||
547 |
width: 150px; |
|
548 |
} |
|
549 |
||
550 |
div.param { |
|
551 |
padding: 3px; |
|
552 |
} |
|
553 |
</style> |
|
554 |
</head> |
|
555 |
<body> |
|
556 |
<div id='logo'> |
|
557 |
<a href="%(img_url)s"><img src="%(img_url)s" alt="Aaltologo" /></a> |
|
558 |
</div> |
|
559 |
<div id='info'> |
|
560 |
<h1>Aaltologotin</h1> |
|
561 |
<p>Aaltologotin pulauttaa sulle uuden, sattumanvaraisesti valitun aalto-logon!</p> |
|
562 |
||
563 |
<form action="." method="POST"> |
|
564 |
<fieldset> |
|
565 |
<legend>Aalto-parameterit</legend> |
|
566 |
%(form_fields)s |
|
567 |
<input type="submit" value="Logota!" /> |
|
568 |
</fieldset> |
|
569 |
</form> |
|
570 |
</div> |
|
571 |
</body> |
|
572 |
</html>""" % dict( |
|
573 |
img_url = img_url, |
|
574 |
form_fields = "\n".join( |
|
575 |
"\t%s" % opt.build_form(opts) for opt in options.options |
|
576 |
), |
|
577 |
), mimetype='text/html') |
|
578 |
||
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
579 |
def handle_request (req) : |
16 | 580 |
if req.path == '/' : |
581 |
return handle_index(OPTIONS, req) |
|
582 |
||
583 |
elif req.path.startswith('/logo') : |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
584 |
return handle_logo(req) |
3 | 585 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
586 |
elif req.path == '/tile' : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
587 |
return handle_tile(req) |
3 | 588 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
589 |
else : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
590 |
raise ValueError(req) |
3 | 591 |
|
592 |
@werkzeug.Request.application |
|
593 |
def wsgi_application (request) : |
|
0 | 594 |
""" |
3 | 595 |
Our werkzeug WSGI handler |
0 | 596 |
""" |
597 |
||
3 | 598 |
try : |
599 |
# request -> response |
|
600 |
response = handle_request(request) |
|
0 | 601 |
|
3 | 602 |
return response |
603 |
||
604 |
except HTTPException, e : |
|
605 |
# return as HTTP response |
|
606 |
return e |
|
0 | 607 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
608 |
def build_app () : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
609 |
""" |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
610 |
Build and return a WSGI application |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
611 |
""" |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
612 |
|
3 | 613 |
app = wsgi_application |
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
614 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
615 |
# add other middleware |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
616 |
app = werkzeug.SharedDataMiddleware(app, { |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
617 |
'/static': STATIC_PATH, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
618 |
}) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
619 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
620 |
|
6 | 621 |
if DEBUG : |
3 | 622 |
# enable debugging |
623 |
app = werkzeug.DebuggedApplication(app, evalex=False) |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
624 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
625 |
return app |
0 | 626 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
627 |
def main_cgi (app) : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
628 |
import wsgiref.handlers |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
629 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
630 |
handler = wsgiref.handlers.CGIHandler() |
3 | 631 |
handler.run(app) |
0 | 632 |
|
13
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
633 |
def main_fastcgi (app) : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
634 |
import flup.server.fcgi |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
635 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
636 |
server = flup.server.fcgi.WSGIServer(app, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
637 |
multithreaded = False, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
638 |
multiprocess = False, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
639 |
multiplexed = False, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
640 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
641 |
bindAddress = None, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
642 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
643 |
umask = None, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
644 |
debug = True, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
645 |
) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
646 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
647 |
server.run() |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
648 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
649 |
def main () : |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
650 |
from sys import argv |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
651 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
652 |
# get our file extension |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
653 |
root, ext = os.path.splitext(argv[0]) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
654 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
655 |
# get our handler |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
656 |
handler = { |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
657 |
'.cgi': main_cgi, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
658 |
'.fcgi': main_fastcgi, |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
659 |
}[ext] |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
660 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
661 |
# get our app |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
662 |
app = build_app() |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
663 |
|
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
664 |
# run |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
665 |
handler(app) |
a0cb32f3de3d
add silly crap (FastCGI, tiles.*)
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
666 |
|
0 | 667 |
if __name__ == '__main__' : |
668 |
main() |
|
669 |