Permalink
Browse files

move add_traverser and add_resource_url_adapter to adapters

  • Loading branch information...
1 parent 20ecd35 commit e0551cd9e73a20f2180fecb1134e07d5ebb90c68 @mcdonc mcdonc committed Feb 18, 2012
View
@@ -540,10 +540,30 @@ introspectables in categories not described here.
The (resolved) interface or class object that represents the return value
of a root factory that this traverser will be used for.
- ``factory``
+ ``adapter``
The (resolved) traverser class.
+``resource url adapters``
+
+ Each introspectable in the ``resource url adapters`` category represents a
+ call to :meth:`pyramid.config.Configurator.add_resource_url_adapter`; each
+ will have the following data.
+
+ ``adapter``
+
+ The (resolved) resource URL adapter class.
+
+ ``resource_iface``
+
+ The (resolved) interface or class object that represents the resource
+ interface that this url adapter is registered for.
+
+ ``request_iface``
+
+ The (resolved) interface or class object that represents the request
+ interface that this url adapter is registered for.
+
Introspection in the Toolbar
----------------------------
View
@@ -1,6 +1,10 @@
from zope.interface import Interface
-from pyramid.interfaces import IResponse
+from pyramid.interfaces import (
+ IResponse,
+ ITraverser,
+ IResourceURL,
+ )
from pyramid.config.util import action_method
@@ -72,3 +76,144 @@ def _register_response_adapters(self):
# cope with WebOb response objects that aren't decorated with IResponse
from webob import Response as WebobResponse
self.registry.registerSelfAdapter((WebobResponse,), IResponse)
+
+ @action_method
+ def add_traverser(self, adapter, iface=None):
+ """
+ The superdefault :term:`traversal` algorithm that :app:`Pyramid` uses
+ is explained in :ref:`traversal_algorithm`. Though it is rarely
+ necessary, this default algorithm can be swapped out selectively for
+ a different traversal pattern via configuration. The section
+ entitled :ref:`changing_the_traverser` details how to create a
+ traverser class.
+
+ For example, to override the superdefault traverser used by Pyramid,
+ you might do something like this:
+
+ .. code-block:: python
+
+ from myapp.traversal import MyCustomTraverser
+ config.add_traverser(MyCustomTraverser)
+
+ This would cause the Pyramid superdefault traverser to never be used;
+ intead all traversal would be done using your ``MyCustomTraverser``
+ class, no matter which object was returned by the :term:`root
+ factory` of this application. Note that we passed no arguments to
+ the ``iface`` keyword parameter. The default value of ``iface``,
+ ``None`` represents that the registered traverser should be used when
+ no other more specific traverser is available for the object returned
+ by the root factory.
+
+ However, more than one traversal algorithm can be active at the same
+ time. The traverser used can depend on the result of the :term:`root
+ factory`. For instance, if your root factory returns more than one
+ type of object conditionally, you could claim that an alternate
+ traverser adapter should be used agsinst one particular class or
+ interface returned by that root factory. When the root factory
+ returned an object that implemented that class or interface, a custom
+ traverser would be used. Otherwise, the default traverser would be
+ used. The ``iface`` argument represents the class of the object that
+ the root factory might return or an :term:`interface` that the object
+ might implement.
+
+ To use a particular traverser only when the root factory returns a
+ particular class:
+
+ .. code-block:: python
+
+ config.add_traverser(MyCustomTraverser, MyRootClass)
+
+ When more than one traverser is active, the "most specific" traverser
+ will be used (the one that matches the class or interface of the
+ value returned by the root factory most closely).
+
+ Note that either ``adapter`` or ``iface`` can be a :term:`dotted
+ Python name` or a Python object.
+
+ See :ref:`changing_the_traverser` for more information.
+ """
+ iface = self.maybe_dotted(iface)
+ adapter= self.maybe_dotted(adapter)
+ def register(iface=iface):
+ if iface is None:
+ iface = Interface
+ self.registry.registerAdapter(adapter, (iface,), ITraverser)
+ discriminator = ('traverser', iface)
+ intr = self.introspectable(
+ 'traversers',
+ discriminator,
+ 'traverser for %r' % iface,
+ 'traverser',
+ )
+ intr['adapter'] = adapter
+ intr['iface'] = iface
+ self.action(discriminator, register, introspectables=(intr,))
+
+ @action_method
+ def add_resource_url_adapter(self, adapter, resource_iface=None,
+ request_iface=None):
+ """
+ When you add a traverser as described in
+ :ref:`changing_the_traverser`, it's convenient to continue to use the
+ :meth:`pyramid.request.Request.resource_url` API. However, since the
+ way traversal is done may have been modified, the URLs that
+ ``resource_url`` generates by default may be incorrect when resources
+ are returned by a custom traverser.
+
+ If you've added a traverser, you can change how
+ :meth:`~pyramid.request.Request.resource_url` generates a URL for a
+ specific type of resource by calling this method.
+
+ The ``adapter`` argument represents a class that implements the
+ :class:`~pyramid.interfaces.IResourceURL` interface. The class
+ constructor should accept two arguments in its constructor (the
+ resource and the request) and the resulting instance should provide
+ the attributes detailed in that interface (``virtual_path`` and
+ ``physical_path``, in particular).
+
+ The ``resource_iface`` argument represents a class or interface that
+ the resource should possess for this url adapter to be used when
+ :meth:`pyramid.request.Request.resource_url` looks up a resource url
+ adapter. If ``resource_iface`` is not passed, or it is passed as
+ ``None``, the adapter will be used for every type of resource.
+
+ The ``request_iface`` argument represents a class or interface that
+ the request should possess for this url adapter to be used when
+ :meth:`pyramid.request.Request.resource_url` looks up a resource url
+ adapter. If ``request_iface`` is not epassed, or it is passed as
+ ``None``, the adapter will be used for every type of request.
+
+ See :ref:`changing_resource_url` for more information.
+
+ .. note::
+
+ This API is new in Pyramid 1.3.
+ """
+ adapter = self.maybe_dotted(adapter)
+ resource_iface = self.maybe_dotted(resource_iface)
+ request_iface = self.maybe_dotted(request_iface)
+ def register(resource_iface=resource_iface,
+ request_iface=request_iface):
+ if resource_iface is None:
+ resource_iface = Interface
+ if request_iface is None:
+ request_iface = Interface
+ self.registry.registerAdapter(
+ adapter,
+ (resource_iface, request_iface),
+ IResourceURL,
+ )
+ discriminator = ('resource url adapter', resource_iface, request_iface)
+ intr = self.introspectable(
+ 'resource url adapters',
+ discriminator,
+ 'resource url adapter for resource iface %r, request_iface %r' % (
+ resource_iface, request_iface),
+ 'resource url adapter',
+ )
+ intr['adapter'] = adapter
+ intr['resource_iface'] = resource_iface
+ intr['request_iface'] = request_iface
+ self.action(discriminator, register, introspectables=(intr,))
+
+
View
@@ -1,5 +1,3 @@
-from zope.interface import Interface
-
from pyramid.config.util import action_method
from pyramid.interfaces import (
@@ -9,8 +7,6 @@
IRequestProperties,
IRootFactory,
ISessionFactory,
- ITraverser,
- IResourceURL,
)
from pyramid.traversal import DefaultRootFactory
@@ -144,145 +140,6 @@ def register():
self.action(('request properties', name), register,
introspectables=(intr,))
- @action_method
- def add_traverser(self, factory, iface=None):
- """
- The superdefault :term:`traversal` algorithm that :app:`Pyramid` uses
- is explained in :ref:`traversal_algorithm`. Though it is rarely
- necessary, this default algorithm can be swapped out selectively for
- a different traversal pattern via configuration. The section
- entitled :ref:`changing_the_traverser` details how to create a
- traverser class.
-
- For example, to override the superdefault traverser used by Pyramid,
- you might do something like this:
-
- .. code-block:: python
-
- from myapp.traversal import MyCustomTraverser
- config.add_traverser(MyCustomTraverser)
-
- This would cause the Pyramid superdefault traverser to never be used;
- intead all traversal would be done using your ``MyCustomTraverser``
- class, no matter which object was returned by the :term:`root
- factory` of this application. Note that we passed no arguments to
- the ``iface`` keyword parameter. The default value of ``iface``,
- ``None`` represents that the registered traverser should be used when
- no other more specific traverser is available for the object returned
- by the root factory.
-
- However, more than one traversal algorithm can be active at the same
- time. The traverser used can depend on the result of the :term:`root
- factory`. For instance, if your root factory returns more than one
- type of object conditionally, you could claim that an alternate
- traverser adapter should be used agsinst one particular class or
- interface returned by that root factory. When the root factory
- returned an object that implemented that class or interface, a custom
- traverser would be used. Otherwise, the default traverser would be
- used. The ``iface`` argument represents the class of the object that
- the root factory might return or an :term:`interface` that the object
- might implement.
-
- To use a particular traverser only when the root factory returns a
- particular class:
-
- .. code-block:: python
-
- config.add_traverser(MyCustomTraverser, MyRootClass)
-
- When more than one traverser is active, the "most specific" traverser
- will be used (the one that matches the class or interface of the
- value returned by the root factory most closely).
-
- Note that either ``factory`` or ``iface`` can be a :term:`dotted
- Python name` or a Python object.
-
- See :ref:`changing_the_traverser` for more information.
- """
- iface = self.maybe_dotted(iface)
- factory = self.maybe_dotted(factory)
- def register(iface=iface):
- if iface is None:
- iface = Interface
- self.registry.registerAdapter(factory, (iface,), ITraverser)
- discriminator = ('traverser', iface)
- intr = self.introspectable(
- 'traversers',
- discriminator,
- 'traverser for %r' % iface,
- 'traverser',
- )
- intr['factory'] = factory
- intr['iface'] = iface
- self.action(discriminator, register, introspectables=(intr,))
-
- @action_method
- def add_resource_url_adapter(self, factory, resource_iface=None,
- request_iface=None):
- """
- When you add a traverser as described in
- :ref:`changing_the_traverser`, it's convenient to continue to use the
- :meth:`pyramid.request.Request.resource_url` API. However, since the
- way traversal is done may have been modified, the URLs that
- ``resource_url`` generates by default may be incorrect when resources
- are returned by a custom traverser.
-
- If you've added a traverser, you can change how
- :meth:`~pyramid.request.Request.resource_url` generates a URL for a
- specific type of resource by calling this method.
-
- The ``factory`` argument represents a class that implements the
- :class:`~pyramid.interfaces.IResourceURL` interface. The class
- constructor should accept two arguments in its constructor (the
- resource and the request) and the resulting instance should provide
- the attributes detailed in that interface (``virtual_path`` and
- ``physical_path``, in particular).
-
- The ``resource_iface`` argument represents a class or interface that
- the resource should possess for this url adapter to be used when
- :meth:`pyramid.request.Request.resource_url` looks up a resource url
- adapter. If ``resource_iface`` is not passed, or it is passed as
- ``None``, the adapter will be used for every type of resource.
-
- The ``request_iface`` argument represents a class or interface that
- the request should possess for this url adapter to be used when
- :meth:`pyramid.request.Request.resource_url` looks up a resource url
- adapter. If ``request_iface`` is not epassed, or it is passed as
- ``None``, the adapter will be used for every type of request.
-
- See :ref:`changing_resource_url` for more information.
-
- .. note::
-
- This API is new in Pyramid 1.3.
- """
- factory = self.maybe_dotted(factory)
- resource_iface = self.maybe_dotted(resource_iface)
- request_iface = self.maybe_dotted(request_iface)
- def register(resource_iface=resource_iface,
- request_iface=request_iface):
- if resource_iface is None:
- resource_iface = Interface
- if request_iface is None:
- request_iface = Interface
- self.registry.registerAdapter(
- factory,
- (resource_iface, request_iface),
- IResourceURL,
- )
- discriminator = ('resource url adapter', resource_iface, request_iface)
- intr = self.introspectable(
- 'resource url adapters',
- discriminator,
- 'resource url adapter for resource iface %r, request_iface %r' % (
- resource_iface, request_iface),
- 'resource url adapter',
- )
- intr['factory'] = factory
- intr['resource_iface'] = resource_iface
- intr['request_iface'] = request_iface
- self.action(discriminator, register, introspectables=(intr,))
-
def _set_request_properties(event):
request = event.request
plist = request.registry.queryUtility(IRequestProperties)
Oops, something went wrong.

0 comments on commit e0551cd

Please sign in to comment.