noted the new URL for git repository
[yourule] / yourule.py
index c5ffdb7eeed914f187daf138d4cb31eee49c6dc4..0d8adbc8a281063ffefc8052be6d7c2750f54022 100755 (executable)
@@ -1,21 +1,58 @@
 #!/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(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',
          '/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("sqlite:%s/db/yourule.db" %
-                           os.path.dirname(__file__))
+database = create_database("mysql:yourule")
 
 store = Store(database)
 
@@ -26,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):
@@ -57,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()
@@ -69,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'], 
@@ -79,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:
@@ -87,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):
@@ -112,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):
@@ -127,9 +160,15 @@ 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()
@@ -152,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')
 
@@ -167,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]
 
@@ -187,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())
 
 
@@ -211,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()
 

Benjamin Mako Hill || Want to submit a patch?