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 if re.match(r'.+\.\css$', filename):
18 web.header("Content-Type","text/css; charset=utf-8")
20 web.header("Content-Type","text/html; charset=utf-8")
22 tmpl = jinja_env.get_template(filename + '.tmpl')
23 vars['homepath'] = web.ctx.homepath
25 print tmpl.render(vars)
27 # the url map for the application
28 urls = ( '/?', 'index',
29 '/ruler_([0-9\.]+)px_([0-9\.]+)([A-Za-z]+).(svg|png|jpg)', 'ruler_img',
30 '/show/(.*(svg|png|jpg))', 'show_ruler',
31 '/gallery(.*)', 'gallery',
32 '/delete/(\d+)', 'delete',
33 '/undelete/(\d+)', 'undelete',
36 database = create_database("mysql:yourule")
38 store = Store(database)
41 __storm_table__ = "gallery"
42 id = Int(primary=True)
50 def __init__(self, **kw):
51 self.pixel_width = float(kw['pixel_width'])
52 self.unit_width = float(kw['unit_width'])
53 self.units = unicode(kw['units'])
54 if kw.has_key('model'):
55 self.model = unicode(kw['model'])
60 if self.units == 'centimeters':
61 return self.unit_width
62 elif self.units == 'inches':
63 return(round(self.unit_width / self.cm_in_ratio, 2))
66 if self.units == 'inches':
67 return self.unit_width
68 elif self.units == 'centimeters':
69 return(round(self.unit_width * self.cm_in_ratio, 2))
72 return('ruler_%spx_%s%s.png' % (self.pixel_width,
73 self.unit_width, self.units))
78 render('index', locals())
83 errormsg = validate_input(input)
86 pixel_width = input['pixel_width']
87 unit_width = input['unit_width']
88 units = input['units']
89 render('index', locals())
91 ruler = Ruler(pixel_width = input['pixel_width'],
92 unit_width = input['unit_width'],
93 units = input['units'])
95 web.redirect('/show/%s' % ruler.url())
98 def GET(self, ruler_url, ext):
99 if web.input().has_key('fromgallery'):
104 other_unit, other_unit_url = get_other_unit(ruler_url)
106 render('show_ruler', locals())
109 def GET(self, pixel_width=None, unit_width=None, units=None, ext=None):
111 # TODO check to see if it's a format that we support
113 # set ruler height to be 200 px always
114 pixel_width = float(pixel_width)
115 unit_width = float(unit_width)
119 scale = pixel_width / unit_width
120 ruler_length = int(unit_width)
122 ruler = SVGRuler(scale, units, ruler_height, ruler_length)
125 if ext == 'svg': ext = 'svg+xml'
126 web.header("Content-Type", "image/%s" % ext)
129 sys.stdout.write(ruler.getxml())
131 pin, pout = os.popen2('convert -size %sx%s - %s:-' % \
132 (pixel_width, ruler_height, ext))
134 pin.write(ruler.getxml())
136 sys.stdout.write(pout.read())
139 def GET(self, ruler_url):
142 pixel_width, unit_width, units = process_ruler_url(ruler_url)[0:3]
144 new_rulers = store.find(Ruler, Ruler.visible == 1)#.order_by(Ruler.model)
147 for ruler in new_rulers:
150 #rulers = map(lambda x: x, rulers)
152 render('gallery', locals())
154 def POST(self, ruler_url):
157 errormsg = validate_input(input)
159 errormsg = 'Please fill out all fields.'
162 pixel_width = input['pixel_width']
163 unit_width = input['unit_width']
164 units = input['units']
165 model = input['model']
167 new_ruler = Ruler(pixel_width = input['pixel_width'],
168 unit_width = input['unit_width'],
169 units = input['units'],
170 model = input['model'])
175 rulers = store.find(Ruler, Ruler.visible == 1)
176 #rulers.order_by(Ruler.model)
177 render('gallery', locals())
181 ruler = store.get(Ruler, int(id))
184 web.redirect('/gallery')
189 ruler = store.get(Ruler, int(id))
192 web.redirect('/gallery')
198 def get_other_unit(url):
199 pixel_width, unit_width, units = process_ruler_url(url)[0:3]
201 ruler = Ruler(pixel_width=pixel_width, unit_width=unit_width, units=units)
202 pixel_width, unit_width, units = process_ruler_url(url)[0:3]
204 if units == 'centimeters':
206 unit_width = ruler.in_width()
207 elif units == 'inches':
208 units = 'centimeters'
209 unit_width = ruler.cm_width()
211 new_ruler = Ruler(pixel_width=pixel_width, unit_width=unit_width,
214 return(units, new_ruler.url())
217 def process_ruler_url(url):
218 url = re.sub(r'^/?(ruler.*)$', r'\1', url)
219 return(re.match( r'ruler_([\d\.]+)px_([\d\.]+)(\w+).(png|svg|jpg)',
222 def validate_input(input):
225 if not input.pixel_width \
226 or not input.unit_width:
227 errormsg = 'Please fill out all fields.'
228 elif not re.match('^[\d\.]+$', input.pixel_width) \
229 or not re.match('^[\d\.]+$', input.unit_width):
230 errormsg = "Widths must be numbers."
231 elif input['pixel_width'] < 0 \
232 or input['unit_width'] < 0:
233 errormsg = 'Widths must be greater than postive.'
238 web.webapi.internalerror = web.debugerror
239 if __name__ == "__main__":
240 web.run(urls, globals(), web.reloader)
242 application = web.wsgifunc(web.webpyfunc(urls, globals()))