Skip to content

Commit

Permalink
add support for kwargs-based dispatch for URLSpec matches
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Monroy committed Apr 21, 2011
1 parent eadef9a commit 880a9e6
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions cyclone/web.py
Expand Up @@ -55,7 +55,7 @@ class RequestHandler(object):
SUPPORTED_METHODS = ("GET", "HEAD", "POST", "DELETE", "PUT")

no_keep_alive = False
def __init__(self, application, request, transforms=None):
def __init__(self, application, request, transforms=None, **kwargs):
self.application = application
self.request = request
self._headers_written = False
Expand All @@ -68,7 +68,12 @@ def __init__(self, application, request, transforms=None):
application.ui_modules.iteritems())
self.clear()
self.request.connection.no_keep_alive = self.no_keep_alive

self.initialize(**kwargs)

# override for subclass initialization hook
def initialize(self, **kwargs):
pass

@property
def settings(self):
return self.application.settings
Expand Down Expand Up @@ -1239,8 +1244,21 @@ def __call__(self, request):
for spec in handlers:
match = spec.regex.match(request.path)
if match:
# None-safe wrapper around urllib.unquote to handle
# unmatched optional groups correctly
def unquote(s):
if s is None: return s
return urllib.unquote(s)
handler = spec.handler_class(self, request, **spec.kwargs)
args = match.groups()
# Pass matched groups to the handler. Since
# match.groups() includes both named and unnamed groups,
# we want to use either groups or groupdict but not both.
kwargs = dict((k, unquote(v))
for (k, v) in match.groupdict().iteritems())
if kwargs:
args = []
else:
args = [unquote(s) for s in match.groups()]
break
if not handler:
handler = ErrorHandler(self, request, 404)
Expand All @@ -1251,7 +1269,7 @@ def __call__(self, request):
RequestHandler._templates = None
RequestHandler._static_hashes = {}

handler._execute(transforms, *args)
handler._execute(transforms, *args, **kwargs)
return handler

def reverse_url(self, name, *args):
Expand Down

0 comments on commit 880a9e6

Please sign in to comment.