3 from __future__ import division
6 from storm.locals import *
8 # recalculate the path based on the location of the file
9 sys.path.append(os.path.dirname(__file__))
10 from svgruler import SVGRuler
12 from jinja import Environment, FileSystemLoader
13 jinja_env = Environment('<%', '%>', '<%=', '%>', '<%#', '%>',
14 loader=FileSystemLoader(os.path.dirname(__file__) + "/templates/"))
16 def render(filename, vars):
17 web.header("Content-Type","text/html; charset=utf-8")
18 tmpl = jinja_env.get_template(filename + '.tmpl')
19 print tmpl.render(vars)
22 # the url map for the application
23 urls = ( '/?', 'index',
24 '/ruler_([0-9\.]+)px_([0-9\.]+)([A-Za-z]+).(svg|png|jpg)', 'ruler_img',
25 '/show/(.*(svg|png|jpg))', 'show_ruler',
26 '/gallery(.*)', 'gallery',
27 '/delete/(\d+)', 'delete',
28 '/undelete/(\d+)', 'undelete')
30 database = create_database("sqlite:%s/db/yourule.db" %
31 os.path.dirname(__file__))
33 store = Store(database)
36 __storm_table__ = "gallery"
37 id = Int(primary=True)
45 def __init__(self, **kw):
46 self.pixel_width = float(kw['pixel_width'])
47 self.unit_width = float(kw['unit_width'])
48 self.units = unicode(kw['units'])
49 if kw.has_key('model'):
50 self.model = unicode(kw['model'])
55 if self.units == 'centimeters':
56 return self.unit_width
57 elif self.units == 'inches':
58 return(round(self.unit_width / self.cm_in_ratio, 2))
61 if self.units == 'inches':
62 return self.unit_width
63 elif self.units == 'centimeters':
64 return(round(self.unit_width * self.cm_in_ratio, 2))
67 return('ruler_%spx_%s%s.png' % (self.pixel_width,
68 self.unit_width, self.units))
73 render('index', locals())
78 errormsg = validate_input(input)
81 pixel_width = input['pixel_width']
82 unit_width = input['unit_width']
83 units = input['units']
84 render('index', locals())
86 ruler = Ruler(pixel_width = input['pixel_width'],
87 unit_width = input['unit_width'],
88 units = input['units'])
90 web.redirect('/show/%s' % ruler.url())
93 def GET(self, ruler_url, ext):
94 if web.input().has_key('fromgallery'):
99 other_unit, other_unit_url = get_other_unit(ruler_url)
101 render('show_ruler', locals())
104 def GET(self, pixel_width=None, unit_width=None, units=None, ext=None):
106 # TODO check to see if it's a format that we support
108 # set ruler height to be 200 px always
109 pixel_width = float(pixel_width)
110 unit_width = float(unit_width)
114 scale = pixel_width / unit_width
115 ruler_length = int(unit_width)
117 ruler = SVGRuler(scale, units, ruler_height, ruler_length)
120 if ext == 'svg': ext = 'svg+xml'
121 web.header("Content-Type", "image/%s" % ext)
124 sys.stdout.write(ruler.getxml())
126 pin, pout = os.popen2('convert -size %sx%s - %s:-' % \
127 (pixel_width, ruler_height, ext))
129 pin.write(ruler.getxml())
131 sys.stdout.write(pout.read())
134 def GET(self, ruler_url):
137 pixel_width, unit_width, units = process_ruler_url(ruler_url)[0:3]
139 rulers = store.find(Ruler, Ruler.show == 1)
140 rulers.order_by(Ruler.model)
141 render('gallery', locals())
143 def POST(self, ruler_url):
146 errormsg = validate_input(input)
148 errormsg = 'Please fill out all fields.'
151 pixel_width = input['pixel_width']
152 unit_width = input['unit_width']
153 units = input['units']
154 model = input['model']
156 new_ruler = Ruler(pixel_width = input['pixel_width'],
157 unit_width = input['unit_width'],
158 units = input['units'],
159 model = input['model'])
164 rulers = store.find(Ruler, Ruler.show == 1)
165 rulers.order_by(Ruler.model)
166 render('gallery', locals())
170 ruler = store.get(Ruler, int(id))
173 web.redirect('/gallery')
178 ruler = store.get(Ruler, int(id))
181 web.redirect('/gallery')
183 def get_other_unit(url):
184 pixel_width, unit_width, units = process_ruler_url(url)[0:3]
186 ruler = Ruler(pixel_width=pixel_width, unit_width=unit_width, units=units)
187 pixel_width, unit_width, units = process_ruler_url(url)[0:3]
189 if units == 'centimeters':
191 unit_width = ruler.in_width()
192 elif units == 'inches':
193 units = 'centimeters'
194 unit_width = ruler.cm_width()
196 new_ruler = Ruler(pixel_width=pixel_width, unit_width=unit_width,
200 return(units, new_ruler.url())
203 def process_ruler_url(url):
204 url = re.sub(r'^/?(ruler.*)$', r'\1', url)
205 return(re.match( r'ruler_([\d\.]+)px_([\d\.]+)(\w+).(png|svg|jpg)',
208 def validate_input(input):
211 if not input.pixel_width \
212 or not input.unit_width:
213 errormsg = 'Please fill out all fields.'
214 elif not re.match('^[\d\.]+$', input.pixel_width) \
215 or not re.match('^[\d\.]+$', input.unit_width):
216 errormsg = "Widths must be numbers."
217 elif input['pixel_width'] < 0 \
218 or input['unit_width'] < 0:
219 errormsg = 'Widths must be greater than postive.'
224 web.webapi.internalerror = web.debugerror
225 if __name__ == "__main__":
226 web.run(urls, globals(), web.reloader)
228 application = web.wsgifunc(web.webpyfunc(urls, globals()))