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.)

This commit is contained in:
Alice Bevan-McGregor 2010-03-13 16:29:06 -05:00
parent 5f30af085e
commit b365e38212
6 changed files with 29 additions and 21 deletions

@ -24,13 +24,13 @@ class Dispatcher(object):
""" """
raise NotImplementedError 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 This is expected to be overridden by any subclass that wants to set
the routing_args. 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 This is expected to be overridden by any subclass that wants to set
the script name. the script name.

@ -1,7 +1,7 @@
""" """
This module implements the :class:`DispatchState` class This module implements the :class:`DispatchState` class
""" """
from util import odict from util import odict, Path
class DispatchState(object): class DispatchState(object):
""" """
@ -10,9 +10,12 @@ class DispatchState(object):
us to attach things like routing args and to keep track of the us to attach things like routing args and to keep track of the
path the controller takes along the system. path the controller takes along the system.
""" """
path = Path()
def __init__(self, request, dispatcher=None, params=None): def __init__(self, request, dispatcher=None, params=None):
self.request = request 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: if params is not None:
self.params = params self.params = params

@ -105,31 +105,35 @@ class ObjectDispatcher(Dispatcher):
tree until we found a method which matches with a default or lookup method. tree until we found a method which matches with a default or lookup method.
""" """
orig_url_path = state.url_path print state.path
if len(remainder): orig_path = state.path
state.url_path = state.url_path[:-len(remainder)]
for i in xrange(len(state.controller_path)): for i in xrange(len(state.controller_path)):
controller = state.controller controller = state.controller
if self._is_exposed(controller, '_default'): if self._is_exposed(controller, '_default'):
state.add_method(controller._default, remainder) state.add_method(controller._default, remainder)
state.dispatcher = self state.dispatcher = self
return state return state
if self._is_exposed(controller, '_lookup'): if self._is_exposed(controller, '_lookup'):
controller, remainder = controller._lookup(*remainder) controller, remainder = controller._lookup(*remainder)
last_tried_abstraction = getattr(self, '_last_tried_abstraction', None) last_tried_abstraction = getattr(self, '_last_tried_abstraction', None)
if type(last_tried_abstraction) != type(controller): if type(last_tried_abstraction) != type(controller):
self._last_tried_abstraction = controller self._last_tried_abstraction = controller
return self._dispatch_controller('_lookup', controller, state, remainder) return self._dispatch_controller('_lookup', controller, state, remainder)
if self._is_exposed(controller, 'index') and\ if self._is_exposed(controller, 'index') and\
method_matches_args(controller.index, state.params, remainder, self._use_lax_params): method_matches_args(controller.index, state.params, remainder, self._use_lax_params):
state.add_method(controller.index, remainder) state.add_method(controller.index, remainder)
state.dispatcher = self state.dispatcher = self
return state return state
state.controller_path.pop() state.controller_path.pop()
if len(state.url_path): if len(state.path):
remainder = list(remainder) remainder = list(remainder)
remainder.insert(0, state.url_path[-1]) remainder.insert(0, state.path[-1])
state.url_path.pop() state.path.pop()
raise HTTPNotFound raise HTTPNotFound
def _dispatch(self, state, remainder=None): def _dispatch(self, state, remainder=None):
@ -141,7 +145,7 @@ class ObjectDispatcher(Dispatcher):
state.dispatcher = self state.dispatcher = self
state.add_controller('/', self) state.add_controller('/', self)
if remainder is None: if remainder is None:
remainder = state.url_path remainder = state.path
current_controller = state.controller current_controller = state.controller
if hasattr(current_controller, '_check_security'): if hasattr(current_controller, '_check_security'):
@ -175,7 +179,7 @@ class ObjectDispatcher(Dispatcher):
#dispatch not found #dispatch not found
return self._dispatch_first_found_default_or_lookup(state, remainder) 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 This is expected to be overridden by any subclass that wants to set
the routing_args (RestController). Do not delete. the routing_args (RestController). Do not delete.

@ -11,8 +11,8 @@ class RestController(RestDispatcher):
verb = kw.get('_method', None) verb = kw.get('_method', None)
request = web.core.request request = web.core.request
url_path = '/'.join(args) path = '/'.join(args)
state = DispatchState(url_path, kw) state = DispatchState(path, kw)
state.request = request state.request = request
state.add_controller('/', self) state.add_controller('/', self)
state.dispatcher = self state.dispatcher = self

@ -19,7 +19,7 @@ class RestDispatcher(ObjectDispatcher):
if self._is_exposed(controller, method): if self._is_exposed(controller, method):
return getattr(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 pass
#request.environ['wsgiorg.routing_args'] = (tuple(remainder), params) #request.environ['wsgiorg.routing_args'] = (tuple(remainder), params)
@ -62,8 +62,8 @@ class RestDispatcher(ObjectDispatcher):
if sub_controller: if sub_controller:
remainder = remainder[1:] remainder = remainder[1:]
state.current_controller = sub_controller state.current_controller = sub_controller
state.url_path = '/'.join(remainder) state.path = '/'.join(remainder)
r = self._dispatch_controller(state.url_path, sub_controller, state, remainder) r = self._dispatch_controller(state.path, sub_controller, state, remainder)
if r: if r:
return r return r
return self._dispatch_first_found_default_or_lookup(state, remainder) return self._dispatch_first_found_default_or_lookup(state, remainder)
@ -144,8 +144,8 @@ class RestDispatcher(ObjectDispatcher):
if sub_controller: if sub_controller:
remainder = remainder[1:] remainder = remainder[1:]
state.current_controller = sub_controller state.current_controller = sub_controller
state.url_path = '/'.join(remainder) state.path = '/'.join(remainder)
r = self._dispatch_controller(state.url_path, sub_controller, state, remainder) r = self._dispatch_controller(state.path, sub_controller, state, remainder)
if r: if r:
return r return r
return self._dispatch_first_found_default_or_lookup(state, remainder) return self._dispatch_first_found_default_or_lookup(state, remainder)

@ -185,11 +185,12 @@ def method_matches_args(method, params, remainder, lax_params=False):
class Path(collections.deque): class Path(collections.deque):
def __init__(self, value='/', separator='/'): def __init__(self, value=None, separator='/'):
self.separator = separator self.separator = separator
super(Path, self).__init__() super(Path, self).__init__()
if value is not None:
self._assign(value) self._assign(value)
def _assign(self, value): def _assign(self, value):