Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 1 | """Guess the MIME type of a file. |
| 2 | |
Fred Drake | 5109ffd | 1998-05-18 16:27:20 +0000 | [diff] [blame] | 3 | This module defines two useful functions: |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 4 | |
| 5 | guess_type(url) -- guess the MIME type and encoding of a URL. |
| 6 | |
Fred Drake | 5109ffd | 1998-05-18 16:27:20 +0000 | [diff] [blame] | 7 | guess_extension(type) -- guess the extension for a given MIME type. |
| 8 | |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 9 | It also contains the following, for tuning the behavior: |
| 10 | |
| 11 | Data: |
| 12 | |
| 13 | knownfiles -- list of files to parse |
| 14 | inited -- flag set when init() has been called |
| 15 | suffixes_map -- dictionary mapping suffixes to suffixes |
| 16 | encodings_map -- dictionary mapping suffixes to encodings |
| 17 | types_map -- dictionary mapping suffixes to types |
| 18 | |
| 19 | Functions: |
| 20 | |
| 21 | init([files]) -- parse a list of files, default knownfiles |
| 22 | read_mime_types(file) -- parse one file, return a dictionary or None |
| 23 | |
| 24 | """ |
| 25 | |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 26 | import posixpath |
Guido van Rossum | 1c5fb1c | 1998-10-12 15:12:28 +0000 | [diff] [blame] | 27 | import urllib |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 28 | |
Skip Montanaro | 03d9014 | 2001-01-25 15:29:22 +0000 | [diff] [blame] | 29 | __all__ = ["guess_type","guess_extension","read_mime_types","init"] |
| 30 | |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 31 | knownfiles = [ |
| 32 | "/usr/local/etc/httpd/conf/mime.types", |
| 33 | "/usr/local/lib/netscape/mime.types", |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 34 | "/usr/local/etc/httpd/conf/mime.types", # Apache 1.2 |
| 35 | "/usr/local/etc/mime.types", # Apache 1.3 |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 36 | ] |
| 37 | |
| 38 | inited = 0 |
| 39 | |
| 40 | def guess_type(url): |
| 41 | """Guess the type of a file based on its URL. |
| 42 | |
| 43 | Return value is a tuple (type, encoding) where type is None if the |
| 44 | type can't be guessed (no or unknown suffix) or a string of the |
| 45 | form type/subtype, usable for a MIME Content-type header; and |
| 46 | encoding is None for no encoding or the name of the program used |
| 47 | to encode (e.g. compress or gzip). The mappings are table |
| 48 | driven. Encoding suffixes are case sensitive; type suffixes are |
| 49 | first tried case sensitive, then case insensitive. |
| 50 | |
| 51 | The suffixes .tgz, .taz and .tz (case sensitive!) are all mapped |
| 52 | to ".tar.gz". (This is table-driven too, using the dictionary |
Fred Drake | 3130b7a | 1998-05-18 16:05:24 +0000 | [diff] [blame] | 53 | suffix_map). |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 54 | |
| 55 | """ |
| 56 | if not inited: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 57 | init() |
Guido van Rossum | 1c5fb1c | 1998-10-12 15:12:28 +0000 | [diff] [blame] | 58 | scheme, url = urllib.splittype(url) |
| 59 | if scheme == 'data': |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 60 | # syntax of data URLs: |
| 61 | # dataurl := "data:" [ mediatype ] [ ";base64" ] "," data |
| 62 | # mediatype := [ type "/" subtype ] *( ";" parameter ) |
| 63 | # data := *urlchar |
| 64 | # parameter := attribute "=" value |
| 65 | # type/subtype defaults to "text/plain" |
Eric S. Raymond | 51cc3bc | 2001-02-09 09:44:47 +0000 | [diff] [blame] | 66 | comma = url.find(',') |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 67 | if comma < 0: |
| 68 | # bad data URL |
| 69 | return None, None |
Eric S. Raymond | 51cc3bc | 2001-02-09 09:44:47 +0000 | [diff] [blame] | 70 | semi = url.find(';', 0, comma) |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 71 | if semi >= 0: |
| 72 | type = url[:semi] |
| 73 | else: |
| 74 | type = url[:comma] |
| 75 | if '=' in type or '/' not in type: |
| 76 | type = 'text/plain' |
| 77 | return type, None # never compressed, so encoding is None |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 78 | base, ext = posixpath.splitext(url) |
| 79 | while suffix_map.has_key(ext): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 80 | base, ext = posixpath.splitext(base + suffix_map[ext]) |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 81 | if encodings_map.has_key(ext): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 82 | encoding = encodings_map[ext] |
| 83 | base, ext = posixpath.splitext(base) |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 84 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 85 | encoding = None |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 86 | if types_map.has_key(ext): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 87 | return types_map[ext], encoding |
Eric S. Raymond | 51cc3bc | 2001-02-09 09:44:47 +0000 | [diff] [blame] | 88 | elif types_map.has_key(ext.lower()): |
| 89 | return types_map[ext.lower()], encoding |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 90 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 91 | return None, encoding |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 92 | |
Fred Drake | 5109ffd | 1998-05-18 16:27:20 +0000 | [diff] [blame] | 93 | def guess_extension(type): |
| 94 | """Guess the extension for a file based on its MIME type. |
| 95 | |
| 96 | Return value is a string giving a filename extension, including the |
| 97 | leading dot ('.'). The extension is not guaranteed to have been |
Fred Drake | 4941341 | 1998-05-19 15:15:59 +0000 | [diff] [blame] | 98 | associated with any particular data stream, but would be mapped to the |
| 99 | MIME type `type' by guess_type(). If no extension can be guessed for |
| 100 | `type', None is returned. |
Fred Drake | 5109ffd | 1998-05-18 16:27:20 +0000 | [diff] [blame] | 101 | """ |
Fred Drake | 4941341 | 1998-05-19 15:15:59 +0000 | [diff] [blame] | 102 | global inited |
| 103 | if not inited: |
| 104 | init() |
Eric S. Raymond | 51cc3bc | 2001-02-09 09:44:47 +0000 | [diff] [blame] | 105 | type = type.lower() |
Fred Drake | 5109ffd | 1998-05-18 16:27:20 +0000 | [diff] [blame] | 106 | for ext, stype in types_map.items(): |
| 107 | if type == stype: |
| 108 | return ext |
| 109 | return None |
| 110 | |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 111 | def init(files=None): |
| 112 | global inited |
| 113 | for file in files or knownfiles: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 114 | s = read_mime_types(file) |
| 115 | if s: |
| 116 | for key, value in s.items(): |
| 117 | types_map[key] = value |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 118 | inited = 1 |
| 119 | |
| 120 | def read_mime_types(file): |
| 121 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 122 | f = open(file) |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 123 | except IOError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 124 | return None |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 125 | map = {} |
| 126 | while 1: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 127 | line = f.readline() |
| 128 | if not line: break |
Eric S. Raymond | 51cc3bc | 2001-02-09 09:44:47 +0000 | [diff] [blame] | 129 | words = line.split() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 130 | for i in range(len(words)): |
| 131 | if words[i][0] == '#': |
| 132 | del words[i:] |
| 133 | break |
| 134 | if not words: continue |
| 135 | type, suffixes = words[0], words[1:] |
| 136 | for suff in suffixes: |
| 137 | map['.'+suff] = type |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 138 | f.close() |
| 139 | return map |
| 140 | |
| 141 | suffix_map = { |
| 142 | '.tgz': '.tar.gz', |
| 143 | '.taz': '.tar.gz', |
| 144 | '.tz': '.tar.gz', |
| 145 | } |
| 146 | |
| 147 | encodings_map = { |
| 148 | '.gz': 'gzip', |
| 149 | '.Z': 'compress', |
| 150 | } |
| 151 | |
| 152 | types_map = { |
| 153 | '.a': 'application/octet-stream', |
| 154 | '.ai': 'application/postscript', |
| 155 | '.aif': 'audio/x-aiff', |
| 156 | '.aifc': 'audio/x-aiff', |
| 157 | '.aiff': 'audio/x-aiff', |
| 158 | '.au': 'audio/basic', |
| 159 | '.avi': 'video/x-msvideo', |
| 160 | '.bcpio': 'application/x-bcpio', |
| 161 | '.bin': 'application/octet-stream', |
| 162 | '.cdf': 'application/x-netcdf', |
| 163 | '.cpio': 'application/x-cpio', |
| 164 | '.csh': 'application/x-csh', |
| 165 | '.dll': 'application/octet-stream', |
| 166 | '.dvi': 'application/x-dvi', |
| 167 | '.exe': 'application/octet-stream', |
| 168 | '.eps': 'application/postscript', |
| 169 | '.etx': 'text/x-setext', |
| 170 | '.gif': 'image/gif', |
| 171 | '.gtar': 'application/x-gtar', |
| 172 | '.hdf': 'application/x-hdf', |
| 173 | '.htm': 'text/html', |
| 174 | '.html': 'text/html', |
| 175 | '.ief': 'image/ief', |
| 176 | '.jpe': 'image/jpeg', |
| 177 | '.jpeg': 'image/jpeg', |
| 178 | '.jpg': 'image/jpeg', |
Fred Drake | c40c547 | 1999-05-20 12:52:04 +0000 | [diff] [blame] | 179 | '.js': 'application/x-javascript', |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 180 | '.latex': 'application/x-latex', |
| 181 | '.man': 'application/x-troff-man', |
| 182 | '.me': 'application/x-troff-me', |
| 183 | '.mif': 'application/x-mif', |
| 184 | '.mov': 'video/quicktime', |
| 185 | '.movie': 'video/x-sgi-movie', |
| 186 | '.mpe': 'video/mpeg', |
| 187 | '.mpeg': 'video/mpeg', |
| 188 | '.mpg': 'video/mpeg', |
| 189 | '.ms': 'application/x-troff-ms', |
| 190 | '.nc': 'application/x-netcdf', |
| 191 | '.o': 'application/octet-stream', |
| 192 | '.obj': 'application/octet-stream', |
| 193 | '.oda': 'application/oda', |
| 194 | '.pbm': 'image/x-portable-bitmap', |
| 195 | '.pdf': 'application/pdf', |
| 196 | '.pgm': 'image/x-portable-graymap', |
| 197 | '.pnm': 'image/x-portable-anymap', |
| 198 | '.png': 'image/png', |
| 199 | '.ppm': 'image/x-portable-pixmap', |
| 200 | '.py': 'text/x-python', |
| 201 | '.pyc': 'application/x-python-code', |
| 202 | '.ps': 'application/postscript', |
| 203 | '.qt': 'video/quicktime', |
| 204 | '.ras': 'image/x-cmu-raster', |
| 205 | '.rgb': 'image/x-rgb', |
Fred Drake | cbd9870 | 1999-03-11 16:04:04 +0000 | [diff] [blame] | 206 | '.rdf': 'application/xml', |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 207 | '.roff': 'application/x-troff', |
| 208 | '.rtf': 'application/rtf', |
| 209 | '.rtx': 'text/richtext', |
| 210 | '.sgm': 'text/x-sgml', |
| 211 | '.sgml': 'text/x-sgml', |
| 212 | '.sh': 'application/x-sh', |
| 213 | '.shar': 'application/x-shar', |
| 214 | '.snd': 'audio/basic', |
| 215 | '.so': 'application/octet-stream', |
| 216 | '.src': 'application/x-wais-source', |
| 217 | '.sv4cpio': 'application/x-sv4cpio', |
| 218 | '.sv4crc': 'application/x-sv4crc', |
| 219 | '.t': 'application/x-troff', |
| 220 | '.tar': 'application/x-tar', |
| 221 | '.tcl': 'application/x-tcl', |
| 222 | '.tex': 'application/x-tex', |
| 223 | '.texi': 'application/x-texinfo', |
| 224 | '.texinfo': 'application/x-texinfo', |
| 225 | '.tif': 'image/tiff', |
| 226 | '.tiff': 'image/tiff', |
| 227 | '.tr': 'application/x-troff', |
| 228 | '.tsv': 'text/tab-separated-values', |
| 229 | '.txt': 'text/plain', |
| 230 | '.ustar': 'application/x-ustar', |
| 231 | '.wav': 'audio/x-wav', |
| 232 | '.xbm': 'image/x-xbitmap', |
Guido van Rossum | 7beaad4 | 1998-05-18 14:25:08 +0000 | [diff] [blame] | 233 | '.xml': 'text/xml', |
Fred Drake | cbd9870 | 1999-03-11 16:04:04 +0000 | [diff] [blame] | 234 | '.xsl': 'application/xml', |
Guido van Rossum | ac8a9f3 | 1997-09-30 19:05:50 +0000 | [diff] [blame] | 235 | '.xpm': 'image/x-xpixmap', |
| 236 | '.xwd': 'image/x-xwindowdump', |
| 237 | '.zip': 'application/zip', |
| 238 | } |
Eric S. Raymond | 51cc3bc | 2001-02-09 09:44:47 +0000 | [diff] [blame] | 239 | |
| 240 | if __name__ == '__main__': |
| 241 | import sys |
| 242 | print guess_type(sys.argv[1]) |