[Zope-CVS] CVS: Products/Ape/lib/apelib/core - events.py:1.2 exceptions.py:1.2 gateways.py:1.2 interfaces.py:1.3 mapper.py:1.2

Shane Hathaway shane@zope.com
Tue, 29 Apr 2003 18:12:20 -0400


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

Modified Files:
	events.py exceptions.py gateways.py interfaces.py mapper.py 
Log Message:
- Added IGatewayEvent, the base interface for ILoadEvent and
IStoreEvent.

- Filesystem gateways no longer refer directly to a connection.  They
get the connection from the event.  This is to permit replacing the
connection with a zip/tar file reader/writer or some other interesting
thing.

- Added checkConnection() to gateways for checking the connection
configuration early.

- Added ConfigurationError and changed some places that were raising
RuntimeError to raise ConfigurationError instead.

- Changed some calls to getKeyChain()[-1] to simply getKey()

- Updated module import style in some places

- Various other style improvements




=== Products/Ape/lib/apelib/core/events.py 1.1 => 1.2 ===
--- Products/Ape/lib/apelib/core/events.py:1.1	Wed Apr  9 23:09:55 2003
+++ Products/Ape/lib/apelib/core/events.py	Tue Apr 29 18:11:50 2003
@@ -16,9 +16,7 @@
 $Id$
 """
 
-from interfaces \
-     import IMapperEvent, ILoadEvent, IStoreEvent, ISDEvent, \
-     IFullDeserializationEvent, IFullSerializationEvent
+import interfaces
 
 
 SIMPLE_IMMUTABLE_OBJECTS = (None, (), 0, 1, '', u'')
@@ -33,7 +31,7 @@
 
 class MapperEvent:
 
-    __implements__ = IMapperEvent
+    __implements__ = interfaces.IMapperEvent
 
     def __init__(self, mapper, keychain):
         self._mapper = mapper
@@ -56,23 +54,36 @@
         return kcg.makeKeychain(self, name, stored)
 
 
-class LoadEvent (MapperEvent):
+class GatewayEvent (MapperEvent):
+
+    __implements__ = interfaces.IGatewayEvent
+
+    def __init__(self, mapper, keychain, connections):
+        MapperEvent.__init__(self, mapper, keychain)
+        self._connections = connections
+
+    def getConnection(self, name):
+        """Returns the named connection."""
+        return self._connections[name]
+
+
+class LoadEvent (GatewayEvent):
     """Object loading event."""
 
-    __implements__ = ILoadEvent
+    __implements__ = interfaces.ILoadEvent
 
     hash_only = 0
 
 
-class StoreEvent (MapperEvent):
+class StoreEvent (GatewayEvent):
     """Object storing event."""
 
-    __implements__ = IStoreEvent
+    __implements__ = interfaces.IStoreEvent
 
 
 class SDEvent (MapperEvent):
 
-    __implements__ = ISDEvent
+    __implements__ = interfaces.ISDEvent
 
     _serializer_name = ''
 
@@ -114,7 +125,7 @@
 
 class DeserializationEvent (SDEvent):
 
-    __implements__ = IFullDeserializationEvent
+    __implements__ = interfaces.IFullDeserializationEvent
 
     def __init__(self, keyed_ob_sys, object_mapper, keychain, object):
         SDEvent.__init__(
@@ -149,7 +160,7 @@
 
 class SerializationEvent (SDEvent):
 
-    __implements__ = IFullSerializationEvent
+    __implements__ = interfaces.IFullSerializationEvent
 
     def __init__(self, keyed_ob_sys, object_mapper, keychain, object):
         SDEvent.__init__(


=== Products/Ape/lib/apelib/core/exceptions.py 1.1 => 1.2 ===
--- Products/Ape/lib/apelib/core/exceptions.py:1.1	Wed Apr  9 23:09:55 2003
+++ Products/Ape/lib/apelib/core/exceptions.py	Tue Apr 29 18:11:50 2003
@@ -28,3 +28,5 @@
 class NoStateFoundError(MappingError):
     """No state is there to load"""
 
+class ConfigurationError(Exception):
+    """Invalid mapper configuration"""


=== Products/Ape/lib/apelib/core/gateways.py 1.1 => 1.2 ===
--- Products/Ape/lib/apelib/core/gateways.py:1.1	Wed Apr  9 23:09:55 2003
+++ Products/Ape/lib/apelib/core/gateways.py	Tue Apr 29 18:11:50 2003
@@ -55,6 +55,11 @@
                 res[name] = s
         return res
 
+    def checkConnection(self, event):
+        """Verifies that each gateway can connect to its data source."""
+        for name, gw in self._gws.items():
+            gw.checkConnection(event)
+
     def load(self, event):
         """Loads data.
 
@@ -104,6 +109,9 @@
 
     def getSchema(self):
         return self.schema
+
+    def checkConnection(self, event):
+        pass
 
     def load(self, event):
         # Returns (data, serial)


=== Products/Ape/lib/apelib/core/interfaces.py 1.2 => 1.3 ===
--- Products/Ape/lib/apelib/core/interfaces.py:1.2	Fri Apr 11 02:17:49 2003
+++ Products/Ape/lib/apelib/core/interfaces.py	Tue Apr 29 18:11:50 2003
@@ -93,7 +93,14 @@
         """
 
 
-class ILoadEvent (IMapperEvent):
+class IGatewayEvent (IMapperEvent):
+    """Interface for events used by gateways."""
+
+    def getConnection(name):
+        """Returns the named connection."""
+
+
+class ILoadEvent (IGatewayEvent):
     """Interface for events involved in loading objects."""
 
     hash_only = Attribute(
@@ -106,7 +113,7 @@
         """)
 
 
-class IStoreEvent (IMapperEvent):
+class IStoreEvent (IGatewayEvent):
     """Interface for events involved in storing objects."""
 
 
@@ -292,6 +299,12 @@
         See serial.interfaces.ISchema.
         """
 
+    def checkConnection(event):
+        """Verifies that the event provides a usable connection.
+
+        event is an IGatewayEvent.
+        """
+
     def load(event):
         """Loads data.
 
@@ -395,6 +408,9 @@
         The name of a sub-mapper is chosen by either a classifier or
         a serializer.
         """
+
+    def listSubMappers():
+        """Returns the name of all sub-IMappers."""
 
     def getKeychainGenerator():
         """Returns the IKeychainGenerator for subobjects.


=== Products/Ape/lib/apelib/core/mapper.py 1.1 => 1.2 ===
--- Products/Ape/lib/apelib/core/mapper.py:1.1	Wed Apr  9 23:09:55 2003
+++ Products/Ape/lib/apelib/core/mapper.py	Tue Apr 29 18:11:50 2003
@@ -16,15 +16,14 @@
 $Id$
 """
 
-from interfaces \
-     import IConfigurableMapper, IMapper, IFullObjectSerializer, \
-     IGateway, IClassifier, IKeychainGenerator
+import interfaces
+from exceptions import ConfigurationError
 
 
 class Mapper:
     """Standard mapper class."""
 
-    __implements__ = IConfigurableMapper
+    __implements__ = interfaces.IConfigurableMapper
 
     def __init__(self,
                  parent=None,
@@ -64,35 +63,37 @@
     def checkConfiguration(self, path='root', recursive=1):
         s = self._serializer
         if s is None:
-            raise RuntimeError(
+            raise ConfigurationError(
                 'No serializer configured for mapper %s' % repr(path))
-        if not IFullObjectSerializer.isImplementedBy(s):
-            raise RuntimeError(
+        if not interfaces.IFullObjectSerializer.isImplementedBy(s):
+            raise ConfigurationError(
                 'Not an IFullObjectSerializer: %s' % repr(s))
         g = self._gateway
         if g is None:
-            raise RuntimeError(
+            raise ConfigurationError(
                 'No gateway configured for mapper %s' % repr(path))
-        if not IGateway.isImplementedBy(g):
-            raise RuntimeError(
+        if not interfaces.IGateway.isImplementedBy(g):
+            raise ConfigurationError(
                 'Not an IGateway: %s' % repr(g))
         if s.getSchema() != g.getSchema():
-            raise RuntimeError('Mismatched schemas in mapper %s: %s != %s' % (
+            raise ConfigurationError(
+                'Mismatched schemas in mapper %s: %s != %s' % (
                 repr(path), s.getSchema(), g.getSchema()))
         if self._parent is None:
             if self._classifier is None:
-                raise RuntimeError('No root classifier configured')
-            if not IClassifier.isImplementedBy(self._classifier):
-                raise RuntimeError(
+                raise ConfigurationError('No root classifier configured')
+            if not interfaces.IClassifier.isImplementedBy(self._classifier):
+                raise ConfigurationError(
                     'Not an IClassifier: %s' % repr(self._classifier))
             if self._kgen is None:
-                raise RuntimeError('No root keychain generator configured')
-            if not IKeychainGenerator.isImplementedBy(self._kgen):
-                raise RuntimeError(
+                raise ConfigurationError(
+                    'No root keychain generator configured')
+            if not interfaces.IKeychainGenerator.isImplementedBy(self._kgen):
+                raise ConfigurationError(
                     'Not an IKeychainGenerator: %s' % repr(self._kgen))
         else:
-            if not IMapper.isImplementedBy(self._parent):
-                raise RuntimeError(
+            if not interfaces.IMapper.isImplementedBy(self._parent):
+                raise ConfigurationError(
                     'Not an IMapper: %s' % repr(self._parent))
 
         if recursive:
@@ -109,6 +110,9 @@
 
     def getSubMapper(self, name):
         return self._sub_mappers[name]
+
+    def listSubMappers(self):
+        return self._sub_mappers.keys()
 
     def getClassifier(self):
         if self._classifier is not None: