[Zope-CVS] CVS: Products/Ape/lib/apelib/zodb3 - gateways.py:1.2 serializers.py:1.2
Shane Hathaway
shane@zope.com
Wed, 9 Jul 2003 11:40:47 -0400
Update of /cvs-repository/Products/Ape/lib/apelib/zodb3
In directory cvs.zope.org:/tmp/cvs-serv4933/lib/apelib/zodb3
Modified Files:
serializers.py
Added Files:
gateways.py
Log Message:
Merged ape-newconf-branch. Mappers are now configured using XML.
=== Products/Ape/lib/apelib/zodb3/gateways.py 1.1 => 1.2 ===
--- /dev/null Wed Jul 9 11:40:46 2003
+++ Products/Ape/lib/apelib/zodb3/gateways.py Wed Jul 9 11:40:12 2003
@@ -0,0 +1,58 @@
+##############################################################################
+#
+# 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$
+"""
+
+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.2 ===
--- Products/Ape/lib/apelib/zodb3/serializers.py:1.1 Wed Apr 9 23:09:58 2003
+++ Products/Ape/lib/apelib/zodb3/serializers.py Wed Jul 9 11:40:12 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