[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/content_types - ported OFS.content_types from Z2 to Z3 (which is the most recent maintained code)

Andreas Jung andreas at andreas-jung.com
Sun Dec 11 03:37:55 EST 2005


Log message for revision 40683:
  - ported OFS.content_types from Z2 to Z3 (which is the most recent maintained code)
  - fixed tests (which obviously never worked since mimetypes uses a lazy initialization mechanism)
  - content_types is now a package and no longer a simple module
  
  

Changed:
  A   Zope3/trunk/src/zope/app/content_types/
  A   Zope3/trunk/src/zope/app/content_types/__init__.py
  A   Zope3/trunk/src/zope/app/content_types/mime.types
  A   Zope3/trunk/src/zope/app/content_types/tests/
  A   Zope3/trunk/src/zope/app/content_types/tests/__init__.py
  A   Zope3/trunk/src/zope/app/content_types/tests/mime.types-1
  A   Zope3/trunk/src/zope/app/content_types/tests/mime.types-2
  A   Zope3/trunk/src/zope/app/content_types/tests/testContentTypes.py
  A   Zope3/trunk/src/zope/app/content_types/tests/testContentTypes.py.new
  D   Zope3/trunk/src/zope/app/content_types.py

-=-
Added: Zope3/trunk/src/zope/app/content_types/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/content_types/__init__.py	2005-12-10 19:05:51 UTC (rev 40682)
+++ Zope3/trunk/src/zope/app/content_types/__init__.py	2005-12-11 08:37:54 UTC (rev 40683)
@@ -0,0 +1,78 @@
+##############################################################################
+#
+# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE
+#
+##############################################################################
+
+"""A utility module for content-type handling.
+
+$Id: content_types.py 24764 2004-05-17 06:13:48Z philikon $
+"""
+
+import re
+import os.path
+import mimetypes
+
+
+find_binary = re.compile('[\0-\7]').search
+
+def text_type(s):
+    s = s.strip()
+
+    # Yuk. See if we can figure out the type by content.
+    if s.lower().startswith('<html>') or '</' in s:
+        return 'text/html'
+
+    elif s.startswith('<?xml'):
+        return 'text/xml'
+
+    else:
+        return 'text/plain'
+
+
+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)
+    if type is None:
+        if body:
+            if find_binary(body) is not None:
+                type = default or 'application/octet-stream'
+            else:
+                type = (default or text_type(body)
+                        or 'text/x-unknown-content-type')
+        else:
+            type = default or 'text/x-unknown-content-type'
+
+    return type.lower(), enc and enc.lower() or None
+
+
+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

Added: Zope3/trunk/src/zope/app/content_types/mime.types
===================================================================
--- Zope3/trunk/src/zope/app/content_types/mime.types	2005-12-10 19:05:51 UTC (rev 40682)
+++ Zope3/trunk/src/zope/app/content_types/mime.types	2005-12-11 08:37:54 UTC (rev 40683)
@@ -0,0 +1,19 @@
+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

Added: Zope3/trunk/src/zope/app/content_types/tests/__init__.py
===================================================================

Added: Zope3/trunk/src/zope/app/content_types/tests/mime.types-1
===================================================================
--- Zope3/trunk/src/zope/app/content_types/tests/mime.types-1	2005-12-10 19:05:51 UTC (rev 40682)
+++ Zope3/trunk/src/zope/app/content_types/tests/mime.types-1	2005-12-11 08:37:54 UTC (rev 40683)
@@ -0,0 +1,5 @@
+# This is a sample mime.types file.
+# It contains a single bogus MIME type and extention for testing
+# purposes.  It is not loaded during normal Zope operation.
+
+text/x-vnd.zope.test-mime-type-1  ztmt-1

Added: Zope3/trunk/src/zope/app/content_types/tests/mime.types-2
===================================================================
--- Zope3/trunk/src/zope/app/content_types/tests/mime.types-2	2005-12-10 19:05:51 UTC (rev 40682)
+++ Zope3/trunk/src/zope/app/content_types/tests/mime.types-2	2005-12-11 08:37:54 UTC (rev 40683)
@@ -0,0 +1,5 @@
+# This is a sample mime.types file.
+# It contains a single bogus MIME type and extention for testing
+# purposes.  It is not loaded during normal Zope operation.
+
+text/x-vnd.zope.test-mime-type-2  ztmt-2

Added: Zope3/trunk/src/zope/app/content_types/tests/testContentTypes.py
===================================================================
--- Zope3/trunk/src/zope/app/content_types/tests/testContentTypes.py	2005-12-10 19:05:51 UTC (rev 40682)
+++ Zope3/trunk/src/zope/app/content_types/tests/testContentTypes.py	2005-12-11 08:37:54 UTC (rev 40683)
@@ -0,0 +1,77 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Tests of the content_types extension mechanism.
+
+$Id: testContentTypes.py 24763 2004-05-17 05:59:28Z philikon $
+"""
+
+import mimetypes
+import os.path
+import sys
+import unittest
+
+from zope.app import content_types
+
+try:
+    __file__
+except NameError:
+    __file__ = os.path.realpath(sys.argv[0])
+
+here = os.path.dirname(os.path.abspath(__file__))
+MIME_TYPES_1 = os.path.join(here, "mime.types-1")
+MIME_TYPES_2 = os.path.join(here, "mime.types-2")
+
+class ContentTypesTestCase(unittest.TestCase):
+
+    def setUp(self):
+        mimetypes.init()
+        self._old_state = mimetypes.__dict__.copy()
+
+    def tearDown(self):
+        mimetypes.__dict__.update(self._old_state)
+
+    def check_types_count(self, delta):
+        self.assertEqual(len(mimetypes.types_map),
+                         len(self._old_state["types_map"]) + delta)
+
+    
+
+    def test_add_one_file(self):
+        ntypes = len(mimetypes.types_map)
+        content_types.add_files([MIME_TYPES_1])
+        ctype, encoding = content_types.guess_content_type("foo.ztmt-1")
+        self.assert_(encoding is None)
+        self.assertEqual(ctype, "text/x-vnd.zope.test-mime-type-1")
+        ctype, encoding = content_types.guess_content_type("foo.ztmt-1.gz")
+        self.assertEqual(encoding, "gzip")
+        self.assertEqual(ctype, "text/x-vnd.zope.test-mime-type-1")
+        self.check_types_count(1)
+
+    def test_add_two_files(self):
+        ntypes = len(mimetypes.types_map)
+        content_types.add_files([MIME_TYPES_1, MIME_TYPES_2])
+        ctype, encoding = content_types.guess_content_type("foo.ztmt-1")
+        self.assert_(encoding is None)
+        self.assertEqual(ctype, "text/x-vnd.zope.test-mime-type-1")
+        ctype, encoding = content_types.guess_content_type("foo.ztmt-2")
+        self.assert_(encoding is None)
+        self.assertEqual(ctype, "text/x-vnd.zope.test-mime-type-2")
+        self.check_types_count(2)
+
+
+def test_suite():
+    return unittest.makeSuite(ContentTypesTestCase)
+
+if __name__ == '__main__':
+    unittest.main()

Added: Zope3/trunk/src/zope/app/content_types/tests/testContentTypes.py.new
===================================================================

Deleted: Zope3/trunk/src/zope/app/content_types.py
===================================================================
--- Zope3/trunk/src/zope/app/content_types.py	2005-12-10 19:05:51 UTC (rev 40682)
+++ Zope3/trunk/src/zope/app/content_types.py	2005-12-11 08:37:54 UTC (rev 40683)
@@ -1,102 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE
-#
-##############################################################################
-"""A utility module for content-type handling.
-
-$Id$
-"""
-import re, mimetypes
-
-find_binary = re.compile('[\0-\7]').search
-
-def text_type(s):
-    # Yuk. See if we can figure out the type by content.
-    s = s.strip()
-    if (s[:6].lower() == '<html>' or s.find('</') > 0):
-        return 'text/html'
-
-    elif s.startswith('<?xml'):
-        return 'text/xml'
-
-    else:
-        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'),
-    ('.wsdl', 'application/xml'),  # Python 2.5
-    ('.xml', 'text/xml'),
-    ('.xpdl', 'application/xml'),  # Python 2.5
-    ('.xsl', 'text/xml'),
-    ('.xul', 'application/vnd.mozilla.xul+xml'),
-    ('.swf', 'application/x-shockwave-flash'),
-    ('.csv', 'text/csv'),
-    )
-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)
-    if type is None:
-        if body:
-            if find_binary(body) is not None:
-                type = default or 'application/octet-stream'
-            else:
-                type = default or text_type(body)
-        else:
-            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()
-    items.sort()
-    for item in items:
-        print "%s:\t%s" % item



More information about the Zope3-Checkins mailing list