X-Git-Url: https://projects.mako.cc/source/yourule/blobdiff_plain/1711747333c7666b644f6124de39c49cd0b26526..a2e3dac07d377ccc15d31727c0648613b319ee81:/yourule.py diff --git a/yourule.py b/yourule.py index 9010217..0d8adbc 100755 --- a/yourule.py +++ b/yourule.py @@ -1,20 +1,59 @@ #!/usr/bin/env python +# YouRule: Onscreen Ruler Generator +# +# Copyright (C) 2007 Benjamin Mako Hill +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the Affero General Public License as published +# by the Free Software Foundation, either version 1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the Affero General Public License +# along with this program. If not, see +# . + + from __future__ import division import web import sys, os, re from storm.locals import * + +# recalculate the path based on the location of the file +sys.path.append(os.path.dirname(__file__)) from svgruler import SVGRuler +from jinja import Environment, FileSystemLoader +jinja_env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>', + loader=FileSystemLoader(os.path.dirname(__file__) + "/templates/")) + +def render(filename, vars={}): + if re.match(r'.+\.\css$', filename): + web.header("Content-Type","text/css; charset=utf-8") + else: + web.header("Content-Type","text/html; charset=utf-8") + + tmpl = jinja_env.get_template(filename + '.tmpl') + vars['homepath'] = web.ctx.homepath + vars['ctx'] = web.ctx + return tmpl.render(vars) + # the url map for the application -urls = ( '/', 'index', +urls = ( '/?', 'index', '/ruler_([0-9\.]+)px_([0-9\.]+)([A-Za-z]+).(svg|png|jpg)', 'ruler_img', '/show/(.*(svg|png|jpg))', 'show_ruler', '/gallery(.*)', 'gallery', '/delete/(\d+)', 'delete', - '/undelete/(\d+)', 'undelete') + '/undelete/(\d+)', 'undelete', + '/style.css', 'css') + +database = create_database("mysql:yourule") -database = create_database("sqlite:yourule.db") store = Store(database) class Ruler(object): @@ -24,7 +63,7 @@ class Ruler(object): unit_width = Float() model = Unicode() units = Unicode() - show = Int() + visible = Int() cm_in_ratio = 0.3937 def __init__(self, **kw): @@ -55,8 +94,7 @@ class Ruler(object): class index: def GET(self): - web.header("Content-Type","text/html; charset=utf-8") - web.render('index.tmpl') + return render('index', locals()) def POST(self): input = web.input() @@ -67,7 +105,7 @@ class index: pixel_width = input['pixel_width'] unit_width = input['unit_width'] units = input['units'] - web.render('index.tmpl') + return render('index', locals()) else: ruler = Ruler(pixel_width = input['pixel_width'], unit_width = input['unit_width'], @@ -77,7 +115,6 @@ class index: class show_ruler: def GET(self, ruler_url, ext): - web.debug('test test') if web.input().has_key('fromgallery'): fromgallery = True else: @@ -85,9 +122,7 @@ class show_ruler: other_unit, other_unit_url = get_other_unit(ruler_url) - web.header("Content-Type","text/html; charset=utf-8") - web.render('show_ruler.tmpl') - + return render('show_ruler', locals()) class ruler_img: def GET(self, pixel_width=None, unit_width=None, units=None, ext=None): @@ -110,14 +145,14 @@ class ruler_img: web.header("Content-Type", "image/%s" % ext) if ext == 'svg+xml': - sys.stdout.write(ruler.getxml()) + return(ruler.getxml()) else: pin, pout = os.popen2('convert -size %sx%s - %s:-' % \ (pixel_width, ruler_height, ext)) pin.write(ruler.getxml()) pin.close() - sys.stdout.write(pout.read()) + return(pout.read()) class gallery: def GET(self, ruler_url): @@ -125,14 +160,20 @@ class gallery: if ruler_url: pixel_width, unit_width, units = process_ruler_url(ruler_url)[0:3] - rulers = store.find(Ruler, Ruler.show == 1) - rulers.order_by(Ruler.model) - web.render('gallery.tmpl') + new_rulers = store.find(Ruler, Ruler.visible == 1)#.order_by(Ruler.model) + + rulers = [] + for ruler in new_rulers: + rulers.append(ruler) + + #rulers = map(lambda x: x, rulers) + + return render('gallery', locals()) def POST(self, ruler_url): input = web.input() - errormsg = valid_input(input) + errormsg = validate_input(input) if not input.model: errormsg = 'Please fill out all fields.' @@ -150,14 +191,14 @@ class gallery: store.add(new_ruler) store.commit() - rulers = store.find(Ruler, Ruler.show == 1) - rulers.order_by(Ruler.model) - web.render('gallery.tmpl') + rulers = store.find(Ruler, Ruler.visible == 1) + #rulers.order_by(Ruler.model) + return render('gallery', locals()) class delete: def GET(self, id): ruler = store.get(Ruler, int(id)) - ruler.show = 0 + ruler.visible = 0 store.commit() web.redirect('/gallery') @@ -165,10 +206,14 @@ class delete: class undelete: def GET(self, id): ruler = store.get(Ruler, int(id)) - ruler.show = 1 + ruler.visible = 1 store.commit() web.redirect('/gallery') +class css: + def GET(self): + return render('style.css') + def get_other_unit(url): pixel_width, unit_width, units = process_ruler_url(url)[0:3] @@ -185,7 +230,6 @@ def get_other_unit(url): new_ruler = Ruler(pixel_width=pixel_width, unit_width=unit_width, units=units) - web.debug(units) return(units, new_ruler.url()) @@ -209,12 +253,11 @@ def validate_input(input): return(errormsg) -# render the site template here so that i can use it later -web.render('site.tmpl', None, True, 'site') web.webapi.internalerror = web.debugerror if __name__ == "__main__": web.run(urls, globals(), web.reloader) -application = web.wsgifunc(web.webpyfunc(urls, globals())) +app = web.application(urls, globals(), autoreload=False) +application = app.wsgifunc()