1 | n/a | """Guess the MIME type of a file. |
---|
2 | n/a | |
---|
3 | n/a | This module defines two useful functions: |
---|
4 | n/a | |
---|
5 | n/a | guess_type(url, strict=True) -- guess the MIME type and encoding of a URL. |
---|
6 | n/a | |
---|
7 | n/a | guess_extension(type, strict=True) -- guess the extension for a given MIME type. |
---|
8 | n/a | |
---|
9 | n/a | It also contains the following, for tuning the behavior: |
---|
10 | n/a | |
---|
11 | n/a | Data: |
---|
12 | n/a | |
---|
13 | n/a | knownfiles -- list of files to parse |
---|
14 | n/a | inited -- flag set when init() has been called |
---|
15 | n/a | suffix_map -- dictionary mapping suffixes to suffixes |
---|
16 | n/a | encodings_map -- dictionary mapping suffixes to encodings |
---|
17 | n/a | types_map -- dictionary mapping suffixes to types |
---|
18 | n/a | |
---|
19 | n/a | Functions: |
---|
20 | n/a | |
---|
21 | n/a | init([files]) -- parse a list of files, default knownfiles (on Windows, the |
---|
22 | n/a | default values are taken from the registry) |
---|
23 | n/a | read_mime_types(file) -- parse one file, return a dictionary or None |
---|
24 | n/a | """ |
---|
25 | n/a | |
---|
26 | n/a | import os |
---|
27 | n/a | import sys |
---|
28 | n/a | import posixpath |
---|
29 | n/a | import urllib.parse |
---|
30 | n/a | try: |
---|
31 | n/a | import winreg as _winreg |
---|
32 | n/a | except ImportError: |
---|
33 | n/a | _winreg = None |
---|
34 | n/a | |
---|
35 | n/a | __all__ = [ |
---|
36 | n/a | "knownfiles", "inited", "MimeTypes", |
---|
37 | n/a | "guess_type", "guess_all_extensions", "guess_extension", |
---|
38 | n/a | "add_type", "init", "read_mime_types", |
---|
39 | n/a | "suffix_map", "encodings_map", "types_map", "common_types" |
---|
40 | n/a | ] |
---|
41 | n/a | |
---|
42 | n/a | knownfiles = [ |
---|
43 | n/a | "/etc/mime.types", |
---|
44 | n/a | "/etc/httpd/mime.types", # Mac OS X |
---|
45 | n/a | "/etc/httpd/conf/mime.types", # Apache |
---|
46 | n/a | "/etc/apache/mime.types", # Apache 1 |
---|
47 | n/a | "/etc/apache2/mime.types", # Apache 2 |
---|
48 | n/a | "/usr/local/etc/httpd/conf/mime.types", |
---|
49 | n/a | "/usr/local/lib/netscape/mime.types", |
---|
50 | n/a | "/usr/local/etc/httpd/conf/mime.types", # Apache 1.2 |
---|
51 | n/a | "/usr/local/etc/mime.types", # Apache 1.3 |
---|
52 | n/a | ] |
---|
53 | n/a | |
---|
54 | n/a | inited = False |
---|
55 | n/a | _db = None |
---|
56 | n/a | |
---|
57 | n/a | |
---|
58 | n/a | class MimeTypes: |
---|
59 | n/a | """MIME-types datastore. |
---|
60 | n/a | |
---|
61 | n/a | This datastore can handle information from mime.types-style files |
---|
62 | n/a | and supports basic determination of MIME type from a filename or |
---|
63 | n/a | URL, and can guess a reasonable extension given a MIME type. |
---|
64 | n/a | """ |
---|
65 | n/a | |
---|
66 | n/a | def __init__(self, filenames=(), strict=True): |
---|
67 | n/a | if not inited: |
---|
68 | n/a | init() |
---|
69 | n/a | self.encodings_map = encodings_map.copy() |
---|
70 | n/a | self.suffix_map = suffix_map.copy() |
---|
71 | n/a | self.types_map = ({}, {}) # dict for (non-strict, strict) |
---|
72 | n/a | self.types_map_inv = ({}, {}) |
---|
73 | n/a | for (ext, type) in types_map.items(): |
---|
74 | n/a | self.add_type(type, ext, True) |
---|
75 | n/a | for (ext, type) in common_types.items(): |
---|
76 | n/a | self.add_type(type, ext, False) |
---|
77 | n/a | for name in filenames: |
---|
78 | n/a | self.read(name, strict) |
---|
79 | n/a | |
---|
80 | n/a | def add_type(self, type, ext, strict=True): |
---|
81 | n/a | """Add a mapping between a type and an extension. |
---|
82 | n/a | |
---|
83 | n/a | When the extension is already known, the new |
---|
84 | n/a | type will replace the old one. When the type |
---|
85 | n/a | is already known the extension will be added |
---|
86 | n/a | to the list of known extensions. |
---|
87 | n/a | |
---|
88 | n/a | If strict is true, information will be added to |
---|
89 | n/a | list of standard types, else to the list of non-standard |
---|
90 | n/a | types. |
---|
91 | n/a | """ |
---|
92 | n/a | self.types_map[strict][ext] = type |
---|
93 | n/a | exts = self.types_map_inv[strict].setdefault(type, []) |
---|
94 | n/a | if ext not in exts: |
---|
95 | n/a | exts.append(ext) |
---|
96 | n/a | |
---|
97 | n/a | def guess_type(self, url, strict=True): |
---|
98 | n/a | """Guess the type of a file based on its URL. |
---|
99 | n/a | |
---|
100 | n/a | Return value is a tuple (type, encoding) where type is None if |
---|
101 | n/a | the type can't be guessed (no or unknown suffix) or a string |
---|
102 | n/a | of the form type/subtype, usable for a MIME Content-type |
---|
103 | n/a | header; and encoding is None for no encoding or the name of |
---|
104 | n/a | the program used to encode (e.g. compress or gzip). The |
---|
105 | n/a | mappings are table driven. Encoding suffixes are case |
---|
106 | n/a | sensitive; type suffixes are first tried case sensitive, then |
---|
107 | n/a | case insensitive. |
---|
108 | n/a | |
---|
109 | n/a | The suffixes .tgz, .taz and .tz (case sensitive!) are all |
---|
110 | n/a | mapped to '.tar.gz'. (This is table-driven too, using the |
---|
111 | n/a | dictionary suffix_map.) |
---|
112 | n/a | |
---|
113 | n/a | Optional `strict' argument when False adds a bunch of commonly found, |
---|
114 | n/a | but non-standard types. |
---|
115 | n/a | """ |
---|
116 | n/a | scheme, url = urllib.parse.splittype(url) |
---|
117 | n/a | if scheme == 'data': |
---|
118 | n/a | # syntax of data URLs: |
---|
119 | n/a | # dataurl := "data:" [ mediatype ] [ ";base64" ] "," data |
---|
120 | n/a | # mediatype := [ type "/" subtype ] *( ";" parameter ) |
---|
121 | n/a | # data := *urlchar |
---|
122 | n/a | # parameter := attribute "=" value |
---|
123 | n/a | # type/subtype defaults to "text/plain" |
---|
124 | n/a | comma = url.find(',') |
---|
125 | n/a | if comma < 0: |
---|
126 | n/a | # bad data URL |
---|
127 | n/a | return None, None |
---|
128 | n/a | semi = url.find(';', 0, comma) |
---|
129 | n/a | if semi >= 0: |
---|
130 | n/a | type = url[:semi] |
---|
131 | n/a | else: |
---|
132 | n/a | type = url[:comma] |
---|
133 | n/a | if '=' in type or '/' not in type: |
---|
134 | n/a | type = 'text/plain' |
---|
135 | n/a | return type, None # never compressed, so encoding is None |
---|
136 | n/a | base, ext = posixpath.splitext(url) |
---|
137 | n/a | while ext in self.suffix_map: |
---|
138 | n/a | base, ext = posixpath.splitext(base + self.suffix_map[ext]) |
---|
139 | n/a | if ext in self.encodings_map: |
---|
140 | n/a | encoding = self.encodings_map[ext] |
---|
141 | n/a | base, ext = posixpath.splitext(base) |
---|
142 | n/a | else: |
---|
143 | n/a | encoding = None |
---|
144 | n/a | types_map = self.types_map[True] |
---|
145 | n/a | if ext in types_map: |
---|
146 | n/a | return types_map[ext], encoding |
---|
147 | n/a | elif ext.lower() in types_map: |
---|
148 | n/a | return types_map[ext.lower()], encoding |
---|
149 | n/a | elif strict: |
---|
150 | n/a | return None, encoding |
---|
151 | n/a | types_map = self.types_map[False] |
---|
152 | n/a | if ext in types_map: |
---|
153 | n/a | return types_map[ext], encoding |
---|
154 | n/a | elif ext.lower() in types_map: |
---|
155 | n/a | return types_map[ext.lower()], encoding |
---|
156 | n/a | else: |
---|
157 | n/a | return None, encoding |
---|
158 | n/a | |
---|
159 | n/a | def guess_all_extensions(self, type, strict=True): |
---|
160 | n/a | """Guess the extensions for a file based on its MIME type. |
---|
161 | n/a | |
---|
162 | n/a | Return value is a list of strings giving the possible filename |
---|
163 | n/a | extensions, including the leading dot ('.'). The extension is not |
---|
164 | n/a | guaranteed to have been associated with any particular data stream, |
---|
165 | n/a | but would be mapped to the MIME type `type' by guess_type(). |
---|
166 | n/a | |
---|
167 | n/a | Optional `strict' argument when false adds a bunch of commonly found, |
---|
168 | n/a | but non-standard types. |
---|
169 | n/a | """ |
---|
170 | n/a | type = type.lower() |
---|
171 | n/a | extensions = self.types_map_inv[True].get(type, []) |
---|
172 | n/a | if not strict: |
---|
173 | n/a | for ext in self.types_map_inv[False].get(type, []): |
---|
174 | n/a | if ext not in extensions: |
---|
175 | n/a | extensions.append(ext) |
---|
176 | n/a | return extensions |
---|
177 | n/a | |
---|
178 | n/a | def guess_extension(self, type, strict=True): |
---|
179 | n/a | """Guess the extension for a file based on its MIME type. |
---|
180 | n/a | |
---|
181 | n/a | Return value is a string giving a filename extension, |
---|
182 | n/a | including the leading dot ('.'). The extension is not |
---|
183 | n/a | guaranteed to have been associated with any particular data |
---|
184 | n/a | stream, but would be mapped to the MIME type `type' by |
---|
185 | n/a | guess_type(). If no extension can be guessed for `type', None |
---|
186 | n/a | is returned. |
---|
187 | n/a | |
---|
188 | n/a | Optional `strict' argument when false adds a bunch of commonly found, |
---|
189 | n/a | but non-standard types. |
---|
190 | n/a | """ |
---|
191 | n/a | extensions = self.guess_all_extensions(type, strict) |
---|
192 | n/a | if not extensions: |
---|
193 | n/a | return None |
---|
194 | n/a | return extensions[0] |
---|
195 | n/a | |
---|
196 | n/a | def read(self, filename, strict=True): |
---|
197 | n/a | """ |
---|
198 | n/a | Read a single mime.types-format file, specified by pathname. |
---|
199 | n/a | |
---|
200 | n/a | If strict is true, information will be added to |
---|
201 | n/a | list of standard types, else to the list of non-standard |
---|
202 | n/a | types. |
---|
203 | n/a | """ |
---|
204 | n/a | with open(filename, encoding='utf-8') as fp: |
---|
205 | n/a | self.readfp(fp, strict) |
---|
206 | n/a | |
---|
207 | n/a | def readfp(self, fp, strict=True): |
---|
208 | n/a | """ |
---|
209 | n/a | Read a single mime.types-format file. |
---|
210 | n/a | |
---|
211 | n/a | If strict is true, information will be added to |
---|
212 | n/a | list of standard types, else to the list of non-standard |
---|
213 | n/a | types. |
---|
214 | n/a | """ |
---|
215 | n/a | while 1: |
---|
216 | n/a | line = fp.readline() |
---|
217 | n/a | if not line: |
---|
218 | n/a | break |
---|
219 | n/a | words = line.split() |
---|
220 | n/a | for i in range(len(words)): |
---|
221 | n/a | if words[i][0] == '#': |
---|
222 | n/a | del words[i:] |
---|
223 | n/a | break |
---|
224 | n/a | if not words: |
---|
225 | n/a | continue |
---|
226 | n/a | type, suffixes = words[0], words[1:] |
---|
227 | n/a | for suff in suffixes: |
---|
228 | n/a | self.add_type(type, '.' + suff, strict) |
---|
229 | n/a | |
---|
230 | n/a | def read_windows_registry(self, strict=True): |
---|
231 | n/a | """ |
---|
232 | n/a | Load the MIME types database from Windows registry. |
---|
233 | n/a | |
---|
234 | n/a | If strict is true, information will be added to |
---|
235 | n/a | list of standard types, else to the list of non-standard |
---|
236 | n/a | types. |
---|
237 | n/a | """ |
---|
238 | n/a | |
---|
239 | n/a | # Windows only |
---|
240 | n/a | if not _winreg: |
---|
241 | n/a | return |
---|
242 | n/a | |
---|
243 | n/a | def enum_types(mimedb): |
---|
244 | n/a | i = 0 |
---|
245 | n/a | while True: |
---|
246 | n/a | try: |
---|
247 | n/a | ctype = _winreg.EnumKey(mimedb, i) |
---|
248 | n/a | except EnvironmentError: |
---|
249 | n/a | break |
---|
250 | n/a | else: |
---|
251 | n/a | if '\0' not in ctype: |
---|
252 | n/a | yield ctype |
---|
253 | n/a | i += 1 |
---|
254 | n/a | |
---|
255 | n/a | with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr: |
---|
256 | n/a | for subkeyname in enum_types(hkcr): |
---|
257 | n/a | try: |
---|
258 | n/a | with _winreg.OpenKey(hkcr, subkeyname) as subkey: |
---|
259 | n/a | # Only check file extensions |
---|
260 | n/a | if not subkeyname.startswith("."): |
---|
261 | n/a | continue |
---|
262 | n/a | # raises EnvironmentError if no 'Content Type' value |
---|
263 | n/a | mimetype, datatype = _winreg.QueryValueEx( |
---|
264 | n/a | subkey, 'Content Type') |
---|
265 | n/a | if datatype != _winreg.REG_SZ: |
---|
266 | n/a | continue |
---|
267 | n/a | self.add_type(mimetype, subkeyname, strict) |
---|
268 | n/a | except EnvironmentError: |
---|
269 | n/a | continue |
---|
270 | n/a | |
---|
271 | n/a | def guess_type(url, strict=True): |
---|
272 | n/a | """Guess the type of a file based on its URL. |
---|
273 | n/a | |
---|
274 | n/a | Return value is a tuple (type, encoding) where type is None if the |
---|
275 | n/a | type can't be guessed (no or unknown suffix) or a string of the |
---|
276 | n/a | form type/subtype, usable for a MIME Content-type header; and |
---|
277 | n/a | encoding is None for no encoding or the name of the program used |
---|
278 | n/a | to encode (e.g. compress or gzip). The mappings are table |
---|
279 | n/a | driven. Encoding suffixes are case sensitive; type suffixes are |
---|
280 | n/a | first tried case sensitive, then case insensitive. |
---|
281 | n/a | |
---|
282 | n/a | The suffixes .tgz, .taz and .tz (case sensitive!) are all mapped |
---|
283 | n/a | to ".tar.gz". (This is table-driven too, using the dictionary |
---|
284 | n/a | suffix_map). |
---|
285 | n/a | |
---|
286 | n/a | Optional `strict' argument when false adds a bunch of commonly found, but |
---|
287 | n/a | non-standard types. |
---|
288 | n/a | """ |
---|
289 | n/a | if _db is None: |
---|
290 | n/a | init() |
---|
291 | n/a | return _db.guess_type(url, strict) |
---|
292 | n/a | |
---|
293 | n/a | |
---|
294 | n/a | def guess_all_extensions(type, strict=True): |
---|
295 | n/a | """Guess the extensions for a file based on its MIME type. |
---|
296 | n/a | |
---|
297 | n/a | Return value is a list of strings giving the possible filename |
---|
298 | n/a | extensions, including the leading dot ('.'). The extension is not |
---|
299 | n/a | guaranteed to have been associated with any particular data |
---|
300 | n/a | stream, but would be mapped to the MIME type `type' by |
---|
301 | n/a | guess_type(). If no extension can be guessed for `type', None |
---|
302 | n/a | is returned. |
---|
303 | n/a | |
---|
304 | n/a | Optional `strict' argument when false adds a bunch of commonly found, |
---|
305 | n/a | but non-standard types. |
---|
306 | n/a | """ |
---|
307 | n/a | if _db is None: |
---|
308 | n/a | init() |
---|
309 | n/a | return _db.guess_all_extensions(type, strict) |
---|
310 | n/a | |
---|
311 | n/a | def guess_extension(type, strict=True): |
---|
312 | n/a | """Guess the extension for a file based on its MIME type. |
---|
313 | n/a | |
---|
314 | n/a | Return value is a string giving a filename extension, including the |
---|
315 | n/a | leading dot ('.'). The extension is not guaranteed to have been |
---|
316 | n/a | associated with any particular data stream, but would be mapped to the |
---|
317 | n/a | MIME type `type' by guess_type(). If no extension can be guessed for |
---|
318 | n/a | `type', None is returned. |
---|
319 | n/a | |
---|
320 | n/a | Optional `strict' argument when false adds a bunch of commonly found, |
---|
321 | n/a | but non-standard types. |
---|
322 | n/a | """ |
---|
323 | n/a | if _db is None: |
---|
324 | n/a | init() |
---|
325 | n/a | return _db.guess_extension(type, strict) |
---|
326 | n/a | |
---|
327 | n/a | def add_type(type, ext, strict=True): |
---|
328 | n/a | """Add a mapping between a type and an extension. |
---|
329 | n/a | |
---|
330 | n/a | When the extension is already known, the new |
---|
331 | n/a | type will replace the old one. When the type |
---|
332 | n/a | is already known the extension will be added |
---|
333 | n/a | to the list of known extensions. |
---|
334 | n/a | |
---|
335 | n/a | If strict is true, information will be added to |
---|
336 | n/a | list of standard types, else to the list of non-standard |
---|
337 | n/a | types. |
---|
338 | n/a | """ |
---|
339 | n/a | if _db is None: |
---|
340 | n/a | init() |
---|
341 | n/a | return _db.add_type(type, ext, strict) |
---|
342 | n/a | |
---|
343 | n/a | |
---|
344 | n/a | def init(files=None): |
---|
345 | n/a | global suffix_map, types_map, encodings_map, common_types |
---|
346 | n/a | global inited, _db |
---|
347 | n/a | inited = True # so that MimeTypes.__init__() doesn't call us again |
---|
348 | n/a | db = MimeTypes() |
---|
349 | n/a | if files is None: |
---|
350 | n/a | if _winreg: |
---|
351 | n/a | db.read_windows_registry() |
---|
352 | n/a | files = knownfiles |
---|
353 | n/a | for file in files: |
---|
354 | n/a | if os.path.isfile(file): |
---|
355 | n/a | db.read(file) |
---|
356 | n/a | encodings_map = db.encodings_map |
---|
357 | n/a | suffix_map = db.suffix_map |
---|
358 | n/a | types_map = db.types_map[True] |
---|
359 | n/a | common_types = db.types_map[False] |
---|
360 | n/a | # Make the DB a global variable now that it is fully initialized |
---|
361 | n/a | _db = db |
---|
362 | n/a | |
---|
363 | n/a | |
---|
364 | n/a | def read_mime_types(file): |
---|
365 | n/a | try: |
---|
366 | n/a | f = open(file) |
---|
367 | n/a | except OSError: |
---|
368 | n/a | return None |
---|
369 | n/a | with f: |
---|
370 | n/a | db = MimeTypes() |
---|
371 | n/a | db.readfp(f, True) |
---|
372 | n/a | return db.types_map[True] |
---|
373 | n/a | |
---|
374 | n/a | |
---|
375 | n/a | def _default_mime_types(): |
---|
376 | n/a | global suffix_map |
---|
377 | n/a | global encodings_map |
---|
378 | n/a | global types_map |
---|
379 | n/a | global common_types |
---|
380 | n/a | |
---|
381 | n/a | suffix_map = { |
---|
382 | n/a | '.svgz': '.svg.gz', |
---|
383 | n/a | '.tgz': '.tar.gz', |
---|
384 | n/a | '.taz': '.tar.gz', |
---|
385 | n/a | '.tz': '.tar.gz', |
---|
386 | n/a | '.tbz2': '.tar.bz2', |
---|
387 | n/a | '.txz': '.tar.xz', |
---|
388 | n/a | } |
---|
389 | n/a | |
---|
390 | n/a | encodings_map = { |
---|
391 | n/a | '.gz': 'gzip', |
---|
392 | n/a | '.Z': 'compress', |
---|
393 | n/a | '.bz2': 'bzip2', |
---|
394 | n/a | '.xz': 'xz', |
---|
395 | n/a | } |
---|
396 | n/a | |
---|
397 | n/a | # Before adding new types, make sure they are either registered with IANA, |
---|
398 | n/a | # at http://www.iana.org/assignments/media-types |
---|
399 | n/a | # or extensions, i.e. using the x- prefix |
---|
400 | n/a | |
---|
401 | n/a | # If you add to these, please keep them sorted! |
---|
402 | n/a | types_map = { |
---|
403 | n/a | '.a' : 'application/octet-stream', |
---|
404 | n/a | '.ai' : 'application/postscript', |
---|
405 | n/a | '.aif' : 'audio/x-aiff', |
---|
406 | n/a | '.aifc' : 'audio/x-aiff', |
---|
407 | n/a | '.aiff' : 'audio/x-aiff', |
---|
408 | n/a | '.au' : 'audio/basic', |
---|
409 | n/a | '.avi' : 'video/x-msvideo', |
---|
410 | n/a | '.bat' : 'text/plain', |
---|
411 | n/a | '.bcpio' : 'application/x-bcpio', |
---|
412 | n/a | '.bin' : 'application/octet-stream', |
---|
413 | n/a | '.bmp' : 'image/x-ms-bmp', |
---|
414 | n/a | '.c' : 'text/plain', |
---|
415 | n/a | # Duplicates :( |
---|
416 | n/a | '.cdf' : 'application/x-cdf', |
---|
417 | n/a | '.cdf' : 'application/x-netcdf', |
---|
418 | n/a | '.cpio' : 'application/x-cpio', |
---|
419 | n/a | '.csh' : 'application/x-csh', |
---|
420 | n/a | '.css' : 'text/css', |
---|
421 | n/a | '.csv' : 'text/csv', |
---|
422 | n/a | '.dll' : 'application/octet-stream', |
---|
423 | n/a | '.doc' : 'application/msword', |
---|
424 | n/a | '.dot' : 'application/msword', |
---|
425 | n/a | '.dvi' : 'application/x-dvi', |
---|
426 | n/a | '.eml' : 'message/rfc822', |
---|
427 | n/a | '.eps' : 'application/postscript', |
---|
428 | n/a | '.etx' : 'text/x-setext', |
---|
429 | n/a | '.exe' : 'application/octet-stream', |
---|
430 | n/a | '.gif' : 'image/gif', |
---|
431 | n/a | '.gtar' : 'application/x-gtar', |
---|
432 | n/a | '.h' : 'text/plain', |
---|
433 | n/a | '.hdf' : 'application/x-hdf', |
---|
434 | n/a | '.htm' : 'text/html', |
---|
435 | n/a | '.html' : 'text/html', |
---|
436 | n/a | '.ico' : 'image/vnd.microsoft.icon', |
---|
437 | n/a | '.ief' : 'image/ief', |
---|
438 | n/a | '.jpe' : 'image/jpeg', |
---|
439 | n/a | '.jpeg' : 'image/jpeg', |
---|
440 | n/a | '.jpg' : 'image/jpeg', |
---|
441 | n/a | '.js' : 'application/javascript', |
---|
442 | n/a | '.ksh' : 'text/plain', |
---|
443 | n/a | '.latex' : 'application/x-latex', |
---|
444 | n/a | '.m1v' : 'video/mpeg', |
---|
445 | n/a | '.m3u' : 'application/vnd.apple.mpegurl', |
---|
446 | n/a | '.m3u8' : 'application/vnd.apple.mpegurl', |
---|
447 | n/a | '.man' : 'application/x-troff-man', |
---|
448 | n/a | '.me' : 'application/x-troff-me', |
---|
449 | n/a | '.mht' : 'message/rfc822', |
---|
450 | n/a | '.mhtml' : 'message/rfc822', |
---|
451 | n/a | '.mif' : 'application/x-mif', |
---|
452 | n/a | '.mov' : 'video/quicktime', |
---|
453 | n/a | '.movie' : 'video/x-sgi-movie', |
---|
454 | n/a | '.mp2' : 'audio/mpeg', |
---|
455 | n/a | '.mp3' : 'audio/mpeg', |
---|
456 | n/a | '.mp4' : 'video/mp4', |
---|
457 | n/a | '.mpa' : 'video/mpeg', |
---|
458 | n/a | '.mpe' : 'video/mpeg', |
---|
459 | n/a | '.mpeg' : 'video/mpeg', |
---|
460 | n/a | '.mpg' : 'video/mpeg', |
---|
461 | n/a | '.ms' : 'application/x-troff-ms', |
---|
462 | n/a | '.nc' : 'application/x-netcdf', |
---|
463 | n/a | '.nws' : 'message/rfc822', |
---|
464 | n/a | '.o' : 'application/octet-stream', |
---|
465 | n/a | '.obj' : 'application/octet-stream', |
---|
466 | n/a | '.oda' : 'application/oda', |
---|
467 | n/a | '.p12' : 'application/x-pkcs12', |
---|
468 | n/a | '.p7c' : 'application/pkcs7-mime', |
---|
469 | n/a | '.pbm' : 'image/x-portable-bitmap', |
---|
470 | n/a | '.pdf' : 'application/pdf', |
---|
471 | n/a | '.pfx' : 'application/x-pkcs12', |
---|
472 | n/a | '.pgm' : 'image/x-portable-graymap', |
---|
473 | n/a | '.pl' : 'text/plain', |
---|
474 | n/a | '.png' : 'image/png', |
---|
475 | n/a | '.pnm' : 'image/x-portable-anymap', |
---|
476 | n/a | '.pot' : 'application/vnd.ms-powerpoint', |
---|
477 | n/a | '.ppa' : 'application/vnd.ms-powerpoint', |
---|
478 | n/a | '.ppm' : 'image/x-portable-pixmap', |
---|
479 | n/a | '.pps' : 'application/vnd.ms-powerpoint', |
---|
480 | n/a | '.ppt' : 'application/vnd.ms-powerpoint', |
---|
481 | n/a | '.ps' : 'application/postscript', |
---|
482 | n/a | '.pwz' : 'application/vnd.ms-powerpoint', |
---|
483 | n/a | '.py' : 'text/x-python', |
---|
484 | n/a | '.pyc' : 'application/x-python-code', |
---|
485 | n/a | '.pyo' : 'application/x-python-code', |
---|
486 | n/a | '.qt' : 'video/quicktime', |
---|
487 | n/a | '.ra' : 'audio/x-pn-realaudio', |
---|
488 | n/a | '.ram' : 'application/x-pn-realaudio', |
---|
489 | n/a | '.ras' : 'image/x-cmu-raster', |
---|
490 | n/a | '.rdf' : 'application/xml', |
---|
491 | n/a | '.rgb' : 'image/x-rgb', |
---|
492 | n/a | '.roff' : 'application/x-troff', |
---|
493 | n/a | '.rtx' : 'text/richtext', |
---|
494 | n/a | '.sgm' : 'text/x-sgml', |
---|
495 | n/a | '.sgml' : 'text/x-sgml', |
---|
496 | n/a | '.sh' : 'application/x-sh', |
---|
497 | n/a | '.shar' : 'application/x-shar', |
---|
498 | n/a | '.snd' : 'audio/basic', |
---|
499 | n/a | '.so' : 'application/octet-stream', |
---|
500 | n/a | '.src' : 'application/x-wais-source', |
---|
501 | n/a | '.sv4cpio': 'application/x-sv4cpio', |
---|
502 | n/a | '.sv4crc' : 'application/x-sv4crc', |
---|
503 | n/a | '.svg' : 'image/svg+xml', |
---|
504 | n/a | '.swf' : 'application/x-shockwave-flash', |
---|
505 | n/a | '.t' : 'application/x-troff', |
---|
506 | n/a | '.tar' : 'application/x-tar', |
---|
507 | n/a | '.tcl' : 'application/x-tcl', |
---|
508 | n/a | '.tex' : 'application/x-tex', |
---|
509 | n/a | '.texi' : 'application/x-texinfo', |
---|
510 | n/a | '.texinfo': 'application/x-texinfo', |
---|
511 | n/a | '.tif' : 'image/tiff', |
---|
512 | n/a | '.tiff' : 'image/tiff', |
---|
513 | n/a | '.tr' : 'application/x-troff', |
---|
514 | n/a | '.tsv' : 'text/tab-separated-values', |
---|
515 | n/a | '.txt' : 'text/plain', |
---|
516 | n/a | '.ustar' : 'application/x-ustar', |
---|
517 | n/a | '.vcf' : 'text/x-vcard', |
---|
518 | n/a | '.wav' : 'audio/x-wav', |
---|
519 | n/a | '.webm' : 'video/webm', |
---|
520 | n/a | '.wiz' : 'application/msword', |
---|
521 | n/a | '.wsdl' : 'application/xml', |
---|
522 | n/a | '.xbm' : 'image/x-xbitmap', |
---|
523 | n/a | '.xlb' : 'application/vnd.ms-excel', |
---|
524 | n/a | # Duplicates :( |
---|
525 | n/a | '.xls' : 'application/excel', |
---|
526 | n/a | '.xls' : 'application/vnd.ms-excel', |
---|
527 | n/a | '.xml' : 'text/xml', |
---|
528 | n/a | '.xpdl' : 'application/xml', |
---|
529 | n/a | '.xpm' : 'image/x-xpixmap', |
---|
530 | n/a | '.xsl' : 'application/xml', |
---|
531 | n/a | '.xwd' : 'image/x-xwindowdump', |
---|
532 | n/a | '.zip' : 'application/zip', |
---|
533 | n/a | } |
---|
534 | n/a | |
---|
535 | n/a | # These are non-standard types, commonly found in the wild. They will |
---|
536 | n/a | # only match if strict=0 flag is given to the API methods. |
---|
537 | n/a | |
---|
538 | n/a | # Please sort these too |
---|
539 | n/a | common_types = { |
---|
540 | n/a | '.jpg' : 'image/jpg', |
---|
541 | n/a | '.mid' : 'audio/midi', |
---|
542 | n/a | '.midi': 'audio/midi', |
---|
543 | n/a | '.pct' : 'image/pict', |
---|
544 | n/a | '.pic' : 'image/pict', |
---|
545 | n/a | '.pict': 'image/pict', |
---|
546 | n/a | '.rtf' : 'application/rtf', |
---|
547 | n/a | '.xul' : 'text/xul' |
---|
548 | n/a | } |
---|
549 | n/a | |
---|
550 | n/a | |
---|
551 | n/a | _default_mime_types() |
---|
552 | n/a | |
---|
553 | n/a | |
---|
554 | n/a | if __name__ == '__main__': |
---|
555 | n/a | import getopt |
---|
556 | n/a | |
---|
557 | n/a | USAGE = """\ |
---|
558 | n/a | Usage: mimetypes.py [options] type |
---|
559 | n/a | |
---|
560 | n/a | Options: |
---|
561 | n/a | --help / -h -- print this message and exit |
---|
562 | n/a | --lenient / -l -- additionally search of some common, but non-standard |
---|
563 | n/a | types. |
---|
564 | n/a | --extension / -e -- guess extension instead of type |
---|
565 | n/a | |
---|
566 | n/a | More than one type argument may be given. |
---|
567 | n/a | """ |
---|
568 | n/a | |
---|
569 | n/a | def usage(code, msg=''): |
---|
570 | n/a | print(USAGE) |
---|
571 | n/a | if msg: print(msg) |
---|
572 | n/a | sys.exit(code) |
---|
573 | n/a | |
---|
574 | n/a | try: |
---|
575 | n/a | opts, args = getopt.getopt(sys.argv[1:], 'hle', |
---|
576 | n/a | ['help', 'lenient', 'extension']) |
---|
577 | n/a | except getopt.error as msg: |
---|
578 | n/a | usage(1, msg) |
---|
579 | n/a | |
---|
580 | n/a | strict = 1 |
---|
581 | n/a | extension = 0 |
---|
582 | n/a | for opt, arg in opts: |
---|
583 | n/a | if opt in ('-h', '--help'): |
---|
584 | n/a | usage(0) |
---|
585 | n/a | elif opt in ('-l', '--lenient'): |
---|
586 | n/a | strict = 0 |
---|
587 | n/a | elif opt in ('-e', '--extension'): |
---|
588 | n/a | extension = 1 |
---|
589 | n/a | for gtype in args: |
---|
590 | n/a | if extension: |
---|
591 | n/a | guess = guess_extension(gtype, strict) |
---|
592 | n/a | if not guess: print("I don't know anything about type", gtype) |
---|
593 | n/a | else: print(guess) |
---|
594 | n/a | else: |
---|
595 | n/a | guess, encoding = guess_type(gtype, strict) |
---|
596 | n/a | if not guess: print("I don't know anything about type", gtype) |
---|
597 | n/a | else: print('type:', guess, 'encoding:', encoding) |
---|