containedEvent(object, container, name=None)
Establish the containment of the object in the container
The object and necessary event are returned. The object may be a ContainedProxy around the original object. The event is an added event, a moved event, or None.
If the object implements IContained, simply set its __parent__ and __name__ attributes:
>>> container = {} >>> item = Contained() >>> x, event = containedEvent(item, container, u'foo') >>> x is item True >>> item.__parent__ is container True >>> item.__name__ u'foo'
We have an added event:
>>> event.__class__.__name__ 'ObjectAddedEvent' >>> event.object is item True >>> event.newParent is container True >>> event.newName u'foo' >>> event.oldParent >>> event.oldName
Now if we call contained again:
>>> x2, event = containedEvent(item, container, u'foo') >>> x2 is item True >>> item.__parent__ is container True >>> item.__name__ u'foo'
We don't get a new added event:
>>> event
If the object already had a parent but the parent or name was different, we get a moved event:
>>> x, event = containedEvent(item, container, u'foo2') >>> event.__class__.__name__ 'ObjectMovedEvent' >>> event.object is item True >>> event.newParent is container True >>> event.newName u'foo2' >>> event.oldParent is container True >>> event.oldName u'foo'
If the object implements ILocation, but not IContained, set its __parent__ and __name__ attributes and declare that it implements IContained:
>>> from zope.location import Location >>> item = Location() >>> IContained.providedBy(item) False >>> x, event = containedEvent(item, container, 'foo') >>> x is item True >>> item.__parent__ is container True >>> item.__name__ 'foo' >>> IContained.providedBy(item) True
If the object doesn't even implement ILocation, put a ContainedProxy around it:
>>> item = [] >>> x, event = containedEvent(item, container, 'foo') >>> x is item False >>> x.__parent__ is container True >>> x.__name__ 'foo'
Make sure we don't lose existing directly provided interfaces.
>>> from zope.interface import Interface, directlyProvides >>> class IOther(Interface): ... pass >>> from zope.location import Location >>> item = Location() >>> directlyProvides(item, IOther) >>> IOther.providedBy(item) True >>> x, event = containedEvent(item, container, 'foo') >>> IOther.providedBy(item) True