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