summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2010-12-30 03:21:56 -0500
committerChris McDonough <chrism@plope.com>2010-12-30 03:21:56 -0500
commitae6513b9e93d876902936c257aef6a506f850aa3 (patch)
tree3695d629a17f8f5d7271e2ff09d388b194e8e855
parentfcff8cda8f7c60f181e902ca5a349eb8b5655205 (diff)
downloadpyramid-ae6513b9e93d876902936c257aef6a506f850aa3.tar.gz
pyramid-ae6513b9e93d876902936c257aef6a506f850aa3.tar.bz2
pyramid-ae6513b9e93d876902936c257aef6a506f850aa3.zip
factor defaultviewmapper to dispatch in a slightly more readable way
-rw-r--r--pyramid/config.py132
1 files changed, 73 insertions, 59 deletions
diff --git a/pyramid/config.py b/pyramid/config.py
index a53c21b81..eefdaae1f 100644
--- a/pyramid/config.py
+++ b/pyramid/config.py
@@ -2872,68 +2872,39 @@ class DefaultViewMapper(object):
self.attr = kw.get('attr')
self.decorator = kw.get('decorator')
- def requestonly(self, view):
- attr = self.attr
- if attr is None:
- attr = '__call__'
- if inspect.isfunction(view):
- fn = view
- elif inspect.isclass(view):
- try:
- fn = view.__init__
- except AttributeError:
- return False
- else:
- try:
- fn = getattr(view, attr)
- except AttributeError:
- return False
-
- try:
- argspec = inspect.getargspec(fn)
- except TypeError:
- return False
-
- args = argspec[0]
- defaults = argspec[3]
-
- if hasattr(fn, 'im_func'):
- # it's an instance method
- if not args:
- return False
- args = args[1:]
- if not args:
- return False
-
- if len(args) == 1:
- return True
-
- elif args[0] == 'request':
- if len(args) - len(defaults) == 1:
- return True
-
- return False
-
def __call__(self, view):
- attr = self.attr
decorator = self.decorator
- isclass = inspect.isclass(view)
- ronly = self.requestonly(view)
- if isclass and ronly:
- view = preserve_view_attrs(view, self.map_requestonly_class(view))
- elif isclass:
+ if inspect.isclass(view):
view = preserve_view_attrs(view, self.map_class(view))
- elif ronly:
- view = preserve_view_attrs(view, self.map_requestonly_func(view))
- elif attr:
- view = preserve_view_attrs(view, self.map_attr(view))
- elif self.renderer is not None:
- view = preserve_view_attrs(view, self.map_rendered(view))
+ else:
+ view = preserve_view_attrs(view, self.map_nonclass(view))
if decorator is not None:
view = preserve_view_attrs(view, decorator(view))
return view
- def map_requestonly_class(self, view):
+ def map_class(self, view):
+ ronly = self.requestonly(view)
+ if ronly:
+ mapped_view = self._map_class_requestonly(view)
+ else:
+ mapped_view = self._map_class_native(view)
+ return mapped_view
+
+ def map_nonclass(self, view):
+ # We do more work here than appears necessary to avoid wrapping the
+ # view unless it actually requires wrapping (to avoid function call
+ # overhead).
+ mapped_view = view
+ ronly = self.requestonly(view)
+ if ronly:
+ mapped_view = self._map_nonclass_requestonly(view)
+ elif self.attr:
+ mapped_view = self._map_nonclass_attr(view)
+ elif self.renderer is not None:
+ mapped_view = self._map_nonclass_rendered(view)
+ return mapped_view
+
+ def _map_class_requestonly(self, view):
# its a class that has an __init__ which only accepts request
attr = self.attr
def _class_requestonly_view(context, request):
@@ -2948,7 +2919,7 @@ class DefaultViewMapper(object):
return response
return _class_requestonly_view
- def map_class(self, view):
+ def _map_class_native(self, view):
# its a class that has an __init__ which accepts both context and
# request
attr = self.attr
@@ -2964,7 +2935,7 @@ class DefaultViewMapper(object):
return response
return _class_view
- def map_requestonly_func(self, view):
+ def _map_nonclass_requestonly(self, view):
# its a function that has a __call__ which accepts only a single
# request argument
attr = self.attr
@@ -2979,7 +2950,7 @@ class DefaultViewMapper(object):
return response
return _requestonly_view
- def map_attr(self, view):
+ def _map_nonclass_attr(self, view):
# its a function that has a __call__ which accepts both context and
# request, but still has an attr
attr = self.attr
@@ -2991,7 +2962,7 @@ class DefaultViewMapper(object):
return response
return _attr_view
- def map_rendered(self, view):
+ def _map_nonclass_rendered(self, view):
# it's a function that has a __call__ that accepts both context and
# request, but requires rendering
def _rendered_view(context, request):
@@ -3002,6 +2973,49 @@ class DefaultViewMapper(object):
return response
return _rendered_view
+ def requestonly(self, view):
+ attr = self.attr
+ if attr is None:
+ attr = '__call__'
+ if inspect.isfunction(view):
+ fn = view
+ elif inspect.isclass(view):
+ try:
+ fn = view.__init__
+ except AttributeError:
+ return False
+ else:
+ try:
+ fn = getattr(view, attr)
+ except AttributeError:
+ return False
+
+ try:
+ argspec = inspect.getargspec(fn)
+ except TypeError:
+ return False
+
+ args = argspec[0]
+ defaults = argspec[3]
+
+ if hasattr(fn, 'im_func'):
+ # it's an instance method
+ if not args:
+ return False
+ args = args[1:]
+ if not args:
+ return False
+
+ if len(args) == 1:
+ return True
+
+ elif args[0] == 'request':
+ if len(args) - len(defaults) == 1:
+ return True
+
+ return False
+
+
def isexception(o):
if IInterface.providedBy(o):
if IException.isEqualOrExtendedBy(o):