def _cbFinishRender(self, result): def filterit(response, f): if (hasattr(f, 'handleErrors') or (response.code >= 200 and response.code < 300 and response.code != 204)): 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_transitive(self): """ Test that a special-case transitive adaption from something to IOldNevowResource to IResource is possible. """ notResource = NotOldResource() resource = iweb.IResource(notResource) self.failUnless(isinstance(resource, compat.OldNevowResourceAdapter))
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 opt_class(self, className): """A class that will be used to serve the root resource. Must implement hack.web2.iweb.IResource and take no arguments. """ if self['root']: raise usage.UsageError("You may only have one root resource.") classObj = reflect.namedClass(className) self['root'] = iweb.IResource(classObj())
def test_redundant(self): """ Test that the adaption to IResource of an object which provides IResource returns the same object. """ class Resource(object): implements(iweb.IResource) resource = Resource() self.assertIdentical(iweb.IResource(resource), resource)
def test_oldResources(self): """ Test that providers of L{IOldNevowResource} can be adapted to IResource automatically. """ class OldResource(object): implements(iweb.IOldNevowResource) oldResource = OldResource() resource = iweb.IResource(oldResource) self.failUnless(isinstance(resource, compat.OldNevowResourceAdapter))
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 opt_vhost_class(self, virtualHost): """Specify a virtual host in the form of domain=class, where class can be adapted to an iweb.IResource and has a zero-argument constructor. """ if (self['root'] and not \ isinstance(self['root'], vhost.NameVirtualHost)): raise usage.UsageError("You can not use --vhost-class with " "--path or --class.") domain, className = virtualHost.split('=', 1) if not self['root']: self['root'] = vhost.NameVirtualHost() classObj = reflect.namedClass(className) self['root'].addHost(domain, iweb.IResource(classObj()))
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: self._rememberResource(res, url) 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 not newpath is 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 x in xrange(len(path) - len(newpath)): self.prepath.append(self.postpath.pop(0)) child = self._getChild(None, newres, newpath, updatepaths=updatepaths) self._rememberResource(child, url) 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 = getattr(self, 'child_%s' % (segments[0], ), None) 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)