[Zope-CVS] CVS: Products/Ape/lib/apelib/core - classifiers.py:1.1.8.3 events.py:1.6.2.4 gateways.py:1.7.2.4 interfaces.py:1.9.2.4 io.py:1.6.2.4 mapper.py:1.4.4.2 oidgen.py:1.1.2.2 serializers.py:1.4.2.2

Shane Hathaway shane at zope.com
Fri Dec 19 21:53:19 EST 2003


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

Modified Files:
      Tag: ape-0_8-branch
	classifiers.py events.py gateways.py interfaces.py io.py 
	mapper.py oidgen.py serializers.py 
Log Message:
Cleaned up to the point that ApelibImplTests pass.


=== Products/Ape/lib/apelib/core/classifiers.py 1.1.8.2 => 1.1.8.3 ===
--- Products/Ape/lib/apelib/core/classifiers.py:1.1.8.2	Wed Dec 17 23:43:53 2003
+++ Products/Ape/lib/apelib/core/classifiers.py	Fri Dec 19 21:52:47 2003
@@ -20,53 +20,34 @@
 from apelib.core.interfaces import ClassificationError
 
 
-
-class FixedClassifier:
-    """Classifies objects based purely on the OID."""
+class SimpleClassifier:
+    """Classifies objects based purely on the class of the object.
+    """
 
     __implements__ = IConfigurableClassifier
 
-    def __init__(self):
-        self._oid_to_res = {}
-        self._default_res = None
+    def __init__(self, gw):
+        self._class_to_mapper = {}  # class name -> mapper_name
+        self._gw = gw
 
     def register(self, condition, value, mapper_name):
-        if condition == "oid":
-            self._oid_to_res[oid] = ({}, mapper_name)
-        elif condition == "default":
-            self._default_res = ({}, mapper_name)
-
-    def getResult(self, oid):
-        res = self._oid_to_res.get(oid)
-        if res is None:
-            res = self._default_res
-            if res is None:
-                raise ClassificationError(
-                    "OID %s is not known to fixed classifier %s" %
-                    (repr(oid), repr(self)))
-        return res
-
-    def classifyObject(self, event):
-        return self.getResult(event.oid)
-
-    def classifyState(self, event):
-        return self.getResult(event.oid)
-
-    def store(self, event, classification):
-        pass
-
-
-class NullClassifier:
-    """A null classifier refuses to classify anything."""
-
-    __implements__ = IClassifier
+        if condition == "class":
+            self._class_to_mapper[value] = mapper_name
+        else:
+            raise ConfigurationError("Unknown condition type: %s" % condition)
 
     def classifyObject(self, event):
-        raise ClassificationError("Null classifier")
+        c = event.obj.__class__
+        cname = "%s.%s" % (c.__module__, c.__name__)
+        mapper_name = self._class_to_mapper[cname]
+        return ({"class": cname}, mapper_name)
 
     def classifyState(self, event):
-        raise ClassificationError("Null classifier")
+        classification, serial = self._gw.load(event)
+        cname = classification["class"]
+        mapper_name = self._class_to_mapper[cname]
+        return ({"class": cname}, mapper_name)
 
     def store(self, event, classification):
-        pass
+        return self._gw.store(event, classification)
 


=== Products/Ape/lib/apelib/core/events.py 1.6.2.3 => 1.6.2.4 ===
--- Products/Ape/lib/apelib/core/events.py:1.6.2.3	Wed Dec 17 23:43:53 2003
+++ Products/Ape/lib/apelib/core/events.py	Fri Dec 19 21:52:47 2003
@@ -19,20 +19,15 @@
 import interfaces
 
 
-SIMPLE_IMMUTABLE_OBJECTS = (None, (), 0, 1, '', u'')
-
-try:
-    True
-except NameError:
-    pass
-else:
-    SIMPLE_IMMUTABLE_OBJECTS += (False, True)
+SIMPLE_IMMUTABLE_OBJECTS = (None, (), 0, 1, '', u'', False, True)
 
 
 class DatabaseInitEvent:
-    """Database initialization event."""
-
+    """Database initialization event.
+    """
     __implements__ = interfaces.IDatabaseInitEvent
+    connections = None
+    clear_all = False
 
     def __init__(self, connections, clear_all):
         self.connections = connections
@@ -40,8 +35,10 @@
 
 
 class MapperEvent:
-
     __implements__ = interfaces.IMapperEvent
+    conf = None
+    mapper = None
+    oid = ""
 
     def __init__(self, conf, mapper, oid):
         self.conf = conf
@@ -50,8 +47,9 @@
 
 
 class GatewayEvent (MapperEvent):
-
     __implements__ = interfaces.IGatewayEvent
+    connections = None
+    classification = None
 
     def __init__(self, conf, mapper, oid, connections, classification):
         MapperEvent.__init__(self, conf, mapper, oid)
@@ -60,15 +58,16 @@
 
 
 class LoadEvent (GatewayEvent):
-    """Object loading event."""
-
+    """Object loading event.
+    """
     __implements__ = interfaces.ILoadEvent
 
 
 class StoreEvent (GatewayEvent):
-    """Object storing event."""
-
+    """Object storing event.
+    """
     __implements__ = interfaces.IStoreEvent
+    overwrite = False
 
     def __init__(self, conf, mapper, oid, connections, classification,
                  overwrite):
@@ -78,21 +77,23 @@
 
 
 class SDEvent (MapperEvent):
-
     __implements__ = interfaces.ISDEvent
+    obj_db = None
+    obj = None
+    serializer_name = ""
+    upos = None
+    external = None
 
     def __init__(self, conf, mapper, oid, obj_db, obj):
         MapperEvent.__init__(self, conf, mapper, oid)
         self.obj_db = obj_db
         self.obj = obj
-        self.serializer_name = ''
         self.upos = []
         # self.external has the form [(oid, subobject)]
         self.external = []
 
 
 class DeserializationEvent (SDEvent):
-
     __implements__ = interfaces.IFullDeserializationEvent
 
     def __init__(self, conf, mapper, oid, obj_db, obj):
@@ -123,7 +124,6 @@
 
 
 class SerializationEvent (SDEvent):
-
     __implements__ = interfaces.IFullSerializationEvent
 
     def __init__(self, conf, mapper, oid, obj_db, obj):


=== Products/Ape/lib/apelib/core/gateways.py 1.7.2.3 => 1.7.2.4 ===
--- Products/Ape/lib/apelib/core/gateways.py:1.7.2.3	Wed Dec 17 23:43:53 2003
+++ Products/Ape/lib/apelib/core/gateways.py	Fri Dec 19 21:52:47 2003
@@ -18,13 +18,14 @@
 
 import time
 
-from interfaces import IGateway
+from interfaces import IGateway, StoreError
 
 
 class CompositeGateway:
     """Gateway that delegates to multiple smaller gateways."""
 
     __implements__ = IGateway
+    schema = None
 
     def __init__(self, base=None):
         self._gws = {}
@@ -39,17 +40,17 @@
             if s is not None:
                 self.schema[name] = s
 
-    def addGateway(self, name, gw, force=0):
+    def add(self, name, gw, force=0):
         if not force and self._gws.has_key(name):
             raise KeyError, "Gateway name %s in use" % name
         self._gws[name] = gw
         self._updateSchema()
 
-    def removeGateway(self, name):
+    def remove(self, name):
         del self._gws[name]  # raise KeyError if not in use
         self._updateSchema()
 
-    def hasGateway(self, name):
+    def has(self, name):
         return self._gws.has_key(name)
 
     def load(self, event):
@@ -99,11 +100,11 @@
         return res
 
 
-class MappingGateway:
+class RAMGateway:
     """Gateway to a simple dictionary (primarily for testing).
     """
-
     __implements__ = IGateway
+    schema = None
 
     def __init__(self, schema):
         self.schema = schema
@@ -114,6 +115,8 @@
         return self.data[event.oid]
 
     def store(self, event, data):
+        if not event.overwrite and self.data.has_key(event.oid):
+            raise StoreError("Conflict on OID %s" % repr(event.oid))
         h = time.time()
         self.data[event.oid] = (data, h)
         return h


=== Products/Ape/lib/apelib/core/interfaces.py 1.9.2.3 => 1.9.2.4 ===
--- Products/Ape/lib/apelib/core/interfaces.py:1.9.2.3	Wed Dec 17 23:43:53 2003
+++ Products/Ape/lib/apelib/core/interfaces.py	Fri Dec 19 21:52:47 2003
@@ -101,31 +101,30 @@
 class IDatabaseInitEvent (Interface):
     """Interface for events involved in initializing databases."""
 
-    connections = Attribute(description="A mapping of database connections")
+    connections = Attribute(__doc__="A mapping of database connections")
 
-    clear_all = Attribute(
-        description="""True if the database is to be cleared.
+    clear_all = Attribute(__doc__="""True if the database is to be cleared.
 
-        This attribute is designed for testing purposes.
-        """)
+    This attribute is designed for testing purposes.
+    """)
 
 
 class IMapperEvent (Interface):
     """The base interface for events occurring in context of a mapper."""
 
-    conf = Attribute(description="The IMapperConfiguration")
+    conf = Attribute(__doc__="The IMapperConfiguration")
 
-    mapper = Attribute(description="The IMapper")
+    mapper = Attribute(__doc__="The IMapper")
 
-    oid = Attribute(description="The OID of the object being mapped")
+    oid = Attribute(__doc__="The OID of the object being mapped")
 
 
 class IGatewayEvent (IMapperEvent):
     """Interface for events used by gateways."""
 
-    connections = Attribute(description="A mapping of database connections")
+    connections = Attribute(__doc__="A mapping of database connections")
 
-    classification = Attribute(description="The classification of the object.")
+    classification = Attribute(__doc__="The classification of the object.")
 
 
 class ILoadEvent (IGatewayEvent):
@@ -135,7 +134,7 @@
 class IStoreEvent (IGatewayEvent):
     """Interface for events involved in storing objects."""
 
-    overwrite = Attribute(description="""True if the gateway may overwrite.
+    overwrite = Attribute(__doc__="""True if the gateway may overwrite.
 
     When this attribute is true, gateways should overwrite existing
     data.  When it is false, gateways should not overwrite existing
@@ -146,26 +145,24 @@
 class ISDEvent (IMapperEvent):
     """Base for serialization and deserialization events."""
 
-    obj_db = Attribute(description="The relevant object database")
+    obj_db = Attribute(__doc__="The relevant object database")
 
-    obj = Attribute(description="The object being (de)serialized.")
+    obj = Attribute(__doc__="The object being (de)serialized.")
 
-    serializer_name = Attribute(description="The serializer in use.")
+    serializer_name = Attribute(__doc__="The serializer in use.")
 
-    upos = Attribute(
-        description="""The list of unmanaged persistent objects.
+    upos = Attribute(__doc__="""The list of unmanaged persistent objects.
 
-        If no attention is paid to unmanaged persistent objects (UPOs),
-        they will not notify ZODB when they are changed, and hence can be
-        a challenge for the application programmer.  Add UPOs to this list
-        so that ZODB will see changes made to them and save the
-        corresponding managed persistent object.""")
+    If no attention is paid to unmanaged persistent objects (UPOs),
+    they will not notify ZODB when they are changed, and hence can be
+    a challenge for the application programmer.  Add UPOs to this list
+    so that ZODB will see changes made to them and save the
+    corresponding managed persistent object.""")
     
-    external = Attribute(
-        description="""The list of external oids.
+    external = Attribute(__doc__="""The list of external oids.
 
-        The list is built up during (de)serialization.  It contains
-        [(oid, subobject)].""")
+    The list is built up during (de)serialization.  It contains
+    [(oid, subobject)].""")
 
 
 class IDeserializationEvent(ISDEvent):
@@ -259,7 +256,7 @@
 class ISerializer(Interface):
     """Object serializer / deserializer"""
 
-    schema = Attribute(description="The schema used by this component.")
+    schema = Attribute(__doc__="The schema used by this component.")
 
     def canSerialize(obj):
         """Returns true if this serializer can serialize the given object.
@@ -317,7 +314,7 @@
     by Martin Fowler.
     """
 
-    schema = Attribute(description="The schema used by this component.")
+    schema = Attribute(__doc__="The schema used by this component.")
 
     def load(event):
         """Loads data.
@@ -437,24 +434,23 @@
 class IMapper (Interface):
     """A hub for mapping a certain kind of object.
     """
-    serializer = Attribute(description="The IObjectSerializer for this mapper")
+    serializer = Attribute(__doc__="The IObjectSerializer for this mapper")
 
-    gateway = Attribute(description="The IGateway for this mapper")
+    gateway = Attribute(__doc__="The IGateway for this mapper")
 
-    initializers = Attribute(description="A list of IDatabaseInitializers")
+    initializers = Attribute(__doc__="A list of IDatabaseInitializers")
 
 
 class IConfigurableMapper (IMapper):
     """Adds operations to IMapper for configuration.
     """
 
-    def checkConfiguration(path='root', recursive=1):
+    def check(my_name):
         """Verifies the mapper configuration is sane.
 
-        Raises an exception if there are errors.
+        Raises a ConfigurationError if inconsistencies are detected.
 
-        'path' gives the path to the mapper, for debugging purposes.
-        'recursive' can be turned off to not descend into sub-mappers.
+        'my_name' gives the name of the mapper for debugging purposes.
         """
 
 
@@ -462,11 +458,17 @@
     """A configuration of mappers.
     """
 
-    mappers = Attribute(description="Maps mapper name to IMapper")
+    mappers = Attribute(__doc__="Maps mapper name to IMapper")
+
+    classifier = Attribute(__doc__="The IClassifier")
 
-    classifier = Attribute(description="The IClassifier")
+    oid_gen = Attribute(__doc__="The IOIDGenerator")
 
-    oid_gen = Attribute(description="The IOIDGenerator")
+    def check():
+        """Verifies the configuration is sane.
+
+        Raises a ConfigurationError if inconsistencies are detected.
+        """
 
 
 class ITPCConnection(Interface):


=== Products/Ape/lib/apelib/core/io.py 1.6.2.3 => 1.6.2.4 ===
--- Products/Ape/lib/apelib/core/io.py:1.6.2.3	Wed Dec 17 23:43:53 2003
+++ Products/Ape/lib/apelib/core/io.py	Fri Dec 19 21:52:47 2003
@@ -37,12 +37,19 @@
 
 class MapperConfiguration:
     __implements__ = IMapperConfiguration
+    mappers = None
+    classifier = None
+    oid_gen = None
 
     def __init__(self, mappers, classifier, oid_gen):
         self.mappers = mappers
         self.classifier = classifier
         self.oid_gen = oid_gen
 
+    def check(self):
+        for name, mapper in self.mappers.items():
+            mapper.check(name)
+
 
 class GatewayIO:
     """Gateway operations facade."""
@@ -255,7 +262,7 @@
         return root_obj
 
 
-    # IKeyedObjectSystem implementation
+    # IObjectDatabase implementation
 
     def getClass(self, module, name):
         # Normally called only while importing
@@ -279,9 +286,7 @@
             self._register(oid, obj)
             return obj
 
-    loadStub = getObject
-
-    def identifyObject(self, obj):
+    def identify(self, obj):
         # Normally called only while exporting
         return self._oids.get(id(obj))
 


=== Products/Ape/lib/apelib/core/mapper.py 1.4.4.1 => 1.4.4.2 ===
--- Products/Ape/lib/apelib/core/mapper.py:1.4.4.1	Sat Dec 13 23:24:46 2003
+++ Products/Ape/lib/apelib/core/mapper.py	Fri Dec 19 21:52:47 2003
@@ -23,9 +23,12 @@
 
 
 class Mapper:
-    """Standard mapper class."""
-
+    """Standard mapper class.
+    """
     __implements__ = interfaces.IConfigurableMapper
+    serializer = None
+    gateway = None
+    initializers = None
 
     def __init__(self, serializer, gateway):
         self.serializer = serializer
@@ -37,7 +40,7 @@
     def addInitializer(self, obj):
         self.initializers.append(obj)
 
-    def checkConfiguration(self, my_name):
+    def check(self, my_name):
         s = self.serializer
         if s is None:
             raise ConfigurationError(


=== Products/Ape/lib/apelib/core/oidgen.py 1.1.2.1 => 1.1.2.2 ===
--- Products/Ape/lib/apelib/core/oidgen.py:1.1.2.1	Sat Dec 13 23:24:46 2003
+++ Products/Ape/lib/apelib/core/oidgen.py	Fri Dec 19 21:52:47 2003
@@ -19,15 +19,6 @@
 from apelib.core.interfaces import IOIDGenerator, MappingError
 
 
-class NullOIDGenerator:
-    """A null OID generator refuses to generate any oids."""
-
-    __implements__ = IOIDGenerator
-
-    def new_oid(self, event, name, stored):
-        raise MappingError("Null OID generator")
-
-
 class PathOIDGenerator:
     """Path-based OID generator
     """
@@ -37,11 +28,26 @@
         if name is None:
             raise MappingError('Path OIDs require a name')
         if '/' in name:
-            raise ValueError, '%s is not a legal name' % name
-        p = event.oid  # parent OID
+            raise MappingError('%s is not a legal name in a path' % repr(name))
+        p = str(event.oid)  # parent OID
         if p.endswith('/'):
             p += name
         else:
             p = '%s/%s' % (p, name)
         return p
+
+
+class SerialOIDGenerator:
+    """Generates OIDs in series.
+    """
+    __implements__ = IOIDGenerator
+
+    counter = 0
+
+    def new_oid(self, event, name, stored):
+        if not stored:
+            raise MappingError('Serial OIDs must be stored')
+        self.counter += 1
+        oid = str(self.counter)
+        return oid
 


=== Products/Ape/lib/apelib/core/serializers.py 1.4.2.1 => 1.4.2.2 ===
--- Products/Ape/lib/apelib/core/serializers.py:1.4.2.1	Sat Dec 13 23:24:46 2003
+++ Products/Ape/lib/apelib/core/serializers.py	Fri Dec 19 21:52:47 2003
@@ -27,6 +27,7 @@
     """Full serializer based on partial serializers.
     """
     __implements__ = IFullObjectSerializer
+    schema = None
 
     def __init__(self, module, name, base=None):
         self._module = module
@@ -50,7 +51,7 @@
             if s is not None:
                 self.schema[name] = s
 
-    def addSerializer(self, name, serializer, force=0, final=0):
+    def add(self, name, serializer, force=0, final=0):
         if self._part_names.has_key(name):
             if not force:
                 raise KeyError, "Serializer name %s in use" % repr(name)
@@ -62,7 +63,7 @@
         self._part_names[name] = 1
         self._updateSchema()
 
-    def removeSerializer(self, name):
+    def remove(self, name):
         if not self._part_names.has_key(name):
             raise KeyError, "Serializer name %s not in use" % repr(name)
         for lst in (self._parts, self._final_parts):
@@ -73,7 +74,7 @@
         del self._part_names[name]
         self._updateSchema()
 
-    def hasSerializer(self, name):
+    def has(self, name):
         return self._part_names.has_key(name)
 
     def getSerializers(self):
@@ -155,12 +156,11 @@
     """Serializer that explicitly ignores an attribute
     """
     __implements__ = ISerializer
+    schema = None  # No storage
 
     def __init__(self, attrname):
         self.attrname = attrname
 
-    schema = None  # No storage
-
     def canSerialize(self, obj):
         return 1
 
@@ -177,6 +177,7 @@
     """
 
     __implements__ = ISerializer
+    schema = None
 
     def __init__(self, real, default_state=None):
         self._real = real




More information about the Zope-CVS mailing list