Skip to content

Instantly share code, notes, and snippets.

@gilsondev
Forked from rochacbruno/routes.py
Created July 15, 2011 22:01
Show Gist options
  • Save gilsondev/1085645 to your computer and use it in GitHub Desktop.
Save gilsondev/1085645 to your computer and use it in GitHub Desktop.
router example
#!/usr/bin/python
# -*- coding: utf-8 -*-
default_application = 'init' # ordinarily set in base routes.py
default_controller = 'default' # ordinarily set in app-specific routes.py
default_function = 'index' # ordinarily set in app-specific routes.py
routers = dict(
# base router
BASE = dict(
default_application = 'init',
domains = {
'cursodepython.com.br' : 'init',
'www.cursodepython.com.br':'init',
},
applications = ['init','admin'],
controllers = 'DEFAULT'
),
)
# Routes In
routes_in = (
(r'/static/(?P<file>[\w./-]+)', r'/init/static/\g<file>'),
(r'/app01',r'/init/app01/index'),
(r'/app01/$arg',r'/init/app01/edit/$arg'),
)
# Routes Out
routes_out = (
(r'/init/app01/index',r'/app01'),
(r'/init/app01/edit/$arg',r'/app01/$arg'),
)
# Error-handling redirects all HTTP errors (status codes >= 400) to a specified
# path. If you wish to use error-handling redirects, uncomment the tuple
# below. You can customize responses by adding a tuple entry with the first
# value in 'appName/HTTPstatusCode' format. ( Only HTTP codes >= 400 are
# routed. ) and the value as a path to redirect the user to. You may also use
# '*' as a wildcard.
#
# The error handling page is also passed the error code and ticket as
# variables. Traceback information will be stored in the ticket.
#
routes_onerror = [
(r'init/400', r'/init/default/ohmygod')
,(r'init/*', r'/init/default/ohmygod')
,(r'*/404', r'/error/default/ohmygod')
,(r'*/*', r'/error/default/ohmygod')
]
# specify action in charge of error handling
#
# error_handler = dict(application='error',
# controller='default',
# function='index')
# In the event that the error-handling page itself returns an error, web2py will
# fall back to its old static responses. You can customize them here.
# ErrorMessageTicket takes a string format dictionary containing (only) the
# "ticket" key.
# error_message = '<html><body><h1>Invalid request</h1></body></html>'
# error_message_ticket = '<html><body><h1>Internal error</h1>Ticket issued: <a href="/admin/default/ticket/%(ticket)s" target="_blank">%(ticket)s</a></body></html>'
# specify a list of apps that bypass args-checking and use request.raw_args
#
#routes_apps_raw=['myapp']
#routes_apps_raw=['myapp', 'myotherapp']
def __routes_doctest():
'''
Dummy function for doctesting routes.py.
Use filter_url() to test incoming or outgoing routes;
filter_err() for error redirection.
filter_url() accepts overrides for method and remote host:
filter_url(url, method='get', remote='0.0.0.0', out=False)
filter_err() accepts overrides for application and ticket:
filter_err(status, application='app', ticket='tkt')
>>> import os
>>> import gluon.main
>>> from gluon.rewrite import regex_select, load, filter_url, regex_filter_out, filter_err, compile_regex
>>> regex_select()
>>> load(routes=os.path.basename(__file__))
>>> os.path.relpath(filter_url('http://domain.com/favicon.ico'))
'applications/examples/static/favicon.ico'
>>> os.path.relpath(filter_url('http://domain.com/robots.txt'))
'applications/examples/static/robots.txt'
>>> filter_url('http://domain.com')
'/init/default/index'
>>> filter_url('http://domain.com/')
'/init/default/index'
>>> filter_url('http://domain.com/init/default/fcn')
'/init/default/fcn'
>>> filter_url('http://domain.com/init/default/fcn/')
'/init/default/fcn'
>>> filter_url('http://domain.com/app/ctr/fcn')
'/app/ctr/fcn'
>>> filter_url('http://domain.com/app/ctr/fcn/arg1')
"/app/ctr/fcn ['arg1']"
>>> filter_url('http://domain.com/app/ctr/fcn/arg1/')
"/app/ctr/fcn ['arg1']"
>>> filter_url('http://domain.com/app/ctr/fcn/arg1//')
"/app/ctr/fcn ['arg1', '']"
>>> filter_url('http://domain.com/app/ctr/fcn//arg1')
"/app/ctr/fcn ['', 'arg1']"
>>> filter_url('HTTP://DOMAIN.COM/app/ctr/fcn')
'/app/ctr/fcn'
>>> filter_url('http://domain.com/app/ctr/fcn?query')
'/app/ctr/fcn ?query'
>>> filter_url('http://otherdomain.com/fcn')
'/app/ctr/fcn'
>>> regex_filter_out('/app/ctr/fcn')
'/ctr/fcn'
>>> filter_url('https://otherdomain.com/app/ctr/fcn', out=True)
'/ctr/fcn'
>>> filter_url('https://otherdomain.com/app/ctr/fcn/arg1//', out=True)
'/ctr/fcn/arg1//'
>>> filter_url('http://otherdomain.com/app/ctr/fcn', out=True)
'/fcn'
>>> filter_url('http://otherdomain.com/app/ctr/fcn?query', out=True)
'/fcn?query'
>>> filter_url('http://otherdomain.com/app/ctr/fcn#anchor', out=True)
'/fcn#anchor'
>>> filter_err(200)
200
>>> filter_err(399)
399
>>> filter_err(400)
400
>>> filter_url('http://domain.com/welcome', app=True)
'welcome'
>>> filter_url('http://domain.com/', app=True)
'myapp'
>>> filter_url('http://domain.com', app=True)
'myapp'
>>> compile_regex('.*http://otherdomain.com.* (?P<any>.*)', '/app/ctr\g<any>')[0].pattern
'^.*http://otherdomain.com.* (?P<any>.*)$'
>>> compile_regex('.*http://otherdomain.com.* (?P<any>.*)', '/app/ctr\g<any>')[1]
'/app/ctr\\\\g<any>'
>>> compile_regex('/$c/$f', '/init/$c/$f')[0].pattern
'^.*?:https?://[^:/]+:[a-z]+ /(?P<c>\\\\w+)/(?P<f>\\\\w+)$'
>>> compile_regex('/$c/$f', '/init/$c/$f')[1]
'/init/\\\\g<c>/\\\\g<f>'
'''
pass
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment