def __getitem__(self, name): """ Any class attribute which is an instance providing :class:`pyramid.interfaces.ILocation` will be returned as is. Attributes which are constructors for implementing classes will be replaced with a constructed instance by reifying the newly constructed resource in place of the attribute. Assignment to the sub-resources `__name__` and `__parent__` properties is handled automatically. """ factory_or_resource = getattr(self, name, None) if factory_or_resource: if ILocation.implementedBy(factory_or_resource): inst = factory_or_resource(self.request) inst.__name__ = name inst.__parent__ = self setattr(self, name, inst) return inst if ILocation.providedBy(factory_or_resource): return factory_or_resource raise KeyError(name)
def __getitem__(self, name): """ Any class attribute which is an instance providing :class:`pyramid.interfaces.ILocation` will be returned as is. Attributes which are constructors for implementing classes will be replaced with a constructed instance. Assignment to the sub-resources `__name__` and `__parent__` properties is handled automatically. """ if name in self: return super(InnerResource, self).__getitem__(name) factory_or_resource = getattr(self, name, None) if factory_or_resource: try: if ILocation.implementedBy(factory_or_resource): inst = factory_or_resource(self.request) inst.__name__ = name inst.__parent__ = self self.__dict__[name] = inst return inst except TypeError: pass try: if ILocation.providedBy(factory_or_resource): return factory_or_resource except TypeError: pass raise KeyError(name)
def __getitem__(self, name): """ While individual resources may override this method to customize the construction of sub-resources returned from traversal, make the common case easy by treating any attribute whose value is a class implementing :class:`pyramid.interfaces.ILocation` as a factory function for a sub-resource whose path component (`__name__`) is the name of the attribute itself. Any attribute which is an instance providing :class:`pyramid.interfaces.ILocation` will be returned as is. Attributes which are used as factories will replace themselves on the instance by reifying the constructed resource as the new attribute. """ factory_or_resource = getattr(self, name, None) if factory_or_resource: if ILocation.implementedBy(factory_or_resource): inst = factory_or_resource(self.request) inst.__name__ = name inst.__parent__ = self setattr(self, name, inst) return inst if ILocation.providedBy(factory_or_resource): return factory_or_resource raise KeyError(name)