implement params
authorTero Marttila <terom@fixme.fi>
Tue, 05 May 2009 17:15:27 +0300
changeset 3 ac063212bd67
parent 2 50d95ac19dfc
child 4 c5bd0b75b59c
implement params
index.cgi
--- a/index.cgi	Tue May 05 16:33:59 2009 +0300
+++ b/index.cgi	Tue May 05 17:15:27 2009 +0300
@@ -1,35 +1,44 @@
 #!/usr/bin/python2.5
-import cgi, cgitb
-
-cgitb.enable()
+import werkzeug
+from werkzeug.exceptions import HTTPException
+import wsgiref.handlers
 
 from PIL import Image, ImageDraw, ImageFont, ImageEnhance
 from cStringIO import StringIO
 import random
 
 
-# settings
-text = [
-    "aalto",
-    "unive",
-    "rsity"
-]
+class Defaults :
+    # settings
+    text = [
+        "aalto",
+        "unive",
+        "rsity"
+    ]
 
-random_chars = [ '"', '!', '?' ]
+    chars = [ '"', '!', '?' ]
 
-line_colors = [
-    "#0469af",
-    "#fbc614",
-    "#e1313b",
-]
+    line_colors = [
+        "#0469af",
+        "#fbc614",
+        "#e1313b",
+    ]
+
+    font_name = 'helvetica'
+    font_size = 30
+    
+    background_color = "#ffffff"
+    line_spacing = -10
+    sharpness = 0.6
 
 fonts = {
-    'dejavu-sans-bold':     "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf",
-    'helvetica':            "HELR65W.TTF",
-}
+        'dejavu-sans-bold':     "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf",
+        'helvetica':            "HELR65W.TTF",
+    }
 
-font_name = 'helvetica'
-font_size = 30
+
+# enable debugging
+debug = True
 
 def randomize (seq) :
     """
@@ -135,9 +144,9 @@
     
     return img
 
-def effect_smooth (img, factor) :
+def effect_sharpness (img, factor) :
     """
-        De-sharpen the image by the given factor
+        Sharpen the image by the given factor
     """
 
     return ImageEnhance.Sharpness(img).enhance(factor)
@@ -154,27 +163,91 @@
 
     return data
 
-def response (type, data) :
+def arg_bool (val) :
+    if val.lower() in ('true', 't', '1', 'yes', 'y') :
+        return True
+    elif val.lower() in ('false', 'f', '0', 'no', 'n') :
+        return False
+    else :
+        raise ValueError(val)
+
+def arg_color (val) :
+    if val.beginswith('#') :
+        int(val[1:], 16)
+
+        return val
+    else :
+        raise ValueError(val)
+
+def handle_request (req) :
+    # parse args
+    if 'text' in req.args :
+        text = req.args.getlist('text', unicode)
+    else :
+        text = Defaults.text
+    
+    if 'chars' in req.args :
+        chars = req.args.getlist('chars', unicode)
+    else :
+        chars = Defaults.chars
+
+    if 'colors' in req.args :
+        colors = req.args.getlist('colors', arg_color)
+
+    else :
+        colors = Defaults.line_colors
+
+    font_name = req.args.get('font', Defaults.font_name)
+    font_size = req.args.get('font-size', Defaults.font_size, int)
+    background_color = req.args.get('background-color', Defaults.background_color, arg_color)
+    line_spacing = req.args.get('line-spacing', Defaults.line_spacing, int)
+    sharpness = req.args.get('sharpness', Defaults.sharpness, float)
+    
+    # put the chars in random order by default
+    if req.args.get('random_chars', True, arg_bool) :
+        chars = randomize(chars)
+
+    # load/prep resources
+    data = build_data(text, chars, colors)
+    font = load_font(font_name, font_size)
+    
+    # render the image
+    img = render_img(data, font, background_color, line_spacing)
+
+    img = effect_sharpness(img, sharpness)
+
+    png_data = build_png(img)
+    
+    # build the response
+    response = werkzeug.Response(png_data, mimetype='image/png')
+
+    return response
+
+@werkzeug.Request.application
+def wsgi_application (request) :
     """
-        Write out the HTTP Response
+        Our werkzeug WSGI handler
     """
 
-    from sys import stdout
+    try :
+        # request -> response
+        response = handle_request(request)
 
-    stdout.write("Content-Type: %s\r\n" % type)
-    stdout.write("Content-Length: %d\r\n" % len(data))
-    stdout.write("\r\n")
-    stdout.write(data)
+        return response
+
+    except HTTPException, e :
+        # return as HTTP response
+        return e
 
 def main () :
-    data = build_data(text, randomize(random_chars), line_colors)
-    font = load_font(font_name, font_size)
+    handler = wsgiref.handlers.CGIHandler()
+    app = wsgi_application
+    
+    if debug :
+        # enable debugging
+        app = werkzeug.DebuggedApplication(app, evalex=False)
 
-    img = render_img(data, font, line_spacing=-10)
-    img = effect_smooth(img, 0.6)
-    png_data = build_png(img)
-
-    response("image/png", png_data)
+    handler.run(app)
 
 if __name__ == '__main__' :
     main()