blob: f0da453da05e8ea7cd5f915dc8edf31cf42bd627 [file] [log] [blame]
Guido van Rossumac8a9f31997-09-30 19:05:50 +00001"""Guess the MIME type of a file.
2
Fred Drake5109ffd1998-05-18 16:27:20 +00003This module defines two useful functions:
Guido van Rossumac8a9f31997-09-30 19:05:50 +00004
Georg Brandlcdf8b342009-06-08 09:07:34 +00005guess_type(url, strict=True) -- guess the MIME type and encoding of a URL.
Guido van Rossumac8a9f31997-09-30 19:05:50 +00006
Georg Brandlcdf8b342009-06-08 09:07:34 +00007guess_extension(type, strict=True) -- guess the extension for a given MIME type.
Fred Drake5109ffd1998-05-18 16:27:20 +00008
Guido van Rossumac8a9f31997-09-30 19:05:50 +00009It also contains the following, for tuning the behavior:
10
11Data:
12
13knownfiles -- list of files to parse
14inited -- flag set when init() has been called
Fred Drakeeeee4ec2001-08-03 21:01:44 +000015suffix_map -- dictionary mapping suffixes to suffixes
Guido van Rossumac8a9f31997-09-30 19:05:50 +000016encodings_map -- dictionary mapping suffixes to encodings
17types_map -- dictionary mapping suffixes to types
18
19Functions:
20
21init([files]) -- parse a list of files, default knownfiles
22read_mime_types(file) -- parse one file, return a dictionary or None
Guido van Rossumac8a9f31997-09-30 19:05:50 +000023"""
24
Fred Drakeeeee4ec2001-08-03 21:01:44 +000025import os
Guido van Rossumac8a9f31997-09-30 19:05:50 +000026import posixpath
Jeremy Hylton1afc1692008-06-18 20:49:58 +000027import urllib.parse
Guido van Rossumac8a9f31997-09-30 19:05:50 +000028
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +000029__all__ = [
30 "guess_type","guess_extension","guess_all_extensions",
31 "add_type","read_mime_types","init"
32]
Skip Montanaro03d90142001-01-25 15:29:22 +000033
Guido van Rossumac8a9f31997-09-30 19:05:50 +000034knownfiles = [
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +000035 "/etc/mime.types",
Thomas Wouters0e3f5912006-08-11 14:57:12 +000036 "/etc/httpd/mime.types", # Mac OS X
37 "/etc/httpd/conf/mime.types", # Apache
38 "/etc/apache/mime.types", # Apache 1
39 "/etc/apache2/mime.types", # Apache 2
Guido van Rossumac8a9f31997-09-30 19:05:50 +000040 "/usr/local/etc/httpd/conf/mime.types",
41 "/usr/local/lib/netscape/mime.types",
Fred Drake13a2c272000-02-10 17:17:14 +000042 "/usr/local/etc/httpd/conf/mime.types", # Apache 1.2
43 "/usr/local/etc/mime.types", # Apache 1.3
Guido van Rossumac8a9f31997-09-30 19:05:50 +000044 ]
45
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +000046inited = False
Antoine Pitrou57f3d932009-04-27 21:04:19 +000047_db = None
Guido van Rossumac8a9f31997-09-30 19:05:50 +000048
Fred Drakeeeee4ec2001-08-03 21:01:44 +000049
50class MimeTypes:
51 """MIME-types datastore.
52
53 This datastore can handle information from mime.types-style files
54 and supports basic determination of MIME type from a filename or
55 URL, and can guess a reasonable extension given a MIME type.
56 """
57
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +000058 def __init__(self, filenames=(), strict=True):
Fred Drakeeeee4ec2001-08-03 21:01:44 +000059 if not inited:
60 init()
61 self.encodings_map = encodings_map.copy()
62 self.suffix_map = suffix_map.copy()
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +000063 self.types_map = ({}, {}) # dict for (non-strict, strict)
64 self.types_map_inv = ({}, {})
65 for (ext, type) in types_map.items():
66 self.add_type(type, ext, True)
67 for (ext, type) in common_types.items():
68 self.add_type(type, ext, False)
Fred Drakeeeee4ec2001-08-03 21:01:44 +000069 for name in filenames:
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +000070 self.read(name, strict)
Fred Drakeeeee4ec2001-08-03 21:01:44 +000071
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +000072 def add_type(self, type, ext, strict=True):
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +000073 """Add a mapping between a type and an extension.
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +000074
75 When the extension is already known, the new
76 type will replace the old one. When the type
77 is already known the extension will be added
78 to the list of known extensions.
79
80 If strict is true, information will be added to
81 list of standard types, else to the list of non-standard
82 types.
83 """
84 self.types_map[strict][ext] = type
85 exts = self.types_map_inv[strict].setdefault(type, [])
86 if ext not in exts:
87 exts.append(ext)
88
89 def guess_type(self, url, strict=True):
Fred Drakeeeee4ec2001-08-03 21:01:44 +000090 """Guess the type of a file based on its URL.
91
92 Return value is a tuple (type, encoding) where type is None if
93 the type can't be guessed (no or unknown suffix) or a string
94 of the form type/subtype, usable for a MIME Content-type
95 header; and encoding is None for no encoding or the name of
96 the program used to encode (e.g. compress or gzip). The
97 mappings are table driven. Encoding suffixes are case
98 sensitive; type suffixes are first tried case sensitive, then
99 case insensitive.
100
101 The suffixes .tgz, .taz and .tz (case sensitive!) are all
102 mapped to '.tar.gz'. (This is table-driven too, using the
103 dictionary suffix_map.)
Barry Warsaw107771a2001-10-25 21:49:18 +0000104
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000105 Optional `strict' argument when False adds a bunch of commonly found,
Barry Warsaw107771a2001-10-25 21:49:18 +0000106 but non-standard types.
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000107 """
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000108 scheme, url = urllib.parse.splittype(url)
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000109 if scheme == 'data':
110 # syntax of data URLs:
111 # dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
112 # mediatype := [ type "/" subtype ] *( ";" parameter )
113 # data := *urlchar
114 # parameter := attribute "=" value
115 # type/subtype defaults to "text/plain"
116 comma = url.find(',')
117 if comma < 0:
118 # bad data URL
119 return None, None
120 semi = url.find(';', 0, comma)
121 if semi >= 0:
122 type = url[:semi]
123 else:
124 type = url[:comma]
125 if '=' in type or '/' not in type:
126 type = 'text/plain'
127 return type, None # never compressed, so encoding is None
128 base, ext = posixpath.splitext(url)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000129 while ext in self.suffix_map:
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000130 base, ext = posixpath.splitext(base + self.suffix_map[ext])
Raymond Hettinger54f02222002-06-01 14:18:47 +0000131 if ext in self.encodings_map:
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000132 encoding = self.encodings_map[ext]
133 base, ext = posixpath.splitext(base)
134 else:
135 encoding = None
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000136 types_map = self.types_map[True]
Raymond Hettinger54f02222002-06-01 14:18:47 +0000137 if ext in types_map:
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000138 return types_map[ext], encoding
Raymond Hettinger54f02222002-06-01 14:18:47 +0000139 elif ext.lower() in types_map:
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000140 return types_map[ext.lower()], encoding
Barry Warsaw107771a2001-10-25 21:49:18 +0000141 elif strict:
142 return None, encoding
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000143 types_map = self.types_map[False]
144 if ext in types_map:
145 return types_map[ext], encoding
146 elif ext.lower() in types_map:
147 return types_map[ext.lower()], encoding
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000148 else:
149 return None, encoding
150
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000151 def guess_all_extensions(self, type, strict=True):
152 """Guess the extensions for a file based on its MIME type.
153
154 Return value is a list of strings giving the possible filename
155 extensions, including the leading dot ('.'). The extension is not
Barry Warsaw9caa0d12003-06-09 22:27:41 +0000156 guaranteed to have been associated with any particular data stream,
157 but would be mapped to the MIME type `type' by guess_type().
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000158
159 Optional `strict' argument when false adds a bunch of commonly found,
160 but non-standard types.
161 """
162 type = type.lower()
163 extensions = self.types_map_inv[True].get(type, [])
164 if not strict:
165 for ext in self.types_map_inv[False].get(type, []):
166 if ext not in extensions:
167 extensions.append(ext)
Barry Warsaw9caa0d12003-06-09 22:27:41 +0000168 return extensions
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000169
170 def guess_extension(self, type, strict=True):
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000171 """Guess the extension for a file based on its MIME type.
172
173 Return value is a string giving a filename extension,
174 including the leading dot ('.'). The extension is not
175 guaranteed to have been associated with any particular data
176 stream, but would be mapped to the MIME type `type' by
177 guess_type(). If no extension can be guessed for `type', None
178 is returned.
Barry Warsaw107771a2001-10-25 21:49:18 +0000179
180 Optional `strict' argument when false adds a bunch of commonly found,
181 but non-standard types.
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000182 """
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000183 extensions = self.guess_all_extensions(type, strict)
Barry Warsaw9caa0d12003-06-09 22:27:41 +0000184 if not extensions:
185 return None
186 return extensions[0]
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000187
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000188 def read(self, filename, strict=True):
189 """
190 Read a single mime.types-format file, specified by pathname.
191
192 If strict is true, information will be added to
193 list of standard types, else to the list of non-standard
194 types.
195 """
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000196 fp = open(filename)
Walter Dörwald51cc72c2003-01-03 21:02:36 +0000197 self.readfp(fp, strict)
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000198 fp.close()
199
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000200 def readfp(self, fp, strict=True):
201 """
202 Read a single mime.types-format file.
203
204 If strict is true, information will be added to
205 list of standard types, else to the list of non-standard
206 types.
207 """
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000208 while 1:
Fred Drakec019ecb2001-08-16 15:54:28 +0000209 line = fp.readline()
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000210 if not line:
211 break
212 words = line.split()
213 for i in range(len(words)):
214 if words[i][0] == '#':
215 del words[i:]
216 break
217 if not words:
218 continue
219 type, suffixes = words[0], words[1:]
220 for suff in suffixes:
Walter Dörwald8fa89722003-01-03 21:06:46 +0000221 self.add_type(type, '.' + suff, strict)
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000222
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000223def guess_type(url, strict=True):
Guido van Rossumac8a9f31997-09-30 19:05:50 +0000224 """Guess the type of a file based on its URL.
225
226 Return value is a tuple (type, encoding) where type is None if the
227 type can't be guessed (no or unknown suffix) or a string of the
228 form type/subtype, usable for a MIME Content-type header; and
229 encoding is None for no encoding or the name of the program used
230 to encode (e.g. compress or gzip). The mappings are table
231 driven. Encoding suffixes are case sensitive; type suffixes are
232 first tried case sensitive, then case insensitive.
233
234 The suffixes .tgz, .taz and .tz (case sensitive!) are all mapped
235 to ".tar.gz". (This is table-driven too, using the dictionary
Fred Drake3130b7a1998-05-18 16:05:24 +0000236 suffix_map).
Barry Warsaw107771a2001-10-25 21:49:18 +0000237
238 Optional `strict' argument when false adds a bunch of commonly found, but
239 non-standard types.
Guido van Rossumac8a9f31997-09-30 19:05:50 +0000240 """
Antoine Pitrou57f3d932009-04-27 21:04:19 +0000241 if _db is None:
Benjamin Petersone0124bd2009-03-09 21:04:33 +0000242 init()
Antoine Pitrou57f3d932009-04-27 21:04:19 +0000243 return _db.guess_type(url, strict)
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000244
Guido van Rossumac8a9f31997-09-30 19:05:50 +0000245
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000246def guess_all_extensions(type, strict=True):
247 """Guess the extensions for a file based on its MIME type.
248
249 Return value is a list of strings giving the possible filename
250 extensions, including the leading dot ('.'). The extension is not
251 guaranteed to have been associated with any particular data
252 stream, but would be mapped to the MIME type `type' by
253 guess_type(). If no extension can be guessed for `type', None
254 is returned.
255
256 Optional `strict' argument when false adds a bunch of commonly found,
257 but non-standard types.
258 """
Antoine Pitrou57f3d932009-04-27 21:04:19 +0000259 if _db is None:
Benjamin Petersone0124bd2009-03-09 21:04:33 +0000260 init()
Antoine Pitrou57f3d932009-04-27 21:04:19 +0000261 return _db.guess_all_extensions(type, strict)
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000262
263def guess_extension(type, strict=True):
Fred Drake5109ffd1998-05-18 16:27:20 +0000264 """Guess the extension for a file based on its MIME type.
265
266 Return value is a string giving a filename extension, including the
267 leading dot ('.'). The extension is not guaranteed to have been
Fred Drake49413411998-05-19 15:15:59 +0000268 associated with any particular data stream, but would be mapped to the
269 MIME type `type' by guess_type(). If no extension can be guessed for
270 `type', None is returned.
Barry Warsaw107771a2001-10-25 21:49:18 +0000271
272 Optional `strict' argument when false adds a bunch of commonly found,
273 but non-standard types.
Fred Drake5109ffd1998-05-18 16:27:20 +0000274 """
Antoine Pitrou57f3d932009-04-27 21:04:19 +0000275 if _db is None:
Benjamin Petersone0124bd2009-03-09 21:04:33 +0000276 init()
Antoine Pitrou57f3d932009-04-27 21:04:19 +0000277 return _db.guess_extension(type, strict)
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000278
Walter Dörwald893020b2003-12-19 18:15:10 +0000279def add_type(type, ext, strict=True):
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000280 """Add a mapping between a type and an extension.
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000281
282 When the extension is already known, the new
283 type will replace the old one. When the type
284 is already known the extension will be added
285 to the list of known extensions.
286
287 If strict is true, information will be added to
288 list of standard types, else to the list of non-standard
289 types.
290 """
Antoine Pitrou57f3d932009-04-27 21:04:19 +0000291 if _db is None:
Benjamin Petersone0124bd2009-03-09 21:04:33 +0000292 init()
Antoine Pitrou57f3d932009-04-27 21:04:19 +0000293 return _db.add_type(type, ext, strict)
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000294
Fred Drake5109ffd1998-05-18 16:27:20 +0000295
Guido van Rossumac8a9f31997-09-30 19:05:50 +0000296def init(files=None):
Barry Warsaw107771a2001-10-25 21:49:18 +0000297 global suffix_map, types_map, encodings_map, common_types
Antoine Pitrou57f3d932009-04-27 21:04:19 +0000298 global inited, _db
299 inited = True # so that MimeTypes.__init__() doesn't call us again
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000300 db = MimeTypes()
301 if files is None:
302 files = knownfiles
303 for file in files:
304 if os.path.isfile(file):
305 db.readfp(open(file))
306 encodings_map = db.encodings_map
Fred Drakec81a0692001-08-16 18:14:38 +0000307 suffix_map = db.suffix_map
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000308 types_map = db.types_map[True]
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +0000309 common_types = db.types_map[False]
Antoine Pitrou57f3d932009-04-27 21:04:19 +0000310 # Make the DB a global variable now that it is fully initialized
311 _db = db
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000312
Guido van Rossumac8a9f31997-09-30 19:05:50 +0000313
314def read_mime_types(file):
315 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000316 f = open(file)
Guido van Rossumac8a9f31997-09-30 19:05:50 +0000317 except IOError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000318 return None
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000319 db = MimeTypes()
Walter Dörwaldbb51ed32003-01-03 19:33:17 +0000320 db.readfp(f, True)
321 return db.types_map[True]
Fred Drakeeeee4ec2001-08-03 21:01:44 +0000322
Guido van Rossumac8a9f31997-09-30 19:05:50 +0000323
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000324def _default_mime_types():
325 global suffix_map
326 global encodings_map
327 global types_map
328 global common_types
Guido van Rossumac8a9f31997-09-30 19:05:50 +0000329
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000330 suffix_map = {
331 '.tgz': '.tar.gz',
332 '.taz': '.tar.gz',
333 '.tz': '.tar.gz',
Guido van Rossum360e4b82007-05-14 22:51:27 +0000334 '.tbz2': '.tar.bz2',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335 }
Guido van Rossumac8a9f31997-09-30 19:05:50 +0000336
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000337 encodings_map = {
338 '.gz': 'gzip',
339 '.Z': 'compress',
Guido van Rossum360e4b82007-05-14 22:51:27 +0000340 '.bz2': 'bzip2',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000341 }
Barry Warsaw107771a2001-10-25 21:49:18 +0000342
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343 # Before adding new types, make sure they are either registered with IANA,
344 # at http://www.isi.edu/in-notes/iana/assignments/media-types
345 # or extensions, i.e. using the x- prefix
Eric S. Raymond51cc3bc2001-02-09 09:44:47 +0000346
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000347 # If you add to these, please keep them sorted!
348 types_map = {
349 '.a' : 'application/octet-stream',
350 '.ai' : 'application/postscript',
351 '.aif' : 'audio/x-aiff',
352 '.aifc' : 'audio/x-aiff',
353 '.aiff' : 'audio/x-aiff',
354 '.au' : 'audio/basic',
355 '.avi' : 'video/x-msvideo',
356 '.bat' : 'text/plain',
357 '.bcpio' : 'application/x-bcpio',
358 '.bin' : 'application/octet-stream',
359 '.bmp' : 'image/x-ms-bmp',
360 '.c' : 'text/plain',
361 # Duplicates :(
362 '.cdf' : 'application/x-cdf',
363 '.cdf' : 'application/x-netcdf',
364 '.cpio' : 'application/x-cpio',
365 '.csh' : 'application/x-csh',
366 '.css' : 'text/css',
367 '.dll' : 'application/octet-stream',
368 '.doc' : 'application/msword',
369 '.dot' : 'application/msword',
370 '.dvi' : 'application/x-dvi',
371 '.eml' : 'message/rfc822',
372 '.eps' : 'application/postscript',
373 '.etx' : 'text/x-setext',
374 '.exe' : 'application/octet-stream',
375 '.gif' : 'image/gif',
376 '.gtar' : 'application/x-gtar',
377 '.h' : 'text/plain',
378 '.hdf' : 'application/x-hdf',
379 '.htm' : 'text/html',
380 '.html' : 'text/html',
381 '.ief' : 'image/ief',
382 '.jpe' : 'image/jpeg',
383 '.jpeg' : 'image/jpeg',
384 '.jpg' : 'image/jpeg',
385 '.js' : 'application/x-javascript',
386 '.ksh' : 'text/plain',
387 '.latex' : 'application/x-latex',
388 '.m1v' : 'video/mpeg',
389 '.man' : 'application/x-troff-man',
390 '.me' : 'application/x-troff-me',
391 '.mht' : 'message/rfc822',
392 '.mhtml' : 'message/rfc822',
393 '.mif' : 'application/x-mif',
394 '.mov' : 'video/quicktime',
395 '.movie' : 'video/x-sgi-movie',
396 '.mp2' : 'audio/mpeg',
397 '.mp3' : 'audio/mpeg',
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000398 '.mp4' : 'video/mp4',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000399 '.mpa' : 'video/mpeg',
400 '.mpe' : 'video/mpeg',
401 '.mpeg' : 'video/mpeg',
402 '.mpg' : 'video/mpeg',
403 '.ms' : 'application/x-troff-ms',
404 '.nc' : 'application/x-netcdf',
405 '.nws' : 'message/rfc822',
406 '.o' : 'application/octet-stream',
407 '.obj' : 'application/octet-stream',
408 '.oda' : 'application/oda',
409 '.p12' : 'application/x-pkcs12',
410 '.p7c' : 'application/pkcs7-mime',
411 '.pbm' : 'image/x-portable-bitmap',
412 '.pdf' : 'application/pdf',
413 '.pfx' : 'application/x-pkcs12',
414 '.pgm' : 'image/x-portable-graymap',
415 '.pl' : 'text/plain',
416 '.png' : 'image/png',
417 '.pnm' : 'image/x-portable-anymap',
418 '.pot' : 'application/vnd.ms-powerpoint',
419 '.ppa' : 'application/vnd.ms-powerpoint',
420 '.ppm' : 'image/x-portable-pixmap',
421 '.pps' : 'application/vnd.ms-powerpoint',
422 '.ppt' : 'application/vnd.ms-powerpoint',
423 '.ps' : 'application/postscript',
424 '.pwz' : 'application/vnd.ms-powerpoint',
425 '.py' : 'text/x-python',
426 '.pyc' : 'application/x-python-code',
427 '.pyo' : 'application/x-python-code',
428 '.qt' : 'video/quicktime',
429 '.ra' : 'audio/x-pn-realaudio',
430 '.ram' : 'application/x-pn-realaudio',
431 '.ras' : 'image/x-cmu-raster',
432 '.rdf' : 'application/xml',
433 '.rgb' : 'image/x-rgb',
434 '.roff' : 'application/x-troff',
435 '.rtx' : 'text/richtext',
436 '.sgm' : 'text/x-sgml',
437 '.sgml' : 'text/x-sgml',
438 '.sh' : 'application/x-sh',
439 '.shar' : 'application/x-shar',
440 '.snd' : 'audio/basic',
441 '.so' : 'application/octet-stream',
442 '.src' : 'application/x-wais-source',
443 '.sv4cpio': 'application/x-sv4cpio',
444 '.sv4crc' : 'application/x-sv4crc',
445 '.swf' : 'application/x-shockwave-flash',
446 '.t' : 'application/x-troff',
447 '.tar' : 'application/x-tar',
448 '.tcl' : 'application/x-tcl',
449 '.tex' : 'application/x-tex',
450 '.texi' : 'application/x-texinfo',
451 '.texinfo': 'application/x-texinfo',
452 '.tif' : 'image/tiff',
453 '.tiff' : 'image/tiff',
454 '.tr' : 'application/x-troff',
455 '.tsv' : 'text/tab-separated-values',
456 '.txt' : 'text/plain',
457 '.ustar' : 'application/x-ustar',
458 '.vcf' : 'text/x-vcard',
459 '.wav' : 'audio/x-wav',
460 '.wiz' : 'application/msword',
461 '.wsdl' : 'application/xml',
462 '.xbm' : 'image/x-xbitmap',
463 '.xlb' : 'application/vnd.ms-excel',
464 # Duplicates :(
465 '.xls' : 'application/excel',
466 '.xls' : 'application/vnd.ms-excel',
467 '.xml' : 'text/xml',
468 '.xpdl' : 'application/xml',
469 '.xpm' : 'image/x-xpixmap',
470 '.xsl' : 'application/xml',
471 '.xwd' : 'image/x-xwindowdump',
472 '.zip' : 'application/zip',
473 }
Barry Warsaw107771a2001-10-25 21:49:18 +0000474
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000475 # These are non-standard types, commonly found in the wild. They will
476 # only match if strict=0 flag is given to the API methods.
477
478 # Please sort these too
479 common_types = {
480 '.jpg' : 'image/jpg',
481 '.mid' : 'audio/midi',
482 '.midi': 'audio/midi',
483 '.pct' : 'image/pict',
484 '.pic' : 'image/pict',
485 '.pict': 'image/pict',
486 '.rtf' : 'application/rtf',
487 '.xul' : 'text/xul'
488 }
489
490
491_default_mime_types()
Barry Warsaw107771a2001-10-25 21:49:18 +0000492
493
Eric S. Raymond51cc3bc2001-02-09 09:44:47 +0000494if __name__ == '__main__':
495 import sys
Barry Warsaw107771a2001-10-25 21:49:18 +0000496 import getopt
497
Fred Drake698da022001-12-05 15:58:29 +0000498 USAGE = """\
499Usage: mimetypes.py [options] type
500
501Options:
502 --help / -h -- print this message and exit
503 --lenient / -l -- additionally search of some common, but non-standard
504 types.
505 --extension / -e -- guess extension instead of type
506
507More than one type argument may be given.
508"""
509
510 def usage(code, msg=''):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000511 print(USAGE)
512 if msg: print(msg)
Fred Drake698da022001-12-05 15:58:29 +0000513 sys.exit(code)
514
Barry Warsaw107771a2001-10-25 21:49:18 +0000515 try:
516 opts, args = getopt.getopt(sys.argv[1:], 'hle',
517 ['help', 'lenient', 'extension'])
Guido van Rossumb940e112007-01-10 16:19:56 +0000518 except getopt.error as msg:
Barry Warsaw107771a2001-10-25 21:49:18 +0000519 usage(1, msg)
520
521 strict = 1
522 extension = 0
523 for opt, arg in opts:
524 if opt in ('-h', '--help'):
525 usage(0)
526 elif opt in ('-l', '--lenient'):
527 strict = 0
528 elif opt in ('-e', '--extension'):
529 extension = 1
530 for gtype in args:
531 if extension:
532 guess = guess_extension(gtype, strict)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000533 if not guess: print("I don't know anything about type", gtype)
534 else: print(guess)
Barry Warsaw107771a2001-10-25 21:49:18 +0000535 else:
536 guess, encoding = guess_type(gtype, strict)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000537 if not guess: print("I don't know anything about type", gtype)
538 else: print('type:', guess, 'encoding:', encoding)