Skip to content

Commit

Permalink
Update to utilize web.dispatch Crumb named tuple.
Browse files Browse the repository at this point in the history
  • Loading branch information
amcgregor committed Dec 22, 2022
1 parent 5ece03c commit 751d656
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions web/dispatch/resource/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from inspect import isclass, ismethod

from .exc import InvalidMethod
from ..core import Crumb


log = __import__('logging').getLogger(__name__)
Expand All @@ -24,6 +25,7 @@ def __repr__(self):

def __call__(self, context, obj, path):
verb = getattr(context, 'environ', context)['REQUEST_METHOD'].lower()
origin = obj

if __debug__:
if not isinstance(path, deque): # pragma: no cover
Expand All @@ -38,7 +40,7 @@ def __call__(self, context, obj, path):
else:
path = deque(path)

log.debug("Preparing resource dispatch. " + repr(obj), extra=dict(
log.debug("Preparing resource dispatch.", extra=dict(
dispatcher = repr(self),
context = repr(context),
obj = repr(obj),
Expand All @@ -47,8 +49,12 @@ def __call__(self, context, obj, path):
))

if isclass(obj):
obj = obj(context, None, None)
yield None, obj, False # Announce class instantiation.
obj = obj() if context is None else obj(context)

if __debug__:
log.debug("Instantiated class during descent.", extra=dict(LE, obj=obj))

yield Crumb(self, origin, handler=obj) # Announce class instantiation.

context.resource = obj
consumed = None
Expand All @@ -69,10 +75,10 @@ def __call__(self, context, obj, path):
attr = partial(getattr(self, consumed), obj)

if isclass(attr):
yield consumed, attr(context, obj, None), False
yield Crumb(self, origin, path=consumed, handler=obj)
return

yield consumed, attr, True
yield Crumb(self, origin, path=consumed, endpoint=True, handler=obj)
return

if path and Resource:
Expand All @@ -83,18 +89,18 @@ def __call__(self, context, obj, path):
except KeyError:
pass
else:
yield path.popleft(), obj, False
yield Crumb(self, origin, path=path.popleft(), handler=obj)

return

if verb and verb in safe:
obj = getattr(obj, verb, None)
if not obj and verb in {'head', 'options'}:
obj = partial(getattr(self, verb), obj)
yield None, obj, True
yield Crumb(self, origin, endpoint=True, handler=obj)
return

yield None, invalid_method, True
yield Crumb(self, origin, endpoint=True, handler=invalid_method)

def head(self, obj, *args, **kw):
"""Allow the get method to set headers, but return no content.
Expand Down

0 comments on commit 751d656

Please sign in to comment.