Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`mimetypes` --- Map filenames to MIME types |
| 2 | ================================================ |
| 3 | |
| 4 | .. module:: mimetypes |
| 5 | :synopsis: Mapping of filename extensions to MIME types. |
| 6 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 7 | |
| 8 | |
| 9 | .. index:: pair: MIME; content type |
| 10 | |
| 11 | The :mod:`mimetypes` module converts between a filename or URL and the MIME type |
| 12 | associated with the filename extension. Conversions are provided from filename |
| 13 | to MIME type and from MIME type to filename extension; encodings are not |
| 14 | supported for the latter conversion. |
| 15 | |
| 16 | The module provides one class and a number of convenience functions. The |
| 17 | functions are the normal interface to this module, but some applications may be |
| 18 | interested in the class as well. |
| 19 | |
| 20 | The functions described below provide the primary interface for this module. If |
| 21 | the module has not been initialized, they will call :func:`init` if they rely on |
| 22 | the information :func:`init` sets up. |
| 23 | |
| 24 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 25 | .. function:: guess_type(url, strict=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
| 27 | .. index:: pair: MIME; headers |
| 28 | |
| 29 | Guess the type of a file based on its filename or URL, given by *filename*. The |
| 30 | return value is a tuple ``(type, encoding)`` where *type* is ``None`` if the |
| 31 | type can't be guessed (missing or unknown suffix) or a string of the form |
| 32 | ``'type/subtype'``, usable for a MIME :mailheader:`content-type` header. |
| 33 | |
| 34 | *encoding* is ``None`` for no encoding or the name of the program used to encode |
| 35 | (e.g. :program:`compress` or :program:`gzip`). The encoding is suitable for use |
| 36 | as a :mailheader:`Content-Encoding` header, *not* as a |
| 37 | :mailheader:`Content-Transfer-Encoding` header. The mappings are table driven. |
| 38 | Encoding suffixes are case sensitive; type suffixes are first tried case |
| 39 | sensitively, then case insensitively. |
| 40 | |
| 41 | Optional *strict* is a flag specifying whether the list of known MIME types |
| 42 | is limited to only the official types `registered with IANA |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 43 | <http://www.iana.org/assignments/media-types/>`_ are recognized. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 44 | When *strict* is true (the default), only the IANA types are supported; when |
| 45 | *strict* is false, some additional non-standard but commonly used MIME types |
| 46 | are also recognized. |
| 47 | |
| 48 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 49 | .. function:: guess_all_extensions(type, strict=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
| 51 | Guess the extensions for a file based on its MIME type, given by *type*. The |
| 52 | return value is a list of strings giving all possible filename extensions, |
| 53 | including the leading dot (``'.'``). The extensions are not guaranteed to have |
| 54 | been associated with any particular data stream, but would be mapped to the MIME |
| 55 | type *type* by :func:`guess_type`. |
| 56 | |
| 57 | Optional *strict* has the same meaning as with the :func:`guess_type` function. |
| 58 | |
| 59 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 60 | .. function:: guess_extension(type, strict=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | |
| 62 | Guess the extension for a file based on its MIME type, given by *type*. The |
| 63 | return value is a string giving a filename extension, including the leading dot |
| 64 | (``'.'``). The extension is not guaranteed to have been associated with any |
| 65 | particular data stream, but would be mapped to the MIME type *type* by |
| 66 | :func:`guess_type`. If no extension can be guessed for *type*, ``None`` is |
| 67 | returned. |
| 68 | |
| 69 | Optional *strict* has the same meaning as with the :func:`guess_type` function. |
| 70 | |
| 71 | Some additional functions and data items are available for controlling the |
| 72 | behavior of the module. |
| 73 | |
| 74 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 75 | .. function:: init(files=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
| 77 | Initialize the internal data structures. If given, *files* must be a sequence |
| 78 | of file names which should be used to augment the default type map. If omitted, |
Antoine Pitrou | b8108e2 | 2009-11-15 14:25:16 +0000 | [diff] [blame] | 79 | the file names to use are taken from :const:`knownfiles`; on Windows, the |
| 80 | current registry settings are loaded. Each file named in *files* or |
| 81 | :const:`knownfiles` takes precedence over those named before it. Calling |
| 82 | :func:`init` repeatedly is allowed. |
| 83 | |
| 84 | .. versionchanged:: 3.2 |
| 85 | Previously, Windows registry settings were ignored. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | .. function:: read_mime_types(filename) |
| 89 | |
| 90 | Load the type map given in the file *filename*, if it exists. The type map is |
| 91 | returned as a dictionary mapping filename extensions, including the leading dot |
| 92 | (``'.'``), to strings of the form ``'type/subtype'``. If the file *filename* |
| 93 | does not exist or cannot be read, ``None`` is returned. |
| 94 | |
| 95 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 96 | .. function:: add_type(type, ext, strict=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | |
| 98 | Add a mapping from the mimetype *type* to the extension *ext*. When the |
| 99 | extension is already known, the new type will replace the old one. When the type |
| 100 | is already known the extension will be added to the list of known extensions. |
| 101 | |
Guido van Rossum | 87c0f1d | 2007-11-19 18:03:44 +0000 | [diff] [blame] | 102 | When *strict* is True (the default), the mapping will added to the official MIME |
| 103 | types, otherwise to the non-standard ones. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | .. data:: inited |
| 107 | |
| 108 | Flag indicating whether or not the global data structures have been initialized. |
| 109 | This is set to true by :func:`init`. |
| 110 | |
| 111 | |
| 112 | .. data:: knownfiles |
| 113 | |
| 114 | .. index:: single: file; mime.types |
| 115 | |
| 116 | List of type map file names commonly installed. These files are typically named |
| 117 | :file:`mime.types` and are installed in different locations by different |
| 118 | packages. |
| 119 | |
| 120 | |
| 121 | .. data:: suffix_map |
| 122 | |
| 123 | Dictionary mapping suffixes to suffixes. This is used to allow recognition of |
| 124 | encoded files for which the encoding and the type are indicated by the same |
| 125 | extension. For example, the :file:`.tgz` extension is mapped to :file:`.tar.gz` |
| 126 | to allow the encoding and type to be recognized separately. |
| 127 | |
| 128 | |
| 129 | .. data:: encodings_map |
| 130 | |
| 131 | Dictionary mapping filename extensions to encoding types. |
| 132 | |
| 133 | |
| 134 | .. data:: types_map |
| 135 | |
| 136 | Dictionary mapping filename extensions to MIME types. |
| 137 | |
| 138 | |
| 139 | .. data:: common_types |
| 140 | |
| 141 | Dictionary mapping filename extensions to non-standard, but commonly found MIME |
| 142 | types. |
| 143 | |
| 144 | The :class:`MimeTypes` class may be useful for applications which may want more |
| 145 | than one MIME-type database: |
| 146 | |
| 147 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 148 | .. class:: MimeTypes(filenames=(), strict=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 149 | |
| 150 | This class represents a MIME-types database. By default, it provides access to |
| 151 | the same database as the rest of this module. The initial database is a copy of |
| 152 | that provided by the module, and may be extended by loading additional |
| 153 | :file:`mime.types`\ -style files into the database using the :meth:`read` or |
| 154 | :meth:`readfp` methods. The mapping dictionaries may also be cleared before |
| 155 | loading additional data if the default data is not desired. |
| 156 | |
| 157 | The optional *filenames* parameter can be used to cause additional files to be |
| 158 | loaded "on top" of the default database. |
| 159 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 160 | |
| 161 | An example usage of the module:: |
| 162 | |
| 163 | >>> import mimetypes |
| 164 | >>> mimetypes.init() |
| 165 | >>> mimetypes.knownfiles |
| 166 | ['/etc/mime.types', '/etc/httpd/mime.types', ... ] |
| 167 | >>> mimetypes.suffix_map['.tgz'] |
| 168 | '.tar.gz' |
| 169 | >>> mimetypes.encodings_map['.gz'] |
| 170 | 'gzip' |
| 171 | >>> mimetypes.types_map['.tgz'] |
| 172 | 'application/x-tar-gz' |
| 173 | |
| 174 | |
| 175 | .. _mimetypes-objects: |
| 176 | |
| 177 | MimeTypes Objects |
| 178 | ----------------- |
| 179 | |
| 180 | :class:`MimeTypes` instances provide an interface which is very like that of the |
| 181 | :mod:`mimetypes` module. |
| 182 | |
| 183 | |
| 184 | .. attribute:: MimeTypes.suffix_map |
| 185 | |
| 186 | Dictionary mapping suffixes to suffixes. This is used to allow recognition of |
| 187 | encoded files for which the encoding and the type are indicated by the same |
| 188 | extension. For example, the :file:`.tgz` extension is mapped to :file:`.tar.gz` |
| 189 | to allow the encoding and type to be recognized separately. This is initially a |
| 190 | copy of the global ``suffix_map`` defined in the module. |
| 191 | |
| 192 | |
| 193 | .. attribute:: MimeTypes.encodings_map |
| 194 | |
| 195 | Dictionary mapping filename extensions to encoding types. This is initially a |
| 196 | copy of the global ``encodings_map`` defined in the module. |
| 197 | |
| 198 | |
| 199 | .. attribute:: MimeTypes.types_map |
| 200 | |
| 201 | Dictionary mapping filename extensions to MIME types. This is initially a copy |
| 202 | of the global ``types_map`` defined in the module. |
| 203 | |
| 204 | |
| 205 | .. attribute:: MimeTypes.common_types |
| 206 | |
| 207 | Dictionary mapping filename extensions to non-standard, but commonly found MIME |
| 208 | types. This is initially a copy of the global ``common_types`` defined in the |
| 209 | module. |
| 210 | |
| 211 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 212 | .. method:: MimeTypes.guess_extension(type, strict=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 213 | |
| 214 | Similar to the :func:`guess_extension` function, using the tables stored as part |
| 215 | of the object. |
| 216 | |
| 217 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 218 | .. method:: MimeTypes.guess_type(url, strict=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 219 | |
| 220 | Similar to the :func:`guess_type` function, using the tables stored as part of |
| 221 | the object. |
| 222 | |
| 223 | |
| 224 | .. method:: MimeTypes.read(path) |
| 225 | |
| 226 | Load MIME information from a file named *path*. This uses :meth:`readfp` to |
| 227 | parse the file. |
| 228 | |
| 229 | |
| 230 | .. method:: MimeTypes.readfp(file) |
| 231 | |
| 232 | Load MIME type information from an open file. The file must have the format of |
| 233 | the standard :file:`mime.types` files. |
| 234 | |
Antoine Pitrou | b8108e2 | 2009-11-15 14:25:16 +0000 | [diff] [blame] | 235 | |
| 236 | .. method:: MimeTypes.read_windows_registry() |
| 237 | |
| 238 | Load MIME type information from the Windows registry. Availability: Windows. |
| 239 | |
| 240 | .. versionadded:: 3.2 |