Adapter for copying objects between containers
To use an object copier, pass a contained object to the class. The contained object should implement IContained. It should be contained in a container that has an adapter to INameChooser.
>>> from zope.app.container.contained import Contained >>> ob = Contained() >>> container = ExampleContainer() >>> container[u'foo'] = ob >>> copier = ObjectCopier(ob)
In addition to moving objects, object copiers can tell you if the object is movable:
>>> copier.copyable() True
which, at least for now, they always are. A better question to ask is whether we can copy to a particular container. Right now, we can always copy to a container of the same class:
>>> container2 = ExampleContainer() >>> copier.copyableTo(container2) True >>> copier.copyableTo({}) Traceback (most recent call last): ... TypeError: Container is not a valid Zope container.
Of course, once we've decided we can copy an object, we can use the copier to do so:
>>> copier.copyTo(container2) u'foo' >>> list(container) [u'foo'] >>> list(container2) [u'foo'] >>> ob.__parent__ is container True >>> container2[u'foo'] is ob False >>> container2[u'foo'].__parent__ is container2 True >>> container2[u'foo'].__name__ u'foo'
We can also specify a name:
>>> copier.copyTo(container2, u'bar') u'bar' >>> l = list(container2) >>> l.sort() >>> l [u'bar', u'foo']
>>> ob.__parent__ is container True >>> container2[u'bar'] is ob False >>> container2[u'bar'].__parent__ is container2 True >>> container2[u'bar'].__name__ u'bar'
But we may not use the same name given, if the name is already in use:
>>> copier.copyTo(container2, u'bar') u'bar_' >>> l = list(container2) >>> l.sort() >>> l [u'bar', u'bar_', u'foo'] >>> container2[u'bar_'].__name__ u'bar_'
If we try to copy to an invalid container, we'll get an error:
>>> copier.copyTo({}) Traceback (most recent call last): ... TypeError: Container is not a valid Zope container.
Do a test for preconditions:
>>> import zope.interface >>> import zope.schema >>> def preNoZ(container, name, ob): ... "Silly precondition example" ... if name.startswith("Z"): ... raise zope.interface.Invalid("Invalid name.")
>>> class I1(zope.interface.Interface): ... def __setitem__(name, on): ... "Add an item" ... __setitem__.precondition = preNoZ
>>> from zope.app.container.interfaces import IContainer >>> class C1(object): ... zope.interface.implements(I1, IContainer) ... def __repr__(self): ... return 'C1'
>>> from zope.app.container.constraints import checkObject >>> container3 = C1() >>> copier.copyableTo(container3, 'ZDummy') False >>> copier.copyableTo(container3, 'newName') True
And a test for constraints:
>>> def con1(container): ... "silly container constraint" ... if not hasattr(container, 'x'): ... return False ... return True ... >>> class I2(zope.interface.Interface): ... __parent__ = zope.schema.Field(constraint=con1) ... >>> class constrainedObject(object): ... zope.interface.implements(I2) ... def __init__(self): ... self.__name__ = 'constrainedObject' ... >>> cO = constrainedObject() >>> copier2 = ObjectCopier(cO) >>> copier2.copyableTo(container) False >>> container.x = 1 >>> copier2.copyableTo(container) True
There are no attributes in this class.
copyTo(target, new_name=None)
Copy this object to the target given.
Returns the new name within the target.
Typically, the target is adapted to IPasteTarget. After the copy is added to the target container, publish an IObjectCopied event in the context of the target container. If a new object is created as part of the copying process, then an IObjectCreated event should be published.
copyable()
Returns True if the object is copyable, otherwise False.
copyableTo(target, name=None)
Say whether the object can be copied to the given target.
Returns True if it can be copied there. Otherwise, returns False.
There are no known subclasses.