[Zope3-checkins] CVS: Zope3/src/zope/app/container - copy.py:1.1.2.1

Sidnei da Silva sidnei@x3ng.com.br
Tue, 4 Feb 2003 06:33:06 -0500


Update of /cvs-repository/Zope3/src/zope/app/container
In directory cvs.zope.org:/tmp/cvs-serv24200

Added Files:
      Tag: paris-copypasterename-branch
	copy.py 
Log Message:
Adding adapters for IPasteTarget, IMoveSource and ICopySource

=== Added File Zope3/src/zope/app/container/copy.py ===
from zope.app.interfaces.container import IPasteTarget
from zope.app.proxy.introspection import removeAllProxies

class PasteTarget:

    __implements__ = IPasteTarget

    def __init__(self, container):
        self.context = container

    def acceptsObject(key, obj):
        '''Allow the container to say if it accepts the given wrapped
        object.
        
        Returns True if the object would be accepted as contents of
        this container. Otherwise, returns False.
        '''
        container = self.context
        if key in container:
            return False
        return True
        
        
    def pasteObject(key, obj):
        '''Add the given object to the container under the given key.
        
        Raises a ValueError if key is an empty string, unless the
        this object chooses a different key.
        
        Returns the key used, which might be different than the
        given key.
        
        This method must not issue an IObjectAddedEvent, nor must it
        call the manage_afterAdd hook of the object.
        However, it must publish an IObjectModified event for the
        container.
        '''
        if not isinstance(key, StringTypes):
            raise TypeError("Item name is not a string.")

        container = self.context

        if not key:
            if not (IOptionalNamesContainer.isImplementedBy(container)
                    or IContainerNamesContainer.isImplementedBy(container)):
                raise ValueError("Empty names are not allowed")

        # We remove the proxies from the object before adding it to
        # the container, because we can't store proxies.
        object = removeAllProxies(object)

        # Add the object
        key = container.setObject(key, object)

        # we dont publish the added event here
        # and dont call the after add hook

        publish(container, ObjectModifiedEvent(container))
        return key
        
class MoveSource:

    def __init__(self, container):
        self.context = container
          
    def removeObject(key, movingTo):
        '''Remove and return the object with the given key, as the
        first part of a move.
        
        movingTo is the unicode path for where the move is to.
        This method should not publish an IObjectRemovedEvent, nor should
        it call the manage_afterDelete method of the object.
        However, it must publish an IObjectModified event for the
        container.
        '''
        container = self.context

        object = container[key]
        object = ContextWrapper(object, container, name=key)

        # here, we dont call the before delete hook
        del container[key]

        # and we dont publish an ObjectRemovedEvent
        publish(container, ObjectModifiedEvent(container))

        return key

class CopySource:

    def __init__(self, container):
        self.context = container

    def copyObject(key, copyingTo):
        '''Return the object with the given key, as the first part of a
        copy.

        copyingTo is the unicode path for where the copy is to.
        '''
        value = self.context.get(key, None)
        if value is not None:
            return ContextWrapper(value, self.context, name=key)