[Zope-CVS] CVS: Products/AdaptableStorage/serial_ofs - MetaTypeClassifier.py:1.14

Shane Hathaway shane@zope.com
Thu, 6 Feb 2003 12:55:57 -0500


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

Modified Files:
	MetaTypeClassifier.py 
Log Message:
Gave images and files the correct automatic extension according to the
mimetypes module.


=== Products/AdaptableStorage/serial_ofs/MetaTypeClassifier.py 1.13 => 1.14 ===
--- Products/AdaptableStorage/serial_ofs/MetaTypeClassifier.py:1.13	Wed Feb  5 13:17:25 2003
+++ Products/AdaptableStorage/serial_ofs/MetaTypeClassifier.py	Thu Feb  6 12:55:25 2003
@@ -16,6 +16,8 @@
 $Id$
 """
 
+from mimetypes import guess_extension
+
 from Acquisition import aq_base
 from OFS.ObjectManager import ObjectManager
 
@@ -25,9 +27,12 @@
 class MetaTypeClassifier:
     """A classifier based on meta_type attributes.
     """
-
     __implements__ = IClassifier
 
+    # flag values (constant)
+    CONTENT_TYPE_ATTR = 1
+
+
     def __init__(self, gw=None):
         self.gw = gw
         self.key_to_res = {}
@@ -35,6 +40,8 @@
         self.mt_to_ext = {}
         self.fmt_to_mt = {}
         self.mt_to_mapper = {}
+        self.flags = {}
+
 
     def setGateway(self, gw):
         self.gw = gw
@@ -81,6 +88,11 @@
         self.mt_to_mapper[meta_type] = mapper_name
 
 
+    def setFlags(self, mapper_name, flags):
+        """Sets flags associated with a certain mapper."""
+        self.flags[mapper_name] = flags
+
+
     def classifyObject(self, value, keychain):
         """Chooses a mapper and classification for storing an object."""
         res = self.key_to_res.get(keychain[-1])
@@ -106,9 +118,17 @@
         klass = value.__class__
         ci = '%s:%s' % (klass.__module__, klass.__name__)
         classification = {'meta_type': mt, 'class_name': ci}
-        ext = self.mt_to_ext.get(mt)
+
+        flags = self.flags.get(mapper_name, 0)
+        if flags and (flags & MetaTypeClassifier.CONTENT_TYPE_ATTR):
+            ct = str(getattr(aq_base(value), 'content_type', None))
+            if ct:
+                ext = guess_extension(ct, strict=0)
+        else:
+            ext = self.mt_to_ext.get(mt)
         if ext:
             classification['extension'] = ext
+
         return classification, mapper_name