3 from __future__ import division
6 from storm.locals import *
7 from ruler import RulerImage
9 # the url map for the application
10 urls = ( '/', 'index',
11 '/ruler_([0-9\.]+)px_([0-9\.]+)([A-Za-z]+).(svg|png|jpg)', 'ruler_img',
12 '/show/(.*(svg|png|jpg))', 'ruler',
13 '/gallery(.*)', 'gallery',
14 '/delete/(\d+)', 'delete',
15 '/undelete/(\d+)', 'undelete')
17 database = create_database("sqlite:ruler_web.db")
18 store = Store(database)
20 class SavedRuler(object):
21 __storm_table__ = "gallery"
22 id = Int(primary=True)
29 def __init__(self, **kw):
30 self.pixel_width = kw['pixel_width']
31 self.unit_width = kw['unit_width']
32 self.units = kw['units']
33 self.model = kw['model']
36 if self.units == 'centimeters':
37 return self.unit_width
38 elif self.units == 'inches':
39 return(self.unit_width * 2.54)
42 if self.units == 'inches':
43 return self.unit_width
44 elif self.units == 'centimeters':
45 return(self.unit_width * 0.3937)
50 web.header("Content-Type","text/html; charset=utf-8")
51 web.render('index.tmpl')
56 pixel_width = input['pixel_width']
57 unit_width = input['unit_width']
58 units = input['units']
59 ruler_url = 'ruler_%spx_%s%s.png' % (pixel_width, unit_width, units)
60 web.redirect('/show/%s' % ruler_url)
63 def GET(self, ruler_url, ext):
64 web.debug('test test')
65 if web.input().has_key('fromgallery'):
70 other_unit, other_unit_url = get_other_units(ruler_url)
72 web.header("Content-Type","text/html; charset=utf-8")
73 web.render('show_ruler.tmpl')
77 def GET(self, pixel_width=None, unit_width=None, units=None, ext=None):
79 # TODO check to see if it's a format that we support
81 # set ruler height to be 200 px always
82 pixel_width = float(pixel_width)
83 unit_width = float(unit_width)
87 scale = pixel_width / unit_width
88 ruler_length = int(unit_width)
90 ruler = Ruler(scale, units, ruler_height, ruler_length)
93 if ext == 'svg': ext = 'svg+xml'
94 web.header("Content-Type", "image/%s" % ext)
97 sys.stdout.write(ruler.output())
99 pin, pout = os.popen2('convert -size %sx%s - %s:-' % \
100 (pixel_width, ruler_height, ext))
102 pin.write(ruler.output())
104 sys.stdout.write(pout.read())
107 def GET(self, ruler_url):
110 pixel_width, unit_width, units = process_ruler_url(ruler_url)[0:3]
112 rulers = store.find(SavedRuler, SavedRuler.show == 1)
113 rulers.order_by(SavedRuler.model)
115 web.render('gallery.tmpl')
120 new_ruler = SavedRuler(pixel_width=float(input['pixel_width']),
121 unit_width=float(input['unit_width']),
122 units=unicode(input['units']),
123 model=unicode(input['model']))
128 web.redirect('gallery')
132 ruler = store.get(SavedRuler, int(id))
135 web.redirect('/gallery')
140 ruler = store.get(SavedRuler, int(id))
143 web.redirect('/gallery')
145 def get_other_units(url):
146 pixel_width, unit_width, units = process_ruler_url(url)[0:3]
147 ruler = SavedRuler(pixel_width=float(pixel_width),
148 unit_width=float(unit_width),
149 units=unicode(units),
150 model=unicode(model))
152 def process_ruler_url(url):
153 url = re.sub(r'^/?(ruler.*)$', r'\1', url)
154 return(re.match( r'ruler_([\d\.]+)px_([\d\.]+)(\w+).(png|svg|jpg)',
157 # render the site template here so that i can use it later
158 web.render('site.tmpl', None, True, 'site')
160 web.webapi.internalerror = web.debugerror
161 if __name__ == "__main__":
162 web.run(urls, globals(), web.reloader)