[Zope-CVS] CVS: Products/Ape/lib/apelib/zodb3 - gateways.py:1.1.2.1 serializers.py:1.1.4.1

Shane Hathaway shane@zope.com
Tue, 24 Jun 2003 17:39:14 -0400


Update of /cvs-repository/Products/Ape/lib/apelib/zodb3
In directory cvs.zope.org:/tmp/cvs-serv2045/zodb3

Modified Files:
      Tag: ape-newconf-branch
	serializers.py 
Added Files:
      Tag: ape-newconf-branch
	gateways.py 
Log Message:
Reduced the number of things necessary to configure in a mapper.

This change centered around removing the arguments to the constructor
in zope2.basemapper.  They were making it hard to write a
configuration vocabulary.

Removed the app_key argument through the use of a read-only gateway
instead of a fixed persistent mapping, and removed the
stored_keychains argument by always storing keychains.  The filesystem
version of the folder items gateway doesn't actually store the
keychains, but it can verify that when the keychains are loaded again
they will be the same as they were at storage time.



=== Added File Products/Ape/lib/apelib/zodb3/gateways.py ===
##############################################################################
#
# Copyright (c) 2003 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.
#
##############################################################################
"""Gateways specific to ZODB3.

$Id: gateways.py,v 1.1.2.1 2003/06/24 21:38:43 shane Exp $
"""

from apelib.core.interfaces import IGateway
from apelib.core.schemas import RowSequenceSchema


class ReadOnlyItems:
    """Pseudo-storage of persistent mapping items.

    This is generally used for the database root.
    """

    __implements__ = IGateway

    schema = RowSequenceSchema()
    schema.addField('key', 'string', 1)
    schema.addField('keychain', 'keychain')

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

    def set(self, key, keychain):
        self.keychains[key] = keychain

    def getSchema(self):
        return self.schema

    def load(self, event):
        items = self.keychains.items()
        items.sort()
        return items, None

    def store(self, event, data):
        expect = self.load(event)
        if data != expect:
            raise exceptions.StoreError(
                "Attempted to store %s in a read-only gateway.  Should be %s."
                % (repr(data), repr(expect)))
        return None



=== Products/Ape/lib/apelib/zodb3/serializers.py 1.1 => 1.1.4.1 ===
--- Products/Ape/lib/apelib/zodb3/serializers.py:1.1	Wed Apr  9 23:09:58 2003
+++ Products/Ape/lib/apelib/zodb3/serializers.py	Tue Jun 24 17:38:43 2003
@@ -26,17 +26,56 @@
 from ZODB.TimeStamp import TimeStamp
 
 from apelib.core.interfaces \
-     import ISerializer, IFullSerializationEvent, \
-     IFullDeserializationEvent
+     import ISerializer, IFullSerializationEvent, IFullDeserializationEvent
 from apelib.core.events import SerializationEvent, DeserializationEvent
 from apelib.core.exceptions import SerializationError
-from apelib.core.schemas import FieldSchema
+from apelib.core.schemas import RowSequenceSchema, FieldSchema
+
+
+class BasicPersistentMapping:
+    """Basic PersistentMapping (de)serializer
+
+    This version assumes the PM maps string keys to object references.
+    """
+    __implements__ = ISerializer
+
+    schema = RowSequenceSchema()
+    schema.addField('key', 'string', 1)
+    schema.addField('keychain', 'keychain')
+
+    def getSchema(self):
+        return self.schema
+
+    def canSerialize(self, object):
+        return isinstance(object, PersistentMapping)
+
+    def serialize(self, obj, event):
+        assert self.canSerialize(obj)
+        res = []
+        for key, value in obj.items():
+            keychain = event.identifyObject(value)
+            if keychain is None:
+                keychain = event.makeKeychain(key, 1)
+            event.notifySerializedRef(key, value, 0, keychain)
+            res.append((key, keychain))
+        event.ignoreAttribute('data')
+        event.ignoreAttribute('_container')
+        return res
+
+    def deserialize(self, obj, event, state):
+        assert self.canSerialize(obj)
+        data = {}
+        for (key, keychain) in state:
+            value = event.dereference(key, keychain)
+            data[key] = value
+        obj.__init__(data)
 
 
 class FixedPersistentMapping:
     """Unchanging persistent mapping.
 
-    Generally used for a ZODB root object."""
+    Generally used for a ZODB root object.
+    """
 
     __implements__ = ISerializer