[Zope-CVS] CVS: Products/AdaptableStorage/serial_ofs - FolderItems.py:1.3 MetaTypeClassifier.py:1.4

Shane Hathaway shane@zope.com
Fri, 6 Dec 2002 17:07:21 -0500


Update of /cvs-repository/Products/AdaptableStorage/serial_ofs
In directory cvs.zope.org:/tmp/cvs-serv8198/serial_ofs

Modified Files:
	FolderItems.py MetaTypeClassifier.py 
Log Message:
Experiment: removed mapper_name from OIDs, with the intent of allowing
loading and storage by different mappers depending on what the classifier
specifies.  Not yet complete.  Involved changes to virtually every module. :-)

I may decide to revert this.  The shane-before-mapper-name-removal tag
was added just before this checkin.


=== Products/AdaptableStorage/serial_ofs/FolderItems.py 1.2 => 1.3 ===
--- Products/AdaptableStorage/serial_ofs/FolderItems.py:1.2	Wed Dec  4 23:18:08 2002
+++ Products/AdaptableStorage/serial_ofs/FolderItems.py	Fri Dec  6 17:06:50 2002
@@ -30,7 +30,7 @@
 
     schema = RecordSchema()
     schema.addColumn('id', 'string', 1)
-    schema.addColumn('classification', 'classification', 0)
+    schema.addColumn('keychain', 'keychain', 0)
 
     def getSchema(self):
         return self.schema
@@ -41,21 +41,15 @@
         event.ignoreAttribute('_objects')
         for id, subob in object.objectItems():
             base = aq_base(subob)
-            classification, mapper_name = event.classifyObject(base)
-            info = event.identifyObject(base)
-            if info is not None:
-                assert info[0] == mapper_name, 'Object changed mapper'
-                key = info[1]
-            else:
-                key = event.makeKey(mapper_name, id, 0)
-            event.notifySerializedRef(id, base, 1, mapper_name, key)
-            state.append((id, classification))
+            keychain = event.identifyObject(base)
+            event.notifySerializedRef(id, base, 1, keychain)
+            state.append((id, keychain))
         return state
 
     def deserialize(self, object, event, state):
         assert isinstance(object, ObjectManager)
-        for id, classification in state:
-            subob = event.dereference(id, classification=classification)
+        for id, keychain in state:
+            subob = event.dereference(id, keychain)
             setattr(object, id, subob)
             object._objects += ({'id': id, 'meta_type':
                                  subob.__class__.meta_type},)


=== Products/AdaptableStorage/serial_ofs/MetaTypeClassifier.py 1.3 => 1.4 ===
--- Products/AdaptableStorage/serial_ofs/MetaTypeClassifier.py:1.3	Thu Dec  5 12:38:47 2002
+++ Products/AdaptableStorage/serial_ofs/MetaTypeClassifier.py	Fri Dec  6 17:06:50 2002
@@ -29,10 +29,12 @@
 
     __implements__ = IClassifier
 
-    def __init__(self):
+    def __init__(self, gw):
+        self.gw = gw
+        self.key_to_res = {}
         self.ext_to_mt = {}
+        self.fmt_to_mt = {}
         self.mt_to_mapper = {}
-        self.fmt_to_mapper = {}
 
     def register(self, meta_type, mapper_name, extensions):
         for ext in extensions:
@@ -43,7 +45,11 @@
         self.mt_to_mapper[meta_type] = mapper_name
 
 
-    def registerNodeTypeDefault(self, meta_type, mapper_name, isdir):
+    def registerKey(self, meta_type, mapper_name, key):
+        self.key_to_res[key] = (meta_type, mapper_name)
+
+
+    def registerDefaultLoader(self, meta_type, mapper_name, isdir):
         if isdir:
             ext = '<directory>'
         else:
@@ -52,47 +58,63 @@
         self.mt_to_mapper[meta_type] = mapper_name
 
 
-    def registerDefaultFormat(self, mapper_name, folderish):
+    def registerDefaultStorage(self, meta_type, mapper_name, folderish):
         if folderish:
             fmt = 'folder'
         else:
             fmt = 'file'
-        self.fmt_to_mapper[fmt] = mapper_name
+        self.fmt_to_mt[fmt] = meta_type
+        self.mt_to_mapper[meta_type] = mapper_name
 
 
-    def classifyObject(self, value):
+    def classifyObject(self, keychain, value):
+        res = self.key_to_res.get(keychain[-1])
+        if res is not None:
+            return res
         mt = value.meta_type
         mapper_name = self.mt_to_mapper.get(mt)
-        classification = {'meta_type': mt}
         if mapper_name is None:
             folderish = not not getattr(
                 aq_base(value), 'isPrincipiaFolderish', None)
-            # Use a "stored_format" property, which enables
-            # storage of any kind of object.
+            # Store in a default format
             if folderish:
                 fmt = 'folder'
             else:
                 fmt = 'file'
-            mapper_name = self.fmt_to_mapper.get(fmt)
+            mt = self.fmt_to_mt.get(fmt)
+            if mt is None:
+                raise SerializationError(
+                    'No classification known for %s' % repr(keychain))
+            mapper_name = self.mt_to_mapper.get(mt)
             if mapper_name is None:
                 raise DeserializationError(
                     'No mapper known for meta_type %s' % repr(mt))
-            classification['stored_format'] = fmt
-        return classification, mapper_name
-
+        return {'meta_type': mt}, mapper_name
 
-    def classifyFilename(self, filename, isdir):
-        """Guess a classification based on filename.
 
-        To be used only if the classification is not stored.
-        """
-        if isdir:
-            mt = self.ext_to_mt.get('<directory>', 'Folder')
-        else:
-            name, ext = os.path.splitext(filename)
-            mt = self.ext_to_mt.get(ext.lower())
-            if not mt:
-                mt = self.ext_to_mt.get('<file>', 'File')
+    def classifyState(self, mapper, keychain):
+        res = self.key_to_res.get(keychain[-1])
+        if res is not None:
+            return res
+        info, serial = self.gw.load(mapper, keychain)
+        classification = info[0][0]
+        mt = classification.get('meta_type')
+        if mt is None:
+            t = classification.get('node_type')
+            if t == 'd':
+                # Directory
+                mt = self.ext_to_mt.get('<directory>', 'Folder')
+            elif t == 'd':
+                # Some other kind of filesystem node; assume file
+                filename = classification.get('filename')
+                if filename:
+                    name, ext = os.path.splitext(filename)
+                    mt = self.ext_to_mt.get(ext.lower())
+                    if not mt:
+                        mt = self.ext_to_mt.get('<file>', 'File')
+            else:
+                raise DeserializationError(
+                    'No classification known for %s' % repr(keychain))
         mapper_name = self.mt_to_mapper.get(mt)
         if mapper_name is None:
             raise DeserializationError(
@@ -100,18 +122,6 @@
         return {'meta_type': mt}, mapper_name
 
 
-    def chooseMapper(self, classification):
-        fmt = classification.get('stored_format')
-        if fmt is not None:
-            mapper_name = self.fmt_to_mapper.get(fmt)
-            if mapper_name is None:
-                raise DeserializationError(
-                    'No mapper known for stored_format %s' % repr(fmt))
-        else:
-            mt = classification['meta_type']
-            mapper_name = self.mt_to_mapper.get(mt)
-            if mapper_name is None:
-                raise DeserializationError(
-                    'No mapper known for meta_type %s' % repr(mt))
-        return mapper_name
+    def store(self, mapper, keychain, classification):
+        return self.gw.store(mapper, keychain, ((classification,),))