[Twisted-Python] twisted.web.static.loadMimeTypes() patch

Alexander Winston alexander.winston at attbi.com
Sun Jun 8 19:32:50 MDT 2003


This patch is for twisted.web.static.loadMimeTypes().  It reads Python's built-in dictionary and updates its with a dictionary of our own (aka The Usual Suspects).  Support was added for reading/sourcing of an external file (the default being "/etc/mime.types"), which would update the aforementioned dictionary with the contents of the file.  The patch is attached to this E-mail.

----
Alexander Winston (Yeah.) <alexander.winston at attbi.com>
-------------- next part --------------
Index: Twisted/twisted/web/static.py
===================================================================
RCS file: /cvs/Twisted/twisted/web/static.py,v
retrieving revision 1.92
diff -u -r1.92 static.py
--- Twisted/twisted/web/static.py	31 May 2003 05:42:42 -0000	1.92
+++ Twisted/twisted/web/static.py	8 Jun 2003 20:41:27 -0000
@@ -147,43 +147,48 @@
                           app.theApplication)
 
 
-def loadMimeTypes():
-    """Ugg, does this even need to exist anymore?  Stupid stdlib"""
+def loadMimeTypes(mimetypes_location='/etc/mime.types'):
+    '''Yay for mime(-type)s!'''
     import mimetypes
-    # let's try a few of the usual suspects...
-    contentTypes = {
-        ".css": "text/css",
-        ".exe": "application/x-executable",
-        ".flac": "audio/x-flac",
-        ".gif": "image/gif",
-        ".gtar": "application/x-gtar",
-        ".html": "text/html",
-        ".htm": "text/html",
-        ".java": "text/plain",
-        ".jpeg": "image/jpeg",
-        ".jpg": "image/jpeg",
-        ".lisp": "text/x-lisp",
-        ".mp3":  "audio/mpeg",
-        ".oz": "text/x-oz",
-        ".ogg": "application/x-ogg",
-        ".pdf": "application/x-pdf",
-        ".png": "image/png",
-        ".py": "text/plain",
-        ".swf": "application/x-shockwave-flash",
-        ".tar": "application/x-tar",
-        ".tgz": "application/x-gtar",
-        ".tif": "image/tiff",
-        ".tiff": "image/tiff",
-        ".txt": "text/plain",
-        ".wml": "text/vnd.wap.wml",
-        ".xul": "application/vnd.mozilla.xul+xml",
-        ".zip": "application/x-zip",
-        ".patch": "text/plain",
+    # Grab Python's built-in mimetypes dictionary.
+    contentTypes = mimetypes.types_map
+    # Update Python's dictionary with a few of our usual suspects.
+    contentTypes.update(
+        {
+            '.conf':  'text/plain',
+            '.css':   'text/css',
+            '.exe':   'application/x-executable',
+            '.flac':  'audio/x-flac',
+            '.gif':   'image/gif',
+            '.gtar':  'application/x-gtar',
+            '.html':  'text/html',
+            '.htm':   'text/html',
+            '.java':  'text/plain',
+            '.jpeg':  'image/jpeg',
+            '.jpg':   'image/jpeg',
+            '.lisp':  'text/x-lisp',
+            '.mp3':   'audio/mpeg',
+            '.ogg':   'application/ogg',
+            '.oz':    'text/x-oz',
+            '.patch': 'text/plain',
+            '.pdf':   'application/x-pdf',
+            '.png':   'image/png',
+            '.py':    'text/plain',
+            '.swf':   'application/x-shockwave-flash',
+            '.tar':   'application/x-tar',
+            '.tgz':   'application/x-gtar',
+            '.tif':   'image/tiff',
+            '.tiff':  'image/tiff',
+            '.txt':   'text/plain',
+            '.wml':   'text/vnd.wap.wml',
+            '.xul':   'application/vnd.mozilla.xul+xml',
+            '.zip':   'application/x-zip',
         }
-    upd = contentTypes.update
-    if os.path.exists("/etc/mime.types"):
-        upd(mimetypes.read_mime_types("/etc/mime.types"))
-
+    )
+    # Users can override these mime-types by loading them out of a system-wide
+    # configuration file (this defaults to "/etc/mime.types").
+    if os.path.exists(mimetypes_location):
+        contentTypes.update(mimetypes.read_mime_types(mimetypes_location))
     return contentTypes
 
 


More information about the Twisted-Python mailing list