Package gluon :: Module rewrite
[hide private]
[frames] | no frames]

Source Code for Module gluon.rewrite

 1  import os, re 
 2   
3 -def rewrite(wsgibase,URL):
4 if not os.access('routes.py',os.R_OK): 5 return wsgibase,URL 6 print '***************** ATTENTION *******************' 7 print '* you are using web2py rewrite feature *' 8 print '* the use of this feature is discouraged *' 9 print '* unless you really know what you are doing *' 10 print '***********************************************' 11 symbols={} 12 exec(open('routes.py','r').read()) in symbols 13 routes_in=[(re.compile(k),v) for k,v in symbols['routes_in']] 14 routes_out=[(re.compile(k),v) for k,v in symbols['routes_out']] 15 def filter_in(e): 16 path=e['PATH_INFO'] 17 key=e['REMOTE_ADDR']+':'+path 18 for regex,value in routes_in: 19 if regex.match(key): 20 path=regex.sub(value,key) 21 e['PATH_INFO']=path 22 return e
23 def filter_out(url): 24 items=url.split('?',1) 25 for regex,value in routes_out: 26 if regex.match(items[0]): return '?'.join([regex.sub(value,items[0])]+items[1:]) 27 return url 28 wsgibase_new=lambda e,r: wsgibase(filter_in(e),r) 29 URL_new=lambda *a,**b: filter_out(URL(*a,**b)) 30 return wsgibase_new, URL_new 31