[Zope-CVS] CVS: Products/AdaptableStorage/serial_std - .cvsignore:1.1 FixedPersistentMapping.py:1.1 FullState.py:1.1 IgnoredAttribute.py:1.1 MappingGateway.py:1.1 RemainingState.py:1.1 RollCall.py:1.1 StringDataAttribute.py:1.1 __init__.py:1.1 public.py:1.1 serial_public.py:1.1

Shane Hathaway shane@zope.com
Wed, 27 Nov 2002 13:37:09 -0500


Update of /cvs-repository/Products/AdaptableStorage/serial_std
In directory cvs.zope.org:/tmp/cvs-serv12157/serial_std

Added Files:
	.cvsignore FixedPersistentMapping.py FullState.py 
	IgnoredAttribute.py MappingGateway.py RemainingState.py 
	RollCall.py StringDataAttribute.py __init__.py public.py 
	serial_public.py 
Log Message:
Moved the latest AdaptableStorage work out of the private repository.
It took a long time, but I moved it as soon as all the unit tests
passed and I felt that all the interface names and conventions were
good enough.

Documentation is still minimal, but now I think the system is finally
straight enough in my head to write down. :-) If you want a sneak
peek, the interfaces have some docstrings, if you're looking for a
"tree" view, while the OpenOffice diagram presents something of a
"forest" view.

Also note that I'm trying a new coding convention.  The "public"
module in each package defines exactly which objects should be
exported from the package.  This solves a few problems with imports
such as doubling of names and shadowing of modules.  Overall, the
"public" module makes it easier to tell which classes are supposed to
be used by other packages, and makes it easier for other packages to
use the public classes.  See what you think.



=== Added File Products/AdaptableStorage/serial_std/.cvsignore ===
*.pyc


=== Added File Products/AdaptableStorage/serial_std/FixedPersistentMapping.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Unchanging persistent mapping.  Generally used for a ZODB root object.

$Id: FixedPersistentMapping.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""

from serial_public import IAspectSerializer, RecordSchema


class FixedPersistentMapping:
    __implements__ = IAspectSerializer

    def __init__(self, map):
        # map: { name -> (mapper_name, key) }
        self.map = map

    def getSchema(self):
        return None  # No storage

    def serialize(self, object, event):
        names = object.keys()
        names.sort()
        expected = self.map.keys()
        expected.sort()
        assert names == expected, '%s != %s' % (names, expected)

        for name in names:
            mapper_name, key = self.map[name]
            subob = object[name]
            event.notifySerializedRef(name, subob,
                                      mapper_name=mapper_name, key=key)

        # One of the two will work. ;-)
        event.ignoreAttribute('data')
        event.ignoreAttribute('_container')


    def deserialize(self, object, event, state):
        assert state is None
        data = {}
        for name, (mapper_name, key) in self.map.items():
            subob = event.dereference(name, mapper_name=mapper_name, key=key)
            data[name] = subob
        # The PersistentMapping doesn't have its data or _container
        # attribute yet, and we don't know what its name should be
        # since PersistentMapping's internal structure is not fixed.
        # So call the PersistentMapping's constructor.
        object.__init__(data)



=== Added File Products/AdaptableStorage/serial_std/FullState.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Aspect that loads/stores the entire state of an object

$Id: FullState.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""

from serial_public import IAspectSerializer, RecordSchema


class FullState:
    __implements__ = IAspectSerializer

    schema = RecordSchema()
    schema.addColumn('data', 'object')

    def getSchema(self):
        return self.schema

    def serialize(self, object, event):
        return ((object.__getstate__(),),)

    def deserialize(self, object, event, state):
        assert len(state) == 1
        assert len(state[0]) == 1
        object.__setstate__(state[0][0])



=== Added File Products/AdaptableStorage/serial_std/IgnoredAttribute.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Aspect that explicitly ignores an attribute

$Id: IgnoredAttribute.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""


from serial_public import IAspectSerializer, RecordSchema


class IgnoredAttribute:
    __implements__ = IAspectSerializer

    def __init__(self, attrname):
        self.attrname = attrname

    def getSchema(self):
        return None  # No storage

    def serialize(self, object, event):
        event.ignoreAttribute(self.attrname)
        return None

    def deserialize(self, object, event, state):
        assert state is None



=== Added File Products/AdaptableStorage/serial_std/MappingGateway.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Gateway to a simple dictionary (primarily for testing)

$Id: MappingGateway.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""

import time

from serial_public import IGateway

class MappingGateway:
    """Stores data in a mapping."""

    __implements__ = IGateway

    def __init__(self, schema):
        self.schema = schema
        self.data = {}

    def getSchema(self):
        return self.schema

    def load(self, object_mapper, key):
        # Returns (data, serial)
        return self.data[key]

    def store(self, object_mapper, key, data):
        serial = time.time()
        self.data[key] = (data, serial)
        return serial


=== Added File Products/AdaptableStorage/serial_std/RemainingState.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Aspect for (de)serializing the remaining state of an object

$Id: RemainingState.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""

from cStringIO import StringIO
from cPickle import Pickler, Unpickler
from types import DictType

from serial_public import \
     IAspectSerializer, RecordSchema, \
     IFullSerializationEvent, IFullDeserializationEvent


class RemainingState:
    __implements__ = IAspectSerializer

    schema = RecordSchema()
    schema.addColumn('data', 'string')

    def getSchema(self):
        return self.schema


    def serialize(self, object, event):
        assert IFullSerializationEvent.isImplementedBy(event)
        state = object.__getstate__()
        assert isinstance(state, DictType)
        for attrname in event.getSerializedAttributeNames():
            if state.has_key(attrname):
                del state[attrname]
        if not state:
            # No data needs to be stored
            return None

        outfile = StringIO()
        p = Pickler(outfile)
        p.persistent_id = event.getInternalReference
        p.dump(state)
        s = outfile.getvalue()
        return ((s,),)


    def deserialize(self, object, event, state):
        assert IFullDeserializationEvent.isImplementedBy(event)
        if not state:
            return
        assert len(state) == 1
        assert len(state[0]) == 1
        data = state[0][0]
        assert data

        infile = StringIO(data)
        u = Unpickler(infile)
        u.persistent_load = event.loadInternalReference
        state = u.load()
        object.__dict__.update(state)



=== Added File Products/AdaptableStorage/serial_std/RollCall.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Roll call aspect.

$Id: RollCall.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""

from serial_public import \
     IAspectSerializer, IFullSerializationEvent, SerializationError


class RollCall:
    """Helps ensure all parts of an object get serialized.

    Designed for debugging purposes.
    """
    __implements__ = IAspectSerializer

    def getSchema(self):
        return None  # No storage

    def serialize(self, object, tracker):
        assert IFullSerializationEvent.isImplementedBy(tracker)
        attrs = tracker.getSerializedAttributeNames()
        attrs_map = {}
        for attr in attrs:
            attrs_map[attr] = 1
        missed = []
        for k in object.__dict__.keys():
            if not k.startswith('_v_') and not attrs_map.has_key(k):
                missed.append(repr(k))
        if missed:
            raise SerializationError(
                'Attribute(s) %s of %s not serialized' %
                (', '.join(missed), repr(object)))
        return None

    def deserialize(self, object, tracker, state):
        assert state is None




=== Added File Products/AdaptableStorage/serial_std/StringDataAttribute.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Aspect for a simple string data attribute.

$Id: StringDataAttribute.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""

from types import StringType

from serial_public import IAspectSerializer, RecordSchema


class StringDataAttribute:
    __implements__ = IAspectSerializer

    schema = RecordSchema()
    schema.addColumn('data', 'string')

    def __init__(self, attrname):
        self.attrname = attrname

    def getSchema(self):
        return self.schema

    def serialize(self, object, event):
        attrname = self.attrname
        assert attrname
        v = getattr(object, attrname)
        assert isinstance(v, StringType)
        event.notifySerialized(attrname, v, 1)
        return ((v,),)

    def deserialize(self, object, event, state):
        attrname = self.attrname
        assert attrname
        assert len(state) == 1
        assert len(state[0]) == 1
        v = state[0][0]
        assert isinstance(v, StringType)
        setattr(object, attrname, v)
        event.notifyDeserialized(attrname, v)



=== Added File Products/AdaptableStorage/serial_std/__init__.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Package containing some standard aspect serializers.

$Id: __init__.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""



=== Added File Products/AdaptableStorage/serial_std/public.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Public implementations in the serial_std package.

$Id: public.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""

from FixedPersistentMapping import FixedPersistentMapping
from FullState import FullState
from IgnoredAttribute import IgnoredAttribute
from MappingGateway import MappingGateway
from RemainingState import RemainingState
from RollCall import RollCall
from StringDataAttribute import StringDataAttribute


=== Added File Products/AdaptableStorage/serial_std/serial_public.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Import of the public classes and interfaces from the serial package.

$Id: serial_public.py,v 1.1 2002/11/27 18:37:07 shane Exp $
"""

from Products.AdaptableStorage.serial.public import *