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

View File

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

View File

@ -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

View File

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

View File

@ -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

View File

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

View File

@ -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