[Zope-Checkins] CVS: Zope/lib/python/OFS - mime.types:1.1 content_types.py:1.20

Fred Drake cvs-admin at zope.org
Thu Oct 30 11:43:08 EST 2003


Update of /cvs-repository/Zope/lib/python/OFS
In directory cvs.zope.org:/tmp/cvs-serv1862/lib/python/OFS

Modified Files:
	content_types.py 
Added Files:
	mime.types 
Log Message:
Add a "mime-types" configuration value which names a file giving
additional MIME type to filename extension mappings.  The "mime-types"
setting may be given more than once in the configuration file; the
files have the same format at the mime.types file distributed with
Apache.

Not sure if this should be ported to the Zope 2.7 branch.


=== Added File Zope/lib/python/OFS/mime.types ===
application-x-cdf               cdf
application/msword              doc dot wiz
application/pdf                 pdf
application/pkcs7-mime          p7c
application/vnd.mozilla.xul+xml xul
application/vnd.ms-excel        xlb xls
application/vnd.ms-powerpoint   pot ppa pps ppt pwz
application/x-javascript        js
application/x-pkcs12            p12 pfx
application/x-pn-realaudio      ram
audio/mpeg                      mp3
audio/x-pn-realaudio            ra
image/pict                      pct pic pict
message/rfc822                  eml nws mht mhtml
text/css                        css
text/plain                      bat c h pl ksh
text/x-vcard                    vcf
text/xml                        xml xsl
video/mpeg                      m1v mpa


=== Zope/lib/python/OFS/content_types.py 1.19 => 1.20 ===
--- Zope/lib/python/OFS/content_types.py:1.19	Sun Sep 14 08:40:21 2003
+++ Zope/lib/python/OFS/content_types.py	Thu Oct 30 11:43:07 2003
@@ -13,10 +13,12 @@
 """A utility module for content-type handling."""
 __version__='$Revision$'[11:-2]
 
-import re, mimetypes
+import mimetypes
+import os.path
+import re
 
 
-find_binary=re.compile('[\0-\7]').search
+find_binary = re.compile('[\0-\7]').search
 
 def text_type(s):
     # Yuk. See if we can figure out the type by content.
@@ -30,70 +32,43 @@
         return 'text/plain'
 
 
-
-# This gives us a hook to add content types that
-# aren't currently listed in the mimetypes module.
-_addtypes=(
-    ('.mp3', 'audio/mpeg'),
-    ('.ra', 'audio/x-pn-realaudio'),
-    ('.pdf', 'application/pdf'),
-    ('.c', 'text/plain'),
-    ('.bat', 'text/plain'),
-    ('.h', 'text/plain'),
-    ('.pl', 'text/plain'),
-    ('.ksh', 'text/plain'),
-    ('.ram', 'application/x-pn-realaudio'),
-    ('.cdf', 'application-x-cdf'),
-    ('.doc', 'application/msword'),
-    ('.dot', 'application/msword'),
-    ('.wiz', 'application/msword'),
-    ('.xlb', 'application/vnd.ms-excel'),
-    ('.xls', 'application/vnd.ms-excel'),
-    ('.ppa', 'application/vnd.ms-powerpoint'),
-    ('.ppt', 'application/vnd.ms-powerpoint'),
-    ('.pps', 'application/vnd.ms-powerpoint'),
-    ('.pot', 'application/vnd.ms-powerpoint'),
-    ('.pwz', 'application/vnd.ms-powerpoint'),
-    ('.eml',   'message/rfc822'),
-    ('.nws',   'message/rfc822'),
-    ('.mht',   'message/rfc822'),
-    ('.mhtml', 'message/rfc822'),
-    ('.css', 'text/css'),
-    ('.p7c', 'application/pkcs7-mime'),
-    ('.p12', 'application/x-pkcs12'),
-    ('.pfx', 'application/x-pkcs12'),
-    ('.js',  'application/x-javascript'),
-    ('.pct', 'image/pict'),
-    ('.pic', 'image/pict'),
-    ('.pict', 'image/pict'),
-    ('.m1v', 'video/mpeg'),
-    ('.mpa', 'video/mpeg'),
-    ('.vcf', 'text/x-vcard'),
-    ('.xml', 'text/xml'),
-    ('.xsl', 'text/xml'),
-    ('.xul', 'application/vnd.mozilla.xul+xml'),
-    )
-for name, val in _addtypes:
-    mimetypes.types_map[name]=val
-
 def guess_content_type(name='', body='', default=None):
     # Attempt to determine the content type (and possibly
     # content-encoding) based on an an object's name and
     # entity body.
-    type, enc=mimetypes.guess_type(name)
+    type, enc = mimetypes.guess_type(name)
     if type is None:
         if body:
             if find_binary(body) is not None:
-                type=default or 'application/octet-stream'
+                type = default or 'application/octet-stream'
             else:
-                type=(default or text_type(body)
-                      or 'text/x-unknown-content-type')
+                type = (default or text_type(body)
+                        or 'text/x-unknown-content-type')
         else:
-            type=default or 'text/x-unknown-content-type'
+            type = default or 'text/x-unknown-content-type'
 
     return type.lower(), enc and enc.lower() or None
 
-if __name__=='__main__':
-    items=mimetypes.types_map.items()
+
+def add_files(filenames):
+    # Make sure the additional files are either loaded or scheduled to
+    # be loaded:
+    if mimetypes.inited:
+        # Re-initialize the mimetypes module, loading additional files
+        mimetypes.init(filenames)
+    else:
+        # Tell the mimetypes module about the additional files so
+        # when it is initialized, it will pick up all of them, in
+        # the right order.
+        mimetypes.knownfiles.extend(filenames)
+
+
+# Provide definitions shipped as part of Zope:
+here = os.path.dirname(os.path.abspath(__file__))
+add_files([os.path.join(here, "mime.types")])
+
+
+if __name__ == '__main__':
+    items = mimetypes.types_map.items()
     items.sort()
     for item in items: print "%s:\t%s" % item




More information about the Zope-Checkins mailing list