This package provides a pluggable way to copy persistent objects. It
was once extracted from the zc.copy package to contain much less
dependencies. In fact, we only depend on zope.interface to provide
pluggability.
The package provides a clone function that does the object cloning
and the copy wrapper that sets __parent__ and __name__ attributes
of object's copy to None. This is useful, when working with Zope's
located objects (see zope.location package). The copy function
actually calls the clone function so we'll use the first one in
the examples below. We'll also look a bit at their differences in the
end of this document.
The clone function (and thus the copy function that wraps it)
uses pickling to copy the object and all its subobjects recursively.
As each object and subobject is pickled, the function tries to adapt it
to zope.copy.interfaces.ICopyHook. If a copy hook is found,
the recursive copy is halted. The hook is called with two values: the
main, top-level object that is being copied; and a callable that supports
registering functions to be called after the copy is made. The copy hook
should return the exact object or subobject that should be used at this
point in the copy, or raise zope.copy.interfaces.ResumeCopy
exception to resume copying the object or subobject recursively after
all.
Note that we use zope's component architecture provided by the
zope.component package in this document, but the
zope.copy package itself doesn't use or depend on it, so
you can provide another adaptation mechanism as described in zope.interface's
adapter documentation.
|