#!/usr/bin/env python
+# YouRule: Onscreen Ruler Generator
+#
+# Copyright (C) 2007 Benjamin Mako Hill <mako@atdot.cc>
+#
+# 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
+# <http://http://www.affero.org/oagpl.html>.
+
+
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('templates/'))
+ 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")
-def render(filename, vars):
- web.header("Content-Type","text/html; charset=utf-8")
tmpl = jinja_env.get_template(filename + '.tmpl')
- print tmpl.render(vars)
- web.debug(web.ctx)
+ vars['homepath'] = web.ctx.homepath
+ vars['ctx'] = web.ctx
+ return tmpl.render(vars)
# the url map for the application
urls = ( '/?', 'index',
'/show/(.*(svg|png|jpg))', 'show_ruler',
'/gallery(.*)', 'gallery',
'/delete/(\d+)', 'delete',
- '/undelete/(\d+)', 'undelete')
+ '/undelete/(\d+)', 'undelete',
+ '/style.css', 'css')
-database = create_database("sqlite:%s/db/yourule.db" %
- os.path.dirname(__file__))
+database = create_database("mysql:yourule")
store = Store(database)
unit_width = Float()
model = Unicode()
units = Unicode()
- show = Int()
+ visible = Int()
cm_in_ratio = 0.3937
def __init__(self, **kw):
class index:
def GET(self):
- render('index', locals())
+ return render('index', locals())
def POST(self):
input = web.input()
pixel_width = input['pixel_width']
unit_width = input['unit_width']
units = input['units']
- render('index', locals())
+ return render('index', locals())
else:
ruler = Ruler(pixel_width = input['pixel_width'],
unit_width = input['unit_width'],
other_unit, other_unit_url = get_other_unit(ruler_url)
- render('show_ruler', locals())
+ return render('show_ruler', locals())
class ruler_img:
def GET(self, pixel_width=None, unit_width=None, units=None, ext=None):
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):
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)
- render('gallery', locals())
+ 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()
store.add(new_ruler)
store.commit()
- rulers = store.find(Ruler, Ruler.show == 1)
- rulers.order_by(Ruler.model)
- render('gallery', locals())
+ 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')
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]
new_ruler = Ruler(pixel_width=pixel_width, unit_width=unit_width,
units=units)
- web.debug(units)
return(units, new_ruler.url())
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()