Skip to content

Instantly share code, notes, and snippets.

@jzellman
Created September 8, 2009 20:05
Show Gist options
  • Save jzellman/183183 to your computer and use it in GitHub Desktop.
Save jzellman/183183 to your computer and use it in GitHub Desktop.
from sauce import application, get, post, delete, put, urls
import web
@get("/")
def index():
return "Hello from /6"
@get("/login")
def login():
raise web.seeother("/")
@post("/login/create")
def new_session():
pass
@get("/users/(.*)")
def user_profile(username):
pass
@delete("/foos/([0-9*])")
def delete_foo(foo):
pass
app = application()
if __name__ == "__main__":
app.run()
import web
import types
urls = {}
class application(web.application):
def handle(self):
method = web.ctx.method
urls[method]
fn, args = self._match(urls[method], web.ctx.path)
return self._delegate(fn, self.fvars, args)
def _delegate(self, fn, fvars, args=[]):
if fn and isinstance(fn, (types.FunctionType, type)):
return fn(args) if args else fn()
else:
return web.application._delegate(self, fn, fvars, args)
def register(meth, path, fun):
path_to_funs = urls.get(meth, [])
# only allow path in array once
# this also allows fun to be reloaded
if path in path_to_funs:
print "Found path at %d" %(path_to_funs.index(path))
path_index = path_to_funs.index(path)
path_to_funs[path_index + 1] = fun
else:
path_to_funs += [path, fun]
urls[meth] = path_to_funs
class verb:
def __init__(self, path):
self.path = path
def __call__(self, *args):
register(self.__class__.__name__.upper(), self.path, args[0])
class get(verb): pass
class post(verb): pass
class delete(verb): pass
class put(verb): pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment