[Zope-CVS] CVS: Products/AdaptableStorage/serial_ofs - FolderItems.py:1.2 MetaTypeClassifier.py:1.2
Shane Hathaway
shane@zope.com
Wed, 4 Dec 2002 23:18:39 -0500
Update of /cvs-repository/Products/AdaptableStorage/serial_ofs
In directory cvs.zope.org:/tmp/cvs-serv26322/serial_ofs
Modified Files:
FolderItems.py MetaTypeClassifier.py
Log Message:
- Switched to use of the "remaining state" aspect, enabling storage of
any extra data found on ZODB objects. Fixed bugs related to this.
- Made sure Zope 2 objects store their classification.
- Modified the serialization event interface a little to allow more
control over automatic references. This needs work still.
- MetaTypeClassifier is now also an IAspectSerializer, making it
possible to delegate classification details to the classifier.
=== Products/AdaptableStorage/serial_ofs/FolderItems.py 1.1 => 1.2 ===
--- Products/AdaptableStorage/serial_ofs/FolderItems.py:1.1 Wed Nov 27 13:37:07 2002
+++ Products/AdaptableStorage/serial_ofs/FolderItems.py Wed Dec 4 23:18:08 2002
@@ -40,10 +40,15 @@
state = []
event.ignoreAttribute('_objects')
for id, subob in object.objectItems():
- classification, mapper_name = event.classifyObject(subob)
- event.notifySerializedRef(
- id, aq_base(subob), attribute=1,
- mapper_name=mapper_name, stored_key=0)
+ 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))
return state
=== Products/AdaptableStorage/serial_ofs/MetaTypeClassifier.py 1.1 => 1.2 ===
--- Products/AdaptableStorage/serial_ofs/MetaTypeClassifier.py:1.1 Wed Nov 27 13:37:07 2002
+++ Products/AdaptableStorage/serial_ofs/MetaTypeClassifier.py Wed Dec 4 23:18:08 2002
@@ -20,16 +20,29 @@
from Acquisition import aq_base
-from serial_public import IClassifier, DeserializationError
+from serial_public import IClassifier, IAspectSerializer, \
+ DeserializationError, RecordSchema
class MetaTypeClassifier:
+ """A classifier based on meta_type attributes.
- __implements__ = IClassifier
+ Note that this class also implements IAspectSerializer, enabling
+ delegation from ClassificationAspects.
+ """
+
+ __implements__ = IClassifier, IAspectSerializer
+
+ schema = RecordSchema()
+ schema.addColumn('classification', 'classification')
def __init__(self):
self.ext_to_mt = {}
self.mt_to_mapper = {}
+ self.fmt_to_mapper = {}
+
+ def getSchema(self):
+ return self.schema
def register(self, meta_type, mapper_name, extensions):
for ext in extensions:
@@ -49,42 +62,60 @@
self.mt_to_mapper[meta_type] = mapper_name
- def registerObjectTypeDefault(self, mapper_name, folderish):
+ def registerDefaultFormat(self, mapper_name, folderish):
if folderish:
- mt = '(folderish object)'
+ fmt = 'folder'
else:
- mt = '(non-folderish object)'
- self.mt_to_mapper[mt] = mapper_name
+ fmt = 'file'
+ self.fmt_to_mapper[fmt] = mapper_name
+
+
+ def serialize(self, object, event):
+ c, mapper_name = self.classifyObject(object)
+ event.notifySerialized('meta_type', object.meta_type, 1)
+ return ((c,),)
+
+
+ def deserialize(self, object, event, state):
+ mt = state[0][0]['meta_type']
+ if object.meta_type != mt:
+ object.meta_type = mt
+ event.notifyDeserialized('meta_type', mt)
def classifyObject(self, value):
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')
- # Use a special meta_type.
+ folderish = not not getattr(
+ aq_base(value), 'isPrincipiaFolderish', None)
+ # Use a "stored_format" property, which enables
+ # storage of any kind of object.
if folderish:
- smt = '(folderish object)'
+ fmt = 'folder'
else:
- smt = '(non-folderish object)'
- mapper_name = self.mt_to_mapper.get(smt)
+ fmt = 'file'
+ mapper_name = self.fmt_to_mapper.get(fmt)
if mapper_name is None:
raise DeserializationError(
'No mapper known for meta_type %s' % repr(mt))
- mt = smt
- return {'meta_type': mt}, mapper_name
+ classification['stored_format'] = fmt
+ return classification, 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>')
- if not mt:
- mt = 'File'
+ mt = self.ext_to_mt.get('<file>', 'File')
mapper_name = self.mt_to_mapper.get(mt)
if mapper_name is None:
raise DeserializationError(
@@ -93,24 +124,17 @@
def chooseMapper(self, classification):
- 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))
+ 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
-
-
-
-# Old cruft:
-
-meta_types = {
- '<directory>': 'Folder',
- '<file>': 'File',
- '.html': 'Page Template',
- '.dtml': 'DTML Method',
- '.gif': 'Image',
- '.jpg': 'Image',
- '.jpeg': 'Image',
- }