From 11644e705834ff65cb8963333855a1db6272ae1e Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Fri, 30 Oct 2009 18:56:58 +0000 Subject: Features -------- - In previous versions of BFG, the "root factory" (the ``get_root`` callable passed to ``make_app`` or a function pointed to by the ``factory`` attribute of a route) was called with a "bare" WSGI environment. In this version, and going forward, it will be called with a ``request`` object. The request object passed to the factory implements dictionary-like methods in such a way that existing root factory code which expects to be passed an environ will continue to work. Internal -------- - The request implements dictionary-like methods that mutate and query the WSGI environ. This is only for the purpose of backwards compatibility with root factories which expect an ``environ`` rather than a request. - The ``repoze.bfg.request.create_route_request_factory`` function, which returned a request factory was removed in favor of a ``repoze.bfg.request.route_request_interface`` function, which returns an interface. - The ``repoze.bfg.request.Request`` class, which is a subclass of ``webob.Request`` now defines its own ``__setattr__``, ``__getattr__`` and ``__delattr__`` methods, which override the default WebOb behavior. The default WebOb behavior stores attributes of the request in ``self.environ['webob.adhoc_attrs']``, and retrieves them from that dictionary during a ``__getattr__``. This behavior was undesirable for speed and "expectation" reasons. Now attributes of the ``request`` are stored in ``request.__dict__`` (as you otherwise might expect from an object that did not override these methods). - Reverse the order in which the router calls the request factory and the root factory. The request factory is now called first; the resulting request is passed to the root factory. - The ``repoze.bfg.request.request_factory`` function has been removed. Its functionality is no longer required. - The "routes root factory" that wraps the default root factory when there are routes mentioned in the configuration now attaches an interface to the request via ``zope.interface.directlyProvides``. This replaces logic in the (now-gone) ``repoze.bfg.request.request_factory`` function. - The ``route`` and ``view`` ZCML directives now register an interface as a named utility (retrieved from ``repoze.bfg.request.route_request_interface``) rather than a request factory (the previous return value of the now-missing ``repoze.bfg.request.create_route_request_factory``. --- repoze/bfg/request.py | 80 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 23 deletions(-) (limited to 'repoze/bfg/request.py') diff --git a/repoze/bfg/request.py b/repoze/bfg/request.py index e81710ae1..751d1fba1 100644 --- a/repoze/bfg/request.py +++ b/repoze/bfg/request.py @@ -24,29 +24,63 @@ class Request(WebobRequest): __setattr__ = object.__setattr__ __delattr__ = object.__delattr__ -def request_factory(environ): - if 'bfg.routes.route' in environ: - route = environ['bfg.routes.route'] - factory = queryUtility(IRouteRequest, name=route.name) - if factory is not None: - request = factory(environ) - request.matchdict = environ['bfg.routes.matchdict'] - return request - return Request(environ) - -def create_route_request_factory(name): - iface = InterfaceClass('%s_IRequest' % name, (IRouteRequest,)) - - class RouteRequest(WebobRequest): - implements(iface) - charset = 'utf-8' - - # override default WebOb "environ['adhoc_attr']" mutation behavior - __getattr__ = object.__getattribute__ - __setattr__ = object.__setattr__ - __delattr__ = object.__delattr__ - - return RouteRequest + # b/c dict interface for "root factory" code that expects a bare + # environ. Explicitly omitted dict methods: clear (unnecessary), + # copy (implemented by WebOb), fromkeys (unnecessary) + + def __contains__(self, k): + return self.environ.__contains__(k) + + def __delitem__(self, k): + return self.environ.__delitem__(k) + + def __getitem__(self, k): + return self.environ.__getitem__(k) + + def __iter__(self): + return iter(self.environ) + + def __setitem__(self, k, v): + self.environ[k] = v + + def get(self, k, default=None): + return self.environ.get(k, default) + + def has_key(self, k): + return self.environ.has_key(k) + + def items(self): + return self.environ.items() + + def iteritems(self): + return self.environ.iteritems() + + def iterkeys(self): + return self.environ.iterkeys() + + def itervalues(self): + return self.environ.itervalues() + + def keys(self): + return self.environ.keys() + + def pop(self, k): + return self.environ.pop(k) + + def popitem(self): + return self.environ.popitem() + + def setdefault(self, v, default): + return self.environ.setdefault(v, default) + + def update(self, v, **kw): + return self.environ.update(v, **kw) + + def values(self): + return self.environ.values() + +def route_request_iface(name): + return InterfaceClass('%s_IRequest' % name, (IRouteRequest,)) def add_global_response_headers(request, headerlist): attrs = request.__dict__ -- cgit v1.2.3