dispatchToSublocations(object, event)
Dispatches an event to sublocations of a given object.
This handler is used to dispatch copy events to sublocations.
To illustrate, we'll first create a hierarchy of objects:
>>> class L(object): ... def __init__(self, name): ... self.__name__ = name ... self.__parent__ = None ... def __repr__(self): ... return '%s(%s)' % (self.__class__.__name__, self.__name__)>>> class C(L): ... implements(ISublocations) ... def __init__(self, name, *subs): ... L.__init__(self, name) ... self.subs = subs ... for sub in subs: ... sub.__parent__ = self ... def sublocations(self): ... return self.subs>>> c = C(1, ... C(11, ... L(111), ... L(112), ... ), ... C(12, ... L(121), ... L(122), ... L(123), ... L(124), ... ), ... L(13), ... )
and a handler for copy events that records the objects it's seen:
>>> seen = [] >>> def handler(ob, event): ... seen.append((ob, event.object))
Finally, we need to register our handler for copy events:
>>> from zope.lifecycleevent.interfaces import IObjectCopiedEvent >>> gsm = zope.component.getGlobalSiteManager() >>> gsm.registerHandler(handler, [None, IObjectCopiedEvent])
and this function as a dispatcher:
>>> gsm.registerHandler(dispatchToSublocations, ... [None, IObjectCopiedEvent])
When we notify that our root object has been copied:
>>> notify(ObjectCopiedEvent(c, L('')))
we see that our handler has seen all of the subobjects:
>>> seenreprs = map(repr, seen) >>> seenreprs.sort() >>> seenreprs #doctest: +NORMALIZE_WHITESPACE ['(C(1), C(1))', '(C(11), C(1))', '(C(12), C(1))', '(L(111), C(1))', '(L(112), C(1))', '(L(121), C(1))', '(L(122), C(1))', '(L(123), C(1))', '(L(124), C(1))', '(L(13), C(1))']