From b365e382128fd3d85a8757bd550d7c172fc22012 Mon Sep 17 00:00:00 2001 From: Alice Bevan-McGregor Date: Sat, 13 Mar 2010 16:29:06 -0500 Subject: [PATCH] Migrated to using `Path` objects internally, removed an unused feature that was causing tests to fail, minor code cleanup (adding blank lines for readability), and renamed `state.url_path` to `state.path` globally. (As opposed to the previous commit which did not do what it said.) --- crank/dispatcher.py | 4 ++-- crank/dispatchstate.py | 7 +++++-- crank/objectdispatcher.py | 20 ++++++++++++-------- crank/restcontroller.py | 4 ++-- crank/restdispatcher.py | 10 +++++----- crank/util.py | 5 +++-- 6 files changed, 29 insertions(+), 21 deletions(-) diff --git a/crank/dispatcher.py b/crank/dispatcher.py index 86dda04..906348e 100644 --- a/crank/dispatcher.py +++ b/crank/dispatcher.py @@ -24,13 +24,13 @@ class Dispatcher(object): """ raise NotImplementedError - def _setup_wsgiorg_routing_args(self, url_path, remainder, params): + def _setup_wsgiorg_routing_args(self, path, remainder, params): """ This is expected to be overridden by any subclass that wants to set the routing_args. """ - def _setup_wsgi_script_name(self, url_path, remainder, params): + def _setup_wsgi_script_name(self, path, remainder, params): """ This is expected to be overridden by any subclass that wants to set the script name. diff --git a/crank/dispatchstate.py b/crank/dispatchstate.py index 4bd59ae..539821f 100644 --- a/crank/dispatchstate.py +++ b/crank/dispatchstate.py @@ -1,7 +1,7 @@ """ This module implements the :class:`DispatchState` class """ -from util import odict +from util import odict, Path class DispatchState(object): """ @@ -10,9 +10,12 @@ class DispatchState(object): us to attach things like routing args and to keep track of the path the controller takes along the system. """ + path = Path() + def __init__(self, request, dispatcher=None, params=None): self.request = request - self.url_path = request.path_info.split('/')[1:] + self.path = request.path_info + self.path = self.path[1:] if params is not None: self.params = params diff --git a/crank/objectdispatcher.py b/crank/objectdispatcher.py index d4981a7..13ecb75 100644 --- a/crank/objectdispatcher.py +++ b/crank/objectdispatcher.py @@ -105,31 +105,35 @@ class ObjectDispatcher(Dispatcher): tree until we found a method which matches with a default or lookup method. """ - orig_url_path = state.url_path - if len(remainder): - state.url_path = state.url_path[:-len(remainder)] + print state.path + orig_path = state.path + for i in xrange(len(state.controller_path)): controller = state.controller if self._is_exposed(controller, '_default'): state.add_method(controller._default, remainder) state.dispatcher = self return state + if self._is_exposed(controller, '_lookup'): controller, remainder = controller._lookup(*remainder) last_tried_abstraction = getattr(self, '_last_tried_abstraction', None) if type(last_tried_abstraction) != type(controller): self._last_tried_abstraction = controller return self._dispatch_controller('_lookup', controller, state, remainder) + if self._is_exposed(controller, 'index') and\ method_matches_args(controller.index, state.params, remainder, self._use_lax_params): state.add_method(controller.index, remainder) state.dispatcher = self return state + state.controller_path.pop() - if len(state.url_path): + if len(state.path): remainder = list(remainder) - remainder.insert(0, state.url_path[-1]) - state.url_path.pop() + remainder.insert(0, state.path[-1]) + state.path.pop() + raise HTTPNotFound def _dispatch(self, state, remainder=None): @@ -141,7 +145,7 @@ class ObjectDispatcher(Dispatcher): state.dispatcher = self state.add_controller('/', self) if remainder is None: - remainder = state.url_path + remainder = state.path current_controller = state.controller if hasattr(current_controller, '_check_security'): @@ -175,7 +179,7 @@ class ObjectDispatcher(Dispatcher): #dispatch not found return self._dispatch_first_found_default_or_lookup(state, remainder) - def _setup_wsgiorg_routing_args(self, url_path, remainder, params): + def _setup_wsgiorg_routing_args(self, path, remainder, params): """ This is expected to be overridden by any subclass that wants to set the routing_args (RestController). Do not delete. diff --git a/crank/restcontroller.py b/crank/restcontroller.py index 6ab9adb..c9236fe 100644 --- a/crank/restcontroller.py +++ b/crank/restcontroller.py @@ -11,8 +11,8 @@ class RestController(RestDispatcher): verb = kw.get('_method', None) request = web.core.request - url_path = '/'.join(args) - state = DispatchState(url_path, kw) + path = '/'.join(args) + state = DispatchState(path, kw) state.request = request state.add_controller('/', self) state.dispatcher = self diff --git a/crank/restdispatcher.py b/crank/restdispatcher.py index 174ac49..b57ec98 100644 --- a/crank/restdispatcher.py +++ b/crank/restdispatcher.py @@ -19,7 +19,7 @@ class RestDispatcher(ObjectDispatcher): if self._is_exposed(controller, method): return getattr(controller, method) - def _setup_wsgiorg_routing_args(self, url_path, remainder, params): + def _setup_wsgiorg_routing_args(self, path, remainder, params): pass #request.environ['wsgiorg.routing_args'] = (tuple(remainder), params) @@ -62,8 +62,8 @@ class RestDispatcher(ObjectDispatcher): if sub_controller: remainder = remainder[1:] state.current_controller = sub_controller - state.url_path = '/'.join(remainder) - r = self._dispatch_controller(state.url_path, sub_controller, state, remainder) + state.path = '/'.join(remainder) + r = self._dispatch_controller(state.path, sub_controller, state, remainder) if r: return r return self._dispatch_first_found_default_or_lookup(state, remainder) @@ -144,8 +144,8 @@ class RestDispatcher(ObjectDispatcher): if sub_controller: remainder = remainder[1:] state.current_controller = sub_controller - state.url_path = '/'.join(remainder) - r = self._dispatch_controller(state.url_path, sub_controller, state, remainder) + state.path = '/'.join(remainder) + r = self._dispatch_controller(state.path, sub_controller, state, remainder) if r: return r return self._dispatch_first_found_default_or_lookup(state, remainder) diff --git a/crank/util.py b/crank/util.py index 1f06f15..cc05665 100644 --- a/crank/util.py +++ b/crank/util.py @@ -185,12 +185,13 @@ def method_matches_args(method, params, remainder, lax_params=False): class Path(collections.deque): - def __init__(self, value='/', separator='/'): + def __init__(self, value=None, separator='/'): self.separator = separator super(Path, self).__init__() - self._assign(value) + if value is not None: + self._assign(value) def _assign(self, value): separator = self.separator