[Zope-CVS] CVS: Products/AdaptableStorage/mapper - ObjectGateway.py:1.2 ObjectSerializer.py:1.2

Shane Hathaway shane@zope.com
Fri, 21 Feb 2003 11:08:39 -0500


Update of /cvs-repository/Products/AdaptableStorage/mapper
In directory cvs.zope.org:/tmp/cvs-serv5192/mapper

Modified Files:
	ObjectGateway.py ObjectSerializer.py 
Log Message:
- Fixed storage and loading of ZClass instances.
IObjectSerializer.createEmptyInstance() now accepts an optional
class_loader.  ASConnection provides a class loader that can load
ZClasses.

- Simplified configuration of mappers using "base" mappers.  Derived
mappers and gateways start with the configuration of the base mapper.

- Enhanced the configurability of ObjectGateways and
ObjectSerializers: there are now removeGateway() and removeAspect()
methods.


=== Products/AdaptableStorage/mapper/ObjectGateway.py 1.1 => 1.2 ===
--- Products/AdaptableStorage/mapper/ObjectGateway.py:1.1	Tue Dec 31 16:47:46 2002
+++ Products/AdaptableStorage/mapper/ObjectGateway.py	Fri Feb 21 11:08:09 2003
@@ -24,12 +24,21 @@
 
     __implements__ = IGateway
 
-    def __init__(self):
+    def __init__(self, base=None):
         self._gws = {}
+        if base is not None:
+            self._gws.update(base._gws)
 
-    def addGateway(self, name, gw):
+    def addGateway(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
 
+    def removeGateway(self, name):
+        del self._gws[name]  # raise KeyError if not in use
+
+    def hasGateway(self, name):
+        return self._gws.has_key(name)
 
     def getSchema(self):
         """Returns the ISchema of data stored by this gateway.


=== Products/AdaptableStorage/mapper/ObjectSerializer.py 1.1 => 1.2 ===
--- Products/AdaptableStorage/mapper/ObjectSerializer.py:1.1	Tue Dec 31 16:47:46 2002
+++ Products/AdaptableStorage/mapper/ObjectSerializer.py	Fri Feb 21 11:08:09 2003
@@ -25,17 +25,50 @@
 
     __implements__ = IObjectSerializer
 
-    def __init__(self, module, name):
+    def __init__(self, module, name, base=None):
         self._module = module
         self._name = name
-        self._aspects = []  # [(name, aspect)] -- Order matters.
+        self.init(base)
 
-    def addAspect(self, name, aspect):
-        self._aspects.append((name, aspect))
+    def init(self, base=None):
+        self._aspect_names = {}  # { name -> 1 }
+        self._aspects = []       # [(name, aspect)] -- Order matters.
+        self._final_aspects = [] # [(name, aspect)]
+        if base is not None:
+            self._aspect_names.update(base._aspect_names)
+            self._aspects[:] = base._aspects
+            self._final_aspects[:] = base._final_aspects
+
+    def addAspect(self, name, aspect, force=0, final=0):
+        if self._aspect_names.has_key(name):
+            if not force:
+                raise KeyError, "Aspect name %s in use" % repr(name)
+            self.removeAspect(name)
+        if final:
+            self._final_aspects.append((name, aspect))
+        else:
+            self._aspects.append((name, aspect))
+        self._aspect_names[name] = 1
+
+    def removeAspect(self, name):
+        if not self._aspect_names.has_key(name):
+            raise KeyError, "Aspect name %s not in use" % repr(name)
+        for lst in (self._aspects, self._final_aspects):
+            for i in range(len(lst)):
+                if lst[i][0] == name:
+                    del lst[i]
+                    break
+        del self._aspect_names[name]
+
+    def hasAspect(self, name):
+        return self._aspect_names.has_key(name)
+
+    def getAspects(self):
+        return self._aspects + self._final_aspects
 
     def getSchema(self):
         res = {}
-        for name, aspect in self._aspects:
+        for name, aspect in self.getAspects():
             s = aspect.getSchema()
             if s is not None:
                 res[name] = s
@@ -49,7 +82,7 @@
 
     def serialize(self, object, event):
         full_state = {}
-        for name, aspect in self._aspects:
+        for name, aspect in self.getAspects():
             event.setAspectName(name)
             state = aspect.serialize(object, event)
             if state is not None:
@@ -57,13 +90,16 @@
         return full_state
 
     def deserialize(self, object, event, full_state):
-        for name, aspect in self._aspects:
+        for name, aspect in self.getAspects():
             state = full_state.get(name)
             event.setAspectName(name)
             aspect.deserialize(object, event, state)
 
-    def createEmptyInstance(self, classification=None):
-        m = __import__(self._module, {}, {}, ('__doc__',))
-        c = getattr(m, self._name)
+    def createEmptyInstance(self, classification=None, class_factory=None):
+        if class_factory is not None:
+            c = class_factory(self._module, self._name)
+        else:
+            m = __import__(self._module, {}, {}, ('__doc__',))
+            c = getattr(m, self._name)
         return c.__basicnew__()