def _cbFinishRender(self, result): def filterit(response, f): if (hasattr(f, 'handleErrors') or (response.code >= 200 and response.code < 300)): return f(self, response) else: return response response = iweb.IResponse(result, None) if response: d = defer.Deferred() for f in self.responseFilters: d.addCallback(filterit, f) d.addCallback(self.writeResponse) d.callback(response) return d resource = iweb.IResource(result, None) if resource: self.resources.append(resource) d = defer.maybeDeferred(resource.renderHTTP, self) d.addCallback(self._cbFinishRender) return d raise TypeError("html is not a resource or a response")
def test_registered(self): """ Test that if an adapter exists which can provide IResource for an object which does not provide it, that adapter is used. """ notResource = NotResource() self.failUnless(isinstance(iweb.IResource(notResource), ResourceAdapter))
def test_redundant(self): """ Test that the adaption to IResource of an object which provides IResource returns the same object. """ @implementer(iweb.IResource) class Resource(object): "" resource = Resource() self.assertIdentical(iweb.IResource(resource), resource)
def test_unadaptable(self): """ Test that attempting to adapt to IResource an object not adaptable to IResource raises an exception or returns the specified alternate object. """ class Unadaptable(object): pass self.assertRaises(TypeError, iweb.IResource, Unadaptable()) alternate = object() self.assertIdentical(iweb.IResource(Unadaptable(), alternate), alternate)
def _handleSegment(self, result, res, path, updatepaths): """Handle the result of a locateChild call done in _getChild.""" newres, newpath = result # If the child resource is None then display a error page if newres is None: raise http.HTTPError(responsecode.NOT_FOUND) # If we got a deferred then we need to call back later, once the # child is actually available. if isinstance(newres, defer.Deferred): return newres.addCallback(lambda actualRes: self._handleSegment( (actualRes, newpath), res, path, updatepaths)) if path: url = quote("/" + "/".join(path)) else: url = "/" if newpath is StopTraversal: # We need to rethink how to do this. # if newres is res: return res # else: # raise ValueError("locateChild must not return StopTraversal with a resource other than self.") newres = iweb.IResource(newres) if newres is res: assert newpath is not path, "URL traversal cycle detected when attempting to locateChild %r from resource %r." % ( path, res) assert len(newpath) < len(path), "Infinite loop impending..." if updatepaths: # We found a Resource... update the request.prepath and postpath for _ in xrange(len(path) - len(newpath)): self.prepath.append(self.postpath.pop(0)) url = quote("/" + "/".join(self.prepath) + ("/" if self.prepath and self.prepath[-1] else "")) self._rememberResource(newres, url) else: try: previousURL = self.urlForResource(res) url = quote(previousURL + path[0] + ("/" if path[0] and len(path) > 1 else "")) self._rememberResource(newres, url) except NoURLForResourceError: pass child = self._getChild(None, newres, newpath, updatepaths=updatepaths) return child
def locateChild(self, request, segments): """ Locates a child resource of this resource. @param request: the request to process. @param segments: a sequence of URL path segments. @return: a tuple of C{(child, segments)} containing the child of this resource which matches one or more of the given C{segments} in sequence, and a list of remaining segments. """ w = self.getChild(segments[0]) if w: r = iweb.IResource(w, None) if r: return r, segments[1:] return w(request), segments[1:] factory = getattr(self, 'childFactory', None) if factory is not None: r = factory(request, segments[0]) if r: return r, segments[1:] return None, []
def __init__(self, resource): """Initialize. """ self.resource = iweb.IResource(resource)