[Zodb-checkins] CVS: Zope3/lib/python/Persistence - IPersistent.py:1.1.4.1 IPersistentDataManager.py:1.1.4.1 _persistent.py:1.1.2.1 _persistentMapping.py:1.1.2.1 _persistentmech.py:1.1.2.1 __init__.py:1.1.40.2 Persistent.py:NONE PersistentMapping.py:NONE

Jim Fulton jim@zope.com
Thu, 22 Nov 2001 16:01:35 -0500


Update of /cvs-repository/Zope3/lib/python/Persistence
In directory cvs.zope.org:/tmp/cvs-serv1759

Modified Files:
      Tag: Zope-3x-branch
	__init__.py 
Added Files:
      Tag: Zope-3x-branch
	IPersistent.py IPersistentDataManager.py _persistent.py 
	_persistentMapping.py _persistentmech.py 
Removed Files:
      Tag: Zope-3x-branch
	Persistent.py PersistentMapping.py 
Log Message:
Initial Python-only Persistent implementation for Python 2.2
without ExtensionClass. Tests too.

Renamed the modules that define Persistent and PersistentMapping
to be private. The classes are exported at the package level.



=== Added File Zope3/lib/python/Persistence/IPersistent.py ===
##############################################################################
# 
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
# 
# Copyright (c) Digital Creations.  All rights reserved.
# 
# This license has been certified as Open Source(tm).
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
# 1. Redistributions in source code must retain the above copyright
#    notice, this list of conditions, and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions, and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 
# 3. Digital Creations requests that attribution be given to Zope
#    in any manner possible. Zope includes a "Powered by Zope"
#    button that is installed by default. While it is not a license
#    violation to remove this button, it is requested that the
#    attribution remain. A significant investment has been put
#    into Zope, and this effort will continue if the Zope community
#    continues to grow. This is one way to assure that growth.
# 
# 4. All advertising materials and documentation mentioning
#    features derived from or use of this software must display
#    the following acknowledgement:
# 
#      "This product includes software developed by Digital Creations
#      for use in the Z Object Publishing Environment
#      (http://www.zope.org/)."
# 
#    In the event that the product being advertised includes an
#    intact Zope distribution (with copyright and license included)
#    then this clause is waived.
# 
# 5. Names associated with Zope or Digital Creations must not be used to
#    endorse or promote products derived from this software without
#    prior written permission from Digital Creations.
# 
# 6. Modified redistributions of any form whatsoever must retain
#    the following acknowledgment:
# 
#      "This product includes software developed by Digital Creations
#      for use in the Z Object Publishing Environment
#      (http://www.zope.org/)."
# 
#    Intact (re-)distributions of any official Zope release do not
#    require an external acknowledgement.
# 
# 7. Modifications are encouraged but must be packaged separately as
#    patches to official Zope releases.  Distributions that do not
#    clearly separate the patches from the original work must be clearly
#    labeled as unofficial distributions.  Modifications which do not
#    carry the name Zope may be packaged in any form, as long as they
#    conform to all of the clauses above.
# 
# 
# Disclaimer
# 
#   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
#   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
# 
# 
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations.  Specific
# attributions are listed in the accompanying credits file.
# 
##############################################################################

# Hack to overcome absense of Interface package
try:
    from Interface import Interface, Attribute
except ImportError:
    class Interface: pass
    def Attribute(*args): return args

class IPersistent(Interface):
    """Python persistence interface

    Note that there is a separate C API that is not included here.
    The C API requires a specific data layouit and defines an
    additional state, 'sticky' that is used to pevent object
    deactivation while in C routines.
    """

    _p_jar=Attribute(
        """The data manager for the object

        The data manager implements the IPersistentDataManager interface.
        If there is no data manager, then this is None.
        """)

    _p_oid=Attribute(
        """The object id

        It is up to the data manager to assign this.
        The special value None is resrved to indicate that an object
        id has not been assigned.
        """)

    _p_changed=Attribute(
        """The persistence state of the object

        This is one of:

        None -- The object is a ghost. It is not active.

        false -- The object is up to date (or has never been saved.

        true -- The object has been modified.

        The object state may be changed by assigning this attribute,
        however, assigning None is ignored if the object is not in the
        up-to-date state.

        Note that an object can change to the modified state only if
        it has a data manager. When such a state change occurs, the
        'register' method of the data manager is called, passing the
        persistent object.

        Deleting this attribute forces deactivation independent of
        existing state.

        Note that an attribute is used for this to allow optimized
        cache implementations.
        """)

    _p_serial=Attribute(
        """The object serial number

        This is an arbitrary object.
        """)

    def __getstate__():
        """Get the object state data.

        The state should not include peristent attributes ("_p_name")
        """

    def __setstate__(state):
        """Set the object state data

        Note that this does not affect the object's persistence state.
        """
    
    def _p_deactivate():
        """Deactivate the object

        Change the object to the ghost state is it is in the
        up-to-date state."""


=== Added File Zope3/lib/python/Persistence/IPersistentDataManager.py ===
##############################################################################
# 
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
# 
# Copyright (c) Digital Creations.  All rights reserved.
# 
# This license has been certified as Open Source(tm).
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
# 1. Redistributions in source code must retain the above copyright
#    notice, this list of conditions, and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions, and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 
# 3. Digital Creations requests that attribution be given to Zope
#    in any manner possible. Zope includes a "Powered by Zope"
#    button that is installed by default. While it is not a license
#    violation to remove this button, it is requested that the
#    attribution remain. A significant investment has been put
#    into Zope, and this effort will continue if the Zope community
#    continues to grow. This is one way to assure that growth.
# 
# 4. All advertising materials and documentation mentioning
#    features derived from or use of this software must display
#    the following acknowledgement:
# 
#      "This product includes software developed by Digital Creations
#      for use in the Z Object Publishing Environment
#      (http://www.zope.org/)."
# 
#    In the event that the product being advertised includes an
#    intact Zope distribution (with copyright and license included)
#    then this clause is waived.
# 
# 5. Names associated with Zope or Digital Creations must not be used to
#    endorse or promote products derived from this software without
#    prior written permission from Digital Creations.
# 
# 6. Modified redistributions of any form whatsoever must retain
#    the following acknowledgment:
# 
#      "This product includes software developed by Digital Creations
#      for use in the Z Object Publishing Environment
#      (http://www.zope.org/)."
# 
#    Intact (re-)distributions of any official Zope release do not
#    require an external acknowledgement.
# 
# 7. Modifications are encouraged but must be packaged separately as
#    patches to official Zope releases.  Distributions that do not
#    clearly separate the patches from the original work must be clearly
#    labeled as unofficial distributions.  Modifications which do not
#    carry the name Zope may be packaged in any form, as long as they
#    conform to all of the clauses above.
# 
# 
# Disclaimer
# 
#   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
#   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
# 
# 
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations.  Specific
# attributions are listed in the accompanying credits file.
# 
##############################################################################

# Hack to overcome absense of Interface package
try:
    from Interface import Interface
except ImportError:
    class Interface: pass

class IPersistentDataManager(Interface):
    """Provide services for managing persistent state.

    This interface is provided by ZODB Connections.

    This interface is used by persistent objects.
    """

    def setstate(object):
        """Load the state for the given object.

        The object should be in the deactivated (ghost) state.
        The object's state will be set and the object will end up
        in the up-to-date state.

        The object must implement the IPersistent interface.
        """

    def register(object):
        """Register a IPersistent with the current transaction.

        This method provides some insulation of the persistent object
        from details of transaction management. For example, it allows
        the use of per-database-connection rather than per-thread
        transaction managers.
        """

    def mtime(object):
        """Return the modification time of the object.

        The modification time may not be known, in which case None
        is returned.
        """


=== Added File Zope3/lib/python/Persistence/_persistent.py ===

from time import time

oget=object.__getattribute__
oset=object.__setattr__

class Persistent(object):
    """Mix-in class providing IPersistent support
    """

    from _persistentmech import _p_changed
    _p_changed=_p_changed()

    _p_state=0

    _p_oid = _p_jar = _p_serial = None
    
    def __getstate__(self):
        r={}
        for k, v in oget(self, '__dict__').items():
            if k[:3] not in ('_p_', '_v_'):
                r[k]=v
        return r

    def __setstate__(self, state):
        d=oget(self, '__dict__')
        for k, v in d.items():
            if k[:3] != '_p_':
                del d[k]
        d.update(state)

    def _p_deactivate(self):
        state=oget(self, '_p_state')
        if state:
            return
        if oget(self, '_p_jar') is None or oget(self, '_p_oid') is None:
            return
        
        d=oget(self, '__dict__')
        for k, v in d.items():
            if k[:3] != '_p_':
                del d[k]
        oset(self, '_p_state', None)

    def __getattribute__(self, name):
        oget=object.__getattribute__
                
        if name[:3] != '_p_' and name != '__dict__':
            oset=object.__setattr__

            state=oget(self, '_p_state')
            if state is None:
                dm=oget(self, '_p_jar')
                if dm is not None:
                    setstate(self, dm, 0)
                
            oset(self, '_p_atime', int(time()))
            
        return oget(self, name)

    def __setattr__(self, name, v):
        oget=object.__getattribute__
        oset=object.__setattr__
                
        if name[:3] != '_p_' and name != '__dict__':
            state=oget(self, '_p_state')
            if state is None:
                dm=oget(self, '_p_jar')
                if dm is None or oget(self, '_p_oid') is None:
                    raise TypeError('Attempt to modify a unreviveable ghost')
                # revivable ghost
                setstate(self, dm, 1)
                dm.register(self)
            elif not state:
                dm=oget(self, '_p_jar')
                if dm is not None:
                    oset(self, '_p_state', 1)
                    dm.register(self)
                
            oset(self, '_p_atime', int(time()))
            
        return oset(self, name, v)
        
    
def setstate(ob, dm, state=0):
    oset(ob, '_p_state', 1)
    dm.setstate(ob)
    oset(ob, '_p_state', state)
    


=== Added File Zope3/lib/python/Persistence/_persistentMapping.py ===
##############################################################################
# 
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
# 
# Copyright (c) Digital Creations.  All rights reserved.
# 
# This license has been certified as Open Source(tm).
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
# 1. Redistributions in source code must retain the above copyright
#    notice, this list of conditions, and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions, and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 
# 3. Digital Creations requests that attribution be given to Zope
#    in any manner possible. Zope includes a "Powered by Zope"
#    button that is installed by default. While it is not a license
#    violation to remove this button, it is requested that the
#    attribution remain. A significant investment has been put
#    into Zope, and this effort will continue if the Zope community
#    continues to grow. This is one way to assure that growth.
# 
# 4. All advertising materials and documentation mentioning
#    features derived from or use of this software must display
#    the following acknowledgement:
# 
#      "This product includes software developed by Digital Creations
#      for use in the Z Object Publishing Environment
#      (http://www.zope.org/)."
# 
#    In the event that the product being advertised includes an
#    intact Zope distribution (with copyright and license included)
#    then this clause is waived.
# 
# 5. Names associated with Zope or Digital Creations must not be used to
#    endorse or promote products derived from this software without
#    prior written permission from Digital Creations.
# 
# 6. Modified redistributions of any form whatsoever must retain
#    the following acknowledgment:
# 
#      "This product includes software developed by Digital Creations
#      for use in the Z Object Publishing Environment
#      (http://www.zope.org/)."
# 
#    Intact (re-)distributions of any official Zope release do not
#    require an external acknowledgement.
# 
# 7. Modifications are encouraged but must be packaged separately as
#    patches to official Zope releases.  Distributions that do not
#    clearly separate the patches from the original work must be clearly
#    labeled as unofficial distributions.  Modifications which do not
#    carry the name Zope may be packaged in any form, as long as they
#    conform to all of the clauses above.
# 
# 
# Disclaimer
# 
#   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
#   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
# 
# 
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations.  Specific
# attributions are listed in the accompanying credits file.
# 
##############################################################################
__doc__='''Python implementation of persistent base types


$Id: _persistentMapping.py,v 1.1.2.1 2001/11/22 21:01:34 jim Exp $'''
__version__='$Revision: 1.1.2.1 $'[11:-2]

import Persistence
import types

_marker=[]
class PersistentMapping(Persistence.Persistent):
    """A persistent wrapper for mapping objects.

    This class allows wrapping of mapping objects so that
    object changes are registered.  As a side effect,
    mapping objects may be subclassed.
    """

    def __init__(self,container=None):
        if container is None: container={}
        self._container=container

    def __delitem__(self, key):
        del self._container[key]
        try: del self._v_keys
        except: pass
        self.__changed__(1)

    def __getitem__(self, key):
        return self._container[key]

    def __len__(self):     return len(self._container)

    def __setitem__(self, key, v):
        self._container[key]=v
        try: del self._v_keys
        except: pass
        self.__changed__(1)

    def clear(self):
        self._container.clear()
        self._p_changed=1
        if hasattr(self,'_v_keys'): del self._v_keys

    def copy(self): return self.__class__(self._container.copy())

    def get(self, key, default=_marker):
        if default is _marker:
            return self._container.get(key)
        else:
            return self._container.get(key, default)

    def has_key(self,key): return self._container.has_key(key)

    def items(self):
        return map(lambda k, d=self: (k,d[k]), self.keys())

    def keys(self):
        try: return list(self._v_keys) # return a copy (Collector 2283)
        except: pass
        keys=self._v_keys=filter(
            lambda k: not isinstance(k,types.StringType) or k[:1]!='_',
            self._container.keys())
        keys.sort()
        return list(keys)

    def update(self, b):
        a=self._container
        for k, v in b.items(): a[k] = v
        try: del self._v_keys
        except: pass
        self._p_changed=1

    def values(self):
        return map(lambda k, d=self: d[k], self.keys())

    def __cmp__(self,other):
        return cmp(self._container, other._container)



=== Added File Zope3/lib/python/Persistence/_persistentmech.py ===

class _p_changed(object):
    """Special attribute (descriptor) for controlling persistent state.
    """

    def __get__(self, ob, klass=None):
        if ob is None: raise AttributeError, '_p_changed'
        return object.__getattribute__(ob, '_p_state')

    def __set__(self, ob, val):
        oget=object.__getattribute__

        if oget(ob, '_p_jar') is None or oget(ob, '_p_oid') is None:
            return

        state = oget(ob, '_p_state')
        if state is val: return

        oset=object.__setattr__
        if state:
            # changed
            if val==0:
                oset(ob, '_p_state', 0)
        elif state==0:
            # unchanged, but not a ghost
            if val:
                oset(ob, '_p_state', 1)
            elif val is None:
                oget(ob, '_p_deactivate')()
                oset(ob, '_p_state', None)
        else:
            # Ghost. Note val can't be None, cuz then val would equal state.
            oget(ob, '_p_jar').setstate(ob)
            oset(ob, '_p_state', not not state)

    def __del__(self, ob):

        oget=object.__getattribute__
        
        if oget(ob, '_p_jar') is None or oget(ob, '_p_oid') is None:
            return
        
        state = oget(ob, '_p_state')
        if state is not None:
            oset=object.__setattr__
            oset(ob, '_p_state', 0)
            oget(ob, '_p_deactivate')()
            oset(ob, '_p_state', None)


=== Zope3/lib/python/Persistence/__init__.py 1.1.40.1 => 1.1.40.2 ===
 """
 
-from Persistent import Persistent
+from _persistent import Persistent
+Persistent.__module__='Persistence'
+from _persistentMapping import PersistentMapping
+PersistentMapping.__module__='Persistence'

=== Removed File Zope3/lib/python/Persistence/Persistent.py ===

=== Removed File Zope3/lib/python/Persistence/PersistentMapping.py ===