diff options
| author | Michael Merickel <michael@merickel.org> | 2012-04-12 01:37:27 -0500 |
|---|---|---|
| committer | Michael Merickel <michael@merickel.org> | 2012-04-12 01:37:27 -0500 |
| commit | d7f34bdaefb855d811a78dfdf813b4e0d9268d8c (patch) | |
| tree | 66205de7edb84d2e969ad15523fc883e367d0ff2 | |
| parent | 67eb618bbf1eae072c9f096792abd6396bf90096 (diff) | |
| download | pyramid-d7f34bdaefb855d811a78dfdf813b4e0d9268d8c.tar.gz pyramid-d7f34bdaefb855d811a78dfdf813b4e0d9268d8c.tar.bz2 pyramid-d7f34bdaefb855d811a78dfdf813b4e0d9268d8c.zip | |
first cut at supporting request methods
| -rw-r--r-- | pyramid/config/factories.py | 119 | ||||
| -rw-r--r-- | pyramid/interfaces.py | 10 |
2 files changed, 96 insertions, 33 deletions
diff --git a/pyramid/config/factories.py b/pyramid/config/factories.py index ccbf3bbe9..6c4f00784 100644 --- a/pyramid/config/factories.py +++ b/pyramid/config/factories.py @@ -1,10 +1,12 @@ +from zope.interface import implementer + from pyramid.config.util import action_method from pyramid.interfaces import ( IDefaultRootFactory, INewRequest, IRequestFactory, - IRequestProperties, + IRequestExtensions, IRootFactory, ISessionFactory, ) @@ -93,55 +95,112 @@ class FactoriesConfiguratorMixin(object): self.action(IRequestFactory, register, introspectables=(intr,)) @action_method - def set_request_property(self, callable, name=None, reify=False): - """ Add a property to the request object. + def set_request_method(self, + callable=None, + name=None, + property=None, + reify=None): + """ Add a property or method to the request object. + + When adding a method to the request, ``callable`` may be any + function that receives the request object as the first + parameter. If ``name`` is ``None`` then it will be computed + from the name of the ``callable``. - ``callable`` can either be a callable that accepts the request - as its single positional parameter, or it can be a property - descriptor. It may also be a :term:`dotted Python name` which - refers to either a callable or a property descriptor. + When adding a property to the request, ``callable`` can either + be a callable that accepts the request as its single positional + parameter, or it can be a property descriptor. If ``name`` is + ``None``, the name of the property will be computed from the + name of the ``callable``. If the ``callable`` is a property descriptor a ``ValueError`` will be raised if ``name`` is ``None`` or ``reify`` is ``True``. - If ``name`` is None, the name of the property will be computed - from the name of the ``callable``. - See :meth:`pyramid.request.Request.set_property` for more - information on its usage. + details on ``property`` vs ``reify``. When ``reify`` is + ``True``, the value of ``property`` is assumed to also be + ``True``. + + In all cases, ``callable`` may also be a + :term:`dotted Python name` which refers to either a callable or + a property descriptor. + + If ``callable`` is ``None`` then the method is only used to + assist in conflict detection between different addons requesting + the same attribute on the request object. This is the recommended method for extending the request object and should be used in favor of providing a custom request factory via :meth:`pyramid.config.Configurator.set_request_factory`. - .. versionadded:: 1.3 + .. versionadded:: 1.4 """ - callable = self.maybe_dotted(callable) + if callable is not None: + callable = self.maybe_dotted(callable) - name, callable = InstancePropertyMixin._make_property( - callable, name=name, reify=reify) + property = property or reify + if property: + name, callable = InstancePropertyMixin._make_property( + callable, name=name, reify=reify) + elif name is None: + name = callable.__name__ def register(): - plist = self.registry.queryUtility(IRequestProperties) + exts = self.registry.queryUtility(IRequestExtensions) - if plist is None: - plist = [] - self.registry.registerUtility(plist, IRequestProperties) - self.registry.registerHandler(_set_request_properties, + if exts is None: + exts = _RequestExtensions() + self.registry.registerUtility(exts, IRequestExtensions) + self.registry.registerHandler(_set_request_extensions, (INewRequest,)) + plist = exts.descriptors if property else exts.methods plist.append((name, callable)) - intr = self.introspectable('request properties', name, - self.object_description(callable), - 'request property') - intr['callable'] = callable - intr['reify'] = reify - self.action(('request properties', name), register, - introspectables=(intr,)) + if callable is None: + self.action(('request extensions', name), None) + elif property: + intr = self.introspectable('request extensions', name, + self.object_description(callable), + 'request property') + intr['callable'] = callable + intr['property'] = True + intr['reify'] = reify + self.action(('request extensions', name), register, + introspectables=(intr,)) + else: + intr = self.introspectable('request extensions', name, + self.object_description(callable), + 'request method') + intr['callable'] = callable + intr['property'] = False + intr['reify'] = False + self.action(('request extensions', name), register, + introspectables=(intr,)) + + @action_method + def set_request_property(self, callable, name=None, reify=False): + """ Add a property to the request object. + + This method has been superceded by + :meth:`pyramid.config.Configurator.set_request_method` in + version 1.4, more details can be found there. + + .. versionadded:: 1.3 + """ + self.set_request_method( + callable, name=name, property=not reify, reify=reify) + +@implementer(IRequestExtensions) +class _RequestExtensions(object): + def __init__(self): + self.descriptors = [] + self.methods = [] -def _set_request_properties(event): +def _set_request_extensions(event): request = event.request - plist = request.registry.queryUtility(IRequestProperties) - request._set_properties(plist) + exts = request.registry.queryUtility(IRequestExtensions) + for name, method in exts.methods: + setattr(request, name, method) + request._set_properties(exts.descriptors) diff --git a/pyramid/interfaces.py b/pyramid/interfaces.py index 5d9d29afa..d7aae4f9f 100644 --- a/pyramid/interfaces.py +++ b/pyramid/interfaces.py @@ -513,9 +513,13 @@ class IRequestHandler(Interface): IRequest.combined = IRequest # for exception view lookups -class IRequestProperties(Interface): - """ Marker interface for storing a list of request properties which - will be added to the request object.""" +class IRequestExtensions(Interface): + """ Marker interface for storing request extensions (properties and + methods) which will be added to the request object.""" + descriptors = Attribute( + """A list of descriptors that will be added to each request.""") + methods = Attribute( + """A list of methods to be added to each request.""") class IRouteRequest(Interface): """ *internal only* interface used as in a utility lookup to find |
