Permalink
Browse files

- Added the ``pyramid.renderers.null_renderer`` object as an API. The…

… null

  renderer is an object that can be used in advanced integration cases as
  input to the view configuration ``renderer=`` argument.  When the null
  renderer is used as a view renderer argument, Pyramid avoids converting the
  view callable result into a Response object.  This is useful if you want to
  reuse the view configuration and lookup machinery outside the context of
  its use by the Pyramid router.  This feature was added for consumption by
  the ``pyramid_rpc`` package, which uses view configuration and lookup
  outside the context of a router in exactly this way.  ``pyramid_rpc`` has
  been broken under 1.1 since 1.1b1; adding it allows us to make it work
  again.
  • Loading branch information...
mcdonc committed Jul 22, 2011
1 parent 73c0aee commit aa2fe1b0a02ba4edde4d285ec0a5a6ec545b7fec
View
@@ -1,6 +1,21 @@
Next release
============
+Features
+--------
+
+- Added the ``pyramid.renderers.null_renderer`` object as an API. The null
+ renderer is an object that can be used in advanced integration cases as
+ input to the view configuration ``renderer=`` argument. When the null
+ renderer is used as a view renderer argument, Pyramid avoids converting the
+ view callable result into a Response object. This is useful if you want to
+ reuse the view configuration and lookup machinery outside the context of
+ its use by the Pyramid router. This feature was added for consumption by
+ the ``pyramid_rpc`` package, which uses view configuration and lookup
+ outside the context of a router in exactly this way. ``pyramid_rpc`` has
+ been broken under 1.1 since 1.1b1; adding it allows us to make it work
+ again.
+
Internals
---------
View
@@ -13,3 +13,12 @@
.. autoclass:: JSONP
+.. attribute:: null_renderer
+
+ An object that can be used in advanced integration cases as input to the
+ view configuration ``renderer=`` argument. When the null renderer is used
+ as a view renderer argument, Pyramid avoids converting the view callable
+ result into a Response object. This is useful if you want to reuse the
+ view configuration and lookup machinery outside the context of its use by
+ the Pyramid router (e.g. the package named ``pyramid_rpc`` does this).
+
View
@@ -2124,7 +2124,7 @@ def bwcompat_view(context, request):
context = getattr(request, 'context', None)
return view(context, request)
return self.add_view(bwcompat_view, context=HTTPForbidden,
- wrapper=wrapper)
+ wrapper=wrapper, renderer=renderer)
@action_method
def set_notfound_view(self, view=None, attr=None, renderer=None,
@@ -2165,7 +2165,7 @@ def bwcompat_view(context, request):
context = getattr(request, 'context', None)
return view(context, request)
return self.add_view(bwcompat_view, context=HTTPNotFound,
- wrapper=wrapper)
+ wrapper=wrapper, renderer=renderer)
@action_method
def set_request_factory(self, factory):
@@ -3104,14 +3104,17 @@ def attr_view(context, request):
@wraps_view
def rendered_view(self, view):
- # one way or another this wrapper must produce a Response
+ # one way or another this wrapper must produce a Response (unless
+ # the renderer is a NullRendererHelper)
renderer = self.kw.get('renderer')
if renderer is None:
# register a default renderer if you want super-dynamic
# rendering. registering a default renderer will also allow
# override_renderer to work if a renderer is left unspecified for
# a view registration.
return self._response_resolved_view(view)
+ if renderer is renderers.null_renderer:
+ return view
return self._rendered_view(view, renderer)
def _rendered_view(self, view, view_renderer):
@@ -3142,10 +3145,6 @@ def rendered_view(context, request):
def _response_resolved_view(self, view):
registry = self.registry
- if hasattr(registry, '_dont_resolve_responses'):
- # for Pyramid unit tests only
- return view
-
def viewresult_to_response(context, request):
result = view(context, request)
response = registry.queryAdapterOrSelf(result, IResponse)
View
@@ -866,4 +866,3 @@ class IRendererInfo(Interface):
settings = Attribute('The deployment settings dictionary related '
'to the current application')
-
View
@@ -469,3 +469,22 @@ def clone(self, name=None, package=None, registry=None):
registry = self.registry
return self.__class__(name=name, package=package, registry=registry)
+class NullRendererHelper(RendererHelper):
+ """ Special renderer helper that has render_* methods which simply return
+ the value they are fed rather than converting them to response objects;
+ useful for testing purposes and special case view configuration
+ registrations that want to use the view configuration machinery but do
+ not want actual rendering to happen ."""
+ def render_view(self, request, value, view, context):
+ return value
+
+ def render(self, value, system_values, request=None):
+ return value
+
+ def render_to_response(self, value, system_values, request=None):
+ return value
+
+ def clone(self, name=None, package=None, registry=None):
+ return self
+
+null_renderer = NullRendererHelper()
@@ -1,15 +1,16 @@
from pyramid.view import view_config
+from pyramid.renderers import null_renderer
-@view_config()
+@view_config(renderer=null_renderer)
def grokked(context, request):
return 'grokked'
-@view_config(request_method='POST')
+@view_config(request_method='POST', renderer=null_renderer)
def grokked_post(context, request):
return 'grokked_post'
-@view_config(name='stacked2')
-@view_config(name='stacked1')
+@view_config(name='stacked2', renderer=null_renderer)
+@view_config(name='stacked1', renderer=null_renderer)
def stacked(context, request):
return 'stacked'
@@ -21,8 +22,10 @@ def __init__(self, context, request):
def __call__(self):
return 'stacked_class'
-stacked_class = view_config(name='stacked_class1')(stacked_class)
-stacked_class = view_config(name='stacked_class2')(stacked_class)
+stacked_class = view_config(name='stacked_class1',
+ renderer=null_renderer)(stacked_class)
+stacked_class = view_config(name='stacked_class2',
+ renderer=null_renderer)(stacked_class)
class oldstyle_grokked_class:
def __init__(self, context, request):
@@ -32,7 +35,8 @@ def __init__(self, context, request):
def __call__(self):
return 'oldstyle_grokked_class'
-oldstyle_grokked_class = view_config(name='oldstyle_grokked_class')(
+oldstyle_grokked_class = view_config(name='oldstyle_grokked_class',
+ renderer=null_renderer)(
oldstyle_grokked_class)
class grokked_class(object):
@@ -43,17 +47,19 @@ def __init__(self, context, request):
def __call__(self):
return 'grokked_class'
-grokked_class = view_config(name='grokked_class')(grokked_class)
+grokked_class = view_config(name='grokked_class',
+ renderer=null_renderer)(grokked_class)
class Foo(object):
def __call__(self, context, request):
return 'grokked_instance'
grokked_instance = Foo()
-grokked_instance = view_config(name='grokked_instance')(grokked_instance)
+grokked_instance = view_config(name='grokked_instance',
+ renderer=null_renderer)(grokked_instance)
class Base(object):
- @view_config(name='basemethod')
+ @view_config(name='basemethod', renderer=null_renderer)
def basemethod(self):
""" """
@@ -62,16 +68,16 @@ def __init__(self, context, request):
self.context = context
self.request = request
- @view_config(name='method1')
+ @view_config(name='method1', renderer=null_renderer)
def method1(self):
return 'method1'
- @view_config(name='method2')
+ @view_config(name='method2', renderer=null_renderer)
def method2(self):
return 'method2'
- @view_config(name='stacked_method2')
- @view_config(name='stacked_method1')
+ @view_config(name='stacked_method2', renderer=null_renderer)
+ @view_config(name='stacked_method1', renderer=null_renderer)
def stacked(self):
return 'stacked_method'
@@ -1,15 +1,16 @@
from pyramid.view import view_config
+from pyramid.renderers import null_renderer
-@view_config(name='another')
+@view_config(name='another', renderer=null_renderer)
def grokked(context, request):
return 'another_grokked'
-@view_config(request_method='POST', name='another')
+@view_config(request_method='POST', name='another', renderer=null_renderer)
def grokked_post(context, request):
return 'another_grokked_post'
-@view_config(name='another_stacked2')
-@view_config(name='another_stacked1')
+@view_config(name='another_stacked2', renderer=null_renderer)
+@view_config(name='another_stacked1', renderer=null_renderer)
def stacked(context, request):
return 'another_stacked'
@@ -21,8 +22,10 @@ def __init__(self, context, request):
def __call__(self):
return 'another_stacked_class'
-stacked_class = view_config(name='another_stacked_class1')(stacked_class)
-stacked_class = view_config(name='another_stacked_class2')(stacked_class)
+stacked_class = view_config(name='another_stacked_class1',
+ renderer=null_renderer)(stacked_class)
+stacked_class = view_config(name='another_stacked_class2',
+ renderer=null_renderer)(stacked_class)
class oldstyle_grokked_class:
def __init__(self, context, request):
@@ -32,7 +35,8 @@ def __init__(self, context, request):
def __call__(self):
return 'another_oldstyle_grokked_class'
-oldstyle_grokked_class = view_config(name='another_oldstyle_grokked_class')(
+oldstyle_grokked_class = view_config(name='another_oldstyle_grokked_class',
+ renderer=null_renderer)(
oldstyle_grokked_class)
class grokked_class(object):
@@ -43,14 +47,16 @@ def __init__(self, context, request):
def __call__(self):
return 'another_grokked_class'
-grokked_class = view_config(name='another_grokked_class')(grokked_class)
+grokked_class = view_config(name='another_grokked_class',
+ renderer=null_renderer)(grokked_class)
class Foo(object):
def __call__(self, context, request):
return 'another_grokked_instance'
grokked_instance = Foo()
-grokked_instance = view_config(name='another_grokked_instance')(
+grokked_instance = view_config(name='another_grokked_instance',
+ renderer=null_renderer)(
grokked_instance)
# ungrokkable
@@ -1,5 +1,6 @@
from pyramid.view import view_config
+from pyramid.renderers import null_renderer
-@view_config(name='pod_notinit')
+@view_config(name='pod_notinit', renderer=null_renderer)
def subpackage_notinit(context, request):
return 'pod_notinit'
@@ -1,5 +1,6 @@
from pyramid.view import view_config
+from pyramid.renderers import null_renderer
-@view_config(name='subpackage_init')
+@view_config(name='subpackage_init', renderer=null_renderer)
def subpackage_init(context, request):
return 'subpackage_init'
@@ -1,5 +1,6 @@
from pyramid.view import view_config
+from pyramid.renderers import null_renderer
-@view_config(name='subpackage_notinit')
+@view_config(name='subpackage_notinit', renderer=null_renderer)
def subpackage_notinit(context, request):
return 'subpackage_notinit'
@@ -1,5 +1,6 @@
from pyramid.view import view_config
+from pyramid.renderers import null_renderer
-@view_config(name='subsubpackage_init')
+@view_config(name='subsubpackage_init', renderer=null_renderer)
def subpackage_init(context, request):
return 'subsubpackage_init'
Oops, something went wrong.

0 comments on commit aa2fe1b

Please sign in to comment.