def test_subclass_mutate_before_providedBy(self): from pyramid.interfaces import IRequest from pyramid.request import Request from pyramid.util import InstancePropertyHelper class RequestSub(Request): pass req = RequestSub({}) helper = InstancePropertyHelper() helper.apply_properties(req, {'b': 'b'}) self.assertTrue(IRequest.providedBy(req)) self.assertTrue(IRequest.implementedBy(RequestSub))
def apply_request_extensions(request, extensions=None): """Apply request extensions (methods and properties) to an instance of :class:`pyramid.interfaces.IRequest`. This method is dependent on the ``request`` containing a properly initialized registry. After invoking this method, the ``request`` should have the methods and properties that were defined using :meth:`pyramid.config.Configurator.add_request_method`. """ if extensions is None: extensions = request.registry.queryUtility(IRequestExtensions) if extensions is not None: for name, fn in iteritems_(extensions.methods): method = fn.__get__(request, request.__class__) setattr(request, name, method) InstancePropertyHelper.apply_properties(request, extensions.descriptors)
def apply_request_extensions(request, extensions=None): """Apply request extensions (methods and properties) to an instance of :class:`pyramid.interfaces.IRequest`. This method is dependent on the ``request`` containing a properly initialized registry. After invoking this method, the ``request`` should have the methods and properties that were defined using :meth:`pyramid.config.Configurator.add_request_method`. """ if extensions is None: extensions = request.registry.queryUtility(IRequestExtensions) if extensions is not None: for name, fn in iteritems_(extensions.methods): method = fn.__get__(request, request.__class__) setattr(request, name, method) InstancePropertyHelper.apply_properties( request, extensions.descriptors)
def test_it_with_extensions(self): from pyramid.util import InstancePropertyHelper exts = DummyExtensions() ext_method = lambda r: 'bar' name, fn = InstancePropertyHelper.make_property(ext_method, 'foo') exts.descriptors[name] = fn request = DummyRequest({}) registry = request.registry = self._makeRegistry([exts, DummyFactory]) info = self._callFUT(request=request, registry=registry) self.assertEqual(request.foo, 'bar') root, closer = info['root'], info['closer'] closer()
def test_subclass_with_implementer(self): from pyramid.interfaces import IRequest from pyramid.request import Request from pyramid.util import InstancePropertyHelper from zope.interface import implementer @implementer(IRequest) class RequestSub(Request): pass self.assertTrue(hasattr(Request, '__provides__')) self.assertTrue(hasattr(Request, '__implemented__')) self.assertTrue(hasattr(Request, '__providedBy__')) self.assertTrue(hasattr(RequestSub, '__provides__')) self.assertTrue(hasattr(RequestSub, '__providedBy__')) self.assertTrue(hasattr(RequestSub, '__implemented__')) req = RequestSub({}) helper = InstancePropertyHelper() helper.apply_properties(req, {'b': 'b'}) self.assertTrue(IRequest.providedBy(req)) self.assertTrue(IRequest.implementedBy(RequestSub))
def add_request_method(self, callable=None, name=None, property=False, reify=False): """ 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``. 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 ``callable`` is a property descriptor, it has to be an instance of a class which is a subclass of ``property``. 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``. See :meth:`pyramid.request.Request.set_property` for more 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.4 """ if callable is not None: callable = self.maybe_dotted(callable) property = property or reify if property: name, callable = InstancePropertyHelper.make_property(callable, name=name, reify=reify) elif name is None: name = callable.__name__ else: name = get_callable_name(name) def register(): exts = self.registry.queryUtility(IRequestExtensions) if exts is None: exts = _RequestExtensions() self.registry.registerUtility(exts, IRequestExtensions) plist = exts.descriptors if property else exts.methods plist[name] = callable 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, ))
def add_request_method(self, callable=None, name=None, property=False, reify=False): """ 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``. 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``. See :meth:`pyramid.request.Request.set_property` for more 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.4 """ if callable is not None: callable = self.maybe_dotted(callable) property = property or reify if property: name, callable = InstancePropertyHelper.make_property( callable, name=name, reify=reify) elif name is None: name = callable.__name__ else: name = get_callable_name(name) def register(): exts = self.registry.queryUtility(IRequestExtensions) if exts is None: exts = _RequestExtensions() self.registry.registerUtility(exts, IRequestExtensions) plist = exts.descriptors if property else exts.methods plist[name] = callable 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,))