diff options
author | Jonathan LaCour <jonathan@cleverdevil.org> | 2010-09-29 00:01:01 -0400 |
---|---|---|
committer | Jonathan LaCour <jonathan@cleverdevil.org> | 2010-09-29 00:01:01 -0400 |
commit | 972579b68740fb98de05695eed0347031fd9366b (patch) | |
tree | 87a51de60ec91d007e93d3c730fc4292c6988f11 /pecan/routing.py | |
parent | 4061fa2f93a352f225580431963e5819462e7cc1 (diff) | |
download | pecan-972579b68740fb98de05695eed0347031fd9366b.tar.gz |
Switched over to Rick Copeland's iterative object-dispatch. Proved
everything still works with tests.
Diffstat (limited to 'pecan/routing.py')
-rw-r--r-- | pecan/routing.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/pecan/routing.py b/pecan/routing.py new file mode 100644 index 0000000..e596d8f --- /dev/null +++ b/pecan/routing.py @@ -0,0 +1,60 @@ +from webob import exc + + +def lookup_controller(obj, url_path): + remainder = url_path + notfound_handlers = [] + while True: + try: + obj, remainder = find_object(obj, remainder, notfound_handlers) + return obj, remainder + except exc.HTTPNotFound: + while notfound_handlers: + name, obj, remainder = notfound_handlers.pop() + if name == '_default': + # Notfound handler is, in fact, a controller, so stop + # traversal + return obj, remainder + else: + # Notfound handler is an internal redirect, so continue + # traversal + try: + result = obj(*remainder) + if result: + obj, remainder = result + break + except TypeError, te: + print 'Got exception calling lookup(): %s (%s)' % (te, te.args) + else: + raise exc.HTTPNotFound + + +def find_object(obj, remainder, notfound_handlers): + while True: + if obj is None: raise exc.HTTPNotFound + if iscontroller(obj): return obj, remainder + + if remainder and remainder[0] == '': + index = getattr(obj, 'index', None) + if iscontroller(index): return index, remainder[1:] + elif not remainder: + index = getattr(obj, 'index', None) + if iscontroller(index): + return index, remainder[1:] # TODO: why did I have to do this instead? + #raise exc.HTTPFound(add_slash=True) + + default = getattr(obj, '_default', None) + if iscontroller(default): + notfound_handlers.append(('_default', default, remainder)) + + lookup = getattr(obj, '_lookup', None) + if iscontroller(lookup): + notfound_handlers.append(('_lookup', lookup, remainder)) + + if not remainder: raise exc.HTTPNotFound + next, remainder = remainder[0], remainder[1:] + obj = getattr(obj, next, None) + + +def iscontroller(obj): + return getattr(obj, 'exposed', False)
\ No newline at end of file |