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 OPSI.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 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, []