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

Shane Hathaway shane@zope.com
Mon, 19 May 2003 15:33:04 -0400


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

Modified Files:
	events.py gateways.py interfaces.py mapper.py 
Log Message:
Provided new machinery for initializing databases, especially creating
tables.

- Added two new interfaces, IDatabaseInitializer and
IDatabaseInitEvent.  The initializer.init(event) gets called when new
connections are made to the database.

- All gateways in the sql subpackage now implement IDatabaseInitializer.

- Added methods to mappers for configuring and using initializers.

- Renamed setUp() to init(), since it's easier to spell.

- Avoided calling initializers more than once.

- Renamed 'object' to 'obj' in some places.



=== Products/Ape/lib/apelib/core/events.py 1.2 => 1.3 ===
--- Products/Ape/lib/apelib/core/events.py:1.2	Tue Apr 29 18:11:50 2003
+++ Products/Ape/lib/apelib/core/events.py	Mon May 19 15:32:33 2003
@@ -29,6 +29,24 @@
     SIMPLE_IMMUTABLE_OBJECTS += (False, True)
 
 
+class DatabaseInitEvent:
+    """Database initialization event."""
+
+    __implements__ = interfaces.IDatabaseInitEvent
+
+    def __init__(self, connections, clearing):
+        self._connections = connections
+        self._clearing = clearing
+
+    def getConnection(self, name):
+        """Returns the named connection."""
+        return self._connections[name]
+
+    def clearing(self):
+        """Returns true if the database is to be cleared."""
+        return self._clearing
+
+
 class MapperEvent:
 
     __implements__ = interfaces.IMapperEvent


=== Products/Ape/lib/apelib/core/gateways.py 1.4 => 1.5 ===
--- Products/Ape/lib/apelib/core/gateways.py:1.4	Sun May 18 00:16:28 2003
+++ Products/Ape/lib/apelib/core/gateways.py	Mon May 19 15:32:33 2003
@@ -55,11 +55,6 @@
                 res[name] = s
         return res
 
-    def setUp(self, event, clear_all=0):
-        """Initializes the connection to the data source."""
-        for name, gw in self._gws.items():
-            gw.setUp(event, clear_all)
-
     def load(self, event):
         """Loads data.
 
@@ -109,9 +104,6 @@
 
     def getSchema(self):
         return self.schema
-
-    def setUp(self, event, clear_all=0):
-        pass
 
     def load(self, event):
         # Returns (data, serial)


=== Products/Ape/lib/apelib/core/interfaces.py 1.5 => 1.6 ===
--- Products/Ape/lib/apelib/core/interfaces.py:1.5	Sun May 18 00:16:28 2003
+++ Products/Ape/lib/apelib/core/interfaces.py	Mon May 19 15:32:33 2003
@@ -62,7 +62,7 @@
         mapper_names (a list of all mapper names)
         """
 
-    def identifyObject(object):
+    def identifyObject(obj):
         """Returns the keychain of an object.
 
         Returns None if the object is not in the keyed object system.
@@ -73,8 +73,31 @@
 
 
 
+class IDatabaseInitializer (Interface):
+    """Provides a way to initialize a database."""
+
+    def init(event):
+        """Initializes the database, creating tables etc.
+
+        event is an IDatabaseInitEvent.
+        """
+
+
+class IDatabaseInitEvent (Interface):
+    """Interface for events involved in initializing databases."""
+
+    def getConnection(name):
+        """Returns the named connection."""
+
+    def clearing():
+        """Returns true if the database is to be cleared.
+
+        This method is designed for testing purposes.
+        """
+
+
 class IMapperEvent (Interface):
-    """The base interface for events in this package."""
+    """The base interface for events occurring in context of a mapper."""
 
     def getMapper():
         """Returns the mapper for the object being (de)serialized."""
@@ -109,7 +132,7 @@
         Sometimes the system only needs the hash value for an object
         and not the full state.  When this attribute is set, the
         gateway's load() method can choose to return None as the
-        state.
+        state.  This is a read-only attribute.
         """)
 
 
@@ -236,18 +259,18 @@
         """Returns the schema of records (de)serialized by this component.
         """
 
-    def canSerialize(object):
+    def canSerialize(obj):
         """Returns true if this serializer can serialize the given object.
         """
 
-    def serialize(object, event):
+    def serialize(obj, event):
         """Returns the state of this part of the object.
 
         Use the ISerializationEvent to set up internal and external
         references.
         """
 
-    def deserialize(object, event, state):
+    def deserialize(obj, event, state):
         """Fills in the state of this part of the object.
 
         Use the IDeserializationEvent to resolve external references.
@@ -299,14 +322,6 @@
         See serial.interfaces.ISchema.
         """
 
-    def setUp(event, clear_all=0):
-        """Initializes the connection to the data source.
-
-        event is an IGatewayEvent.  The clear_all argument is for
-        testing purposes only.  If clear_all is set, the gateway
-        clears its database tables.
-        """
-
     def load(event):
         """Loads data.
 
@@ -412,7 +427,7 @@
         a serializer.
         """
 
-    def listSubMappers():
+    def listSubMapperNames():
         """Returns the name of all sub-IMappers."""
 
     def getKeychainGenerator():
@@ -422,6 +437,9 @@
         or gateway needs this, it's safe to return None.
         """
 
+    def getInitializers():
+        """Returns the list of configured IDatabaseInitializers."""
+
 
 class IConfigurableMapper (IMapper):
     """Adds operations to IMapper for configuration.
@@ -445,6 +463,13 @@
         If m is None, a default IConfigurableMapper
         implementation is created.  If replace is not set,
         the implemenation prevents overriding an existing mapper.
+        """
+
+    def addInitializer(obj):
+        """Adds a database initializer.
+
+        Database initializers get notified of new connections,
+        allowing them to prepare the database.
         """
 
     def checkConfiguration(path='root', recursive=1):


=== Products/Ape/lib/apelib/core/mapper.py 1.2 => 1.3 ===
--- Products/Ape/lib/apelib/core/mapper.py:1.2	Tue Apr 29 18:11:50 2003
+++ Products/Ape/lib/apelib/core/mapper.py	Mon May 19 15:32:33 2003
@@ -37,6 +37,7 @@
         self._gateway = gateway
         self._classifier = classifier
         self._kgen = kgen
+        self._initializers = []
 
     # IConfigurableMapper implementation
 
@@ -60,6 +61,9 @@
         self._sub_mappers[name] = m
         return m
 
+    def addInitializer(self, obj):
+        self._initializers.append(obj)
+
     def checkConfiguration(self, path='root', recursive=1):
         s = self._serializer
         if s is None:
@@ -111,7 +115,7 @@
     def getSubMapper(self, name):
         return self._sub_mappers[name]
 
-    def listSubMappers(self):
+    def listSubMapperNames(self):
         return self._sub_mappers.keys()
 
     def getClassifier(self):
@@ -127,4 +131,7 @@
         if self._parent is not None:
             return self._parent.getKeychainGenerator()
         return None
+
+    def getInitializers(self):
+        return self._initializers