blob: 8e07768f75cb03d622da0d7542f3e06204eab40c [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{mimetypes} ---
Fred Drake86bd5e41999-04-23 16:02:30 +00002 Map filenames to MIME types}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake180b68b1999-02-22 13:45:09 +00004\declaremodule{standard}{mimetypes}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Mapping of filename extensions to MIME types.}
Fred Drake86bd5e41999-04-23 16:02:30 +00006\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Fred Drake180b68b1999-02-22 13:45:09 +00008
Fred Drakeb818b461998-05-19 15:03:45 +00009\indexii{MIME}{content type}
10
Barry Warsaw107771a2001-10-25 21:49:18 +000011The \module{mimetypes} module converts between a filename or URL and
12the MIME type associated with the filename extension. Conversions are
13provided from filename to MIME type and from MIME type to filename
14extension; encodings are not supported for the latter conversion.
Fred Drakeb818b461998-05-19 15:03:45 +000015
Fred Draked5efb172001-08-03 21:03:14 +000016The module provides one class and a number of convenience functions.
17The functions are the normal interface to this module, but some
18applications may be interested in the class as well.
19
Fred Drakeb818b461998-05-19 15:03:45 +000020The functions described below provide the primary interface for this
Fred Drake86bd5e41999-04-23 16:02:30 +000021module. If the module has not been initialized, they will call
Fred Draked5efb172001-08-03 21:03:14 +000022\function{init()} if they rely on the information \function{init()}
23sets up.
Fred Drakeb818b461998-05-19 15:03:45 +000024
25
Barry Warsaw107771a2001-10-25 21:49:18 +000026\begin{funcdesc}{guess_type}{filename\optional{, strict}}
Fred Drakeb818b461998-05-19 15:03:45 +000027Guess the type of a file based on its filename or URL, given by
Fred Draked86038d2001-08-03 18:39:36 +000028\var{filename}. The return value is a tuple \code{(\var{type},
29\var{encoding})} where \var{type} is \code{None} if the type can't be
Barry Warsaw107771a2001-10-25 21:49:18 +000030guessed (missing or unknown suffix) or a string of the form
Fred Draked86038d2001-08-03 18:39:36 +000031\code{'\var{type}/\var{subtype}'}, usable for a MIME
Barry Warsaw107771a2001-10-25 21:49:18 +000032\mailheader{content-type} header\indexii{MIME}{headers}.
33
34\var{encoding} is \code{None} for no encoding or the name of the
35program used to encode (e.g. \program{compress} or \program{gzip}).
36The encoding is suitable for use as a \mailheader{Content-Encoding}
37header, \emph{not} as a \mailheader{Content-Transfer-Encoding} header.
38The mappings are table driven. Encoding suffixes are case sensitive;
39type suffixes are first tried case sensitively, then case
40insensitively.
41
42Optional \var{strict} is a flag specifying whether the list of known
43MIME types is limited to only the official types \ulink{registered
44with IANA}{http://www.isi.edu/in-notes/iana/assignments/media-types}
45are recognized. When \var{strict} is true (the default), only the
46IANA types are supported; when \var{strict} is false, some additional
47non-standard but commonly used MIME types are also recognized.
Fred Drakeb818b461998-05-19 15:03:45 +000048\end{funcdesc}
49
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +000050\begin{funcdesc}{guess_all_extensions}{type\optional{, strict}}
51Guess the extensions for a file based on its MIME type, given by
52\var{type}.
53The return value is a list of strings giving all possible filename extensions,
54including the leading dot (\character{.}). The extensions are not guaranteed
55to have been associated with any particular data stream, but would be mapped
Barry Warsaw9caa0d12003-06-09 22:27:41 +000056to the MIME type \var{type} by \function{guess_type()}.
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +000057
58Optional \var{strict} has the same meaning as with the
59\function{guess_type()} function.
60\end{funcdesc}
61
62
Barry Warsaw107771a2001-10-25 21:49:18 +000063\begin{funcdesc}{guess_extension}{type\optional{, strict}}
Fred Drakeb818b461998-05-19 15:03:45 +000064Guess the extension for a file based on its MIME type, given by
65\var{type}.
66The return value is a string giving a filename extension, including the
67leading dot (\character{.}). The extension is not guaranteed to have been
68associated with any particular data stream, but would be mapped to the
69MIME type \var{type} by \function{guess_type()}. If no extension can
70be guessed for \var{type}, \code{None} is returned.
Barry Warsaw107771a2001-10-25 21:49:18 +000071
72Optional \var{strict} has the same meaning as with the
73\function{guess_type()} function.
Fred Drakeb818b461998-05-19 15:03:45 +000074\end{funcdesc}
75
76
77Some additional functions and data items are available for controlling
78the behavior of the module.
79
80
81\begin{funcdesc}{init}{\optional{files}}
82Initialize the internal data structures. If given, \var{files} must
83be a sequence of file names which should be used to augment the
84default type map. If omitted, the file names to use are taken from
Fred Draked86038d2001-08-03 18:39:36 +000085\constant{knownfiles}. Each file named in \var{files} or
86\constant{knownfiles} takes precedence over those named before it.
Fred Drakeb818b461998-05-19 15:03:45 +000087Calling \function{init()} repeatedly is allowed.
88\end{funcdesc}
89
90\begin{funcdesc}{read_mime_types}{filename}
91Load the type map given in the file \var{filename}, if it exists. The
92type map is returned as a dictionary mapping filename extensions,
93including the leading dot (\character{.}), to strings of the form
94\code{'\var{type}/\var{subtype}'}. If the file \var{filename} does
95not exist or cannot be read, \code{None} is returned.
96\end{funcdesc}
97
98
Walter Dörwald5ccaf8f2002-09-06 16:15:58 +000099\begin{funcdesc}{add_type}{type, ext\optional{, strict}}
100Add a mapping from the mimetype \var{type} to the extension \var{ext}.
101When the extension is already known, the new type will replace the old
102one. When the type is already known the extension will be added
103to the list of known extensions.
104
105When \var{strict} is the mapping will added to the official
106MIME types, otherwise to the non-standard ones.
107\end{funcdesc}
108
109
Fred Drakeb818b461998-05-19 15:03:45 +0000110\begin{datadesc}{inited}
111Flag indicating whether or not the global data structures have been
112initialized. This is set to true by \function{init()}.
113\end{datadesc}
114
115\begin{datadesc}{knownfiles}
116List of type map file names commonly installed. These files are
Fred Drake86bd5e41999-04-23 16:02:30 +0000117typically named \file{mime.types} and are installed in different
118locations by different packages.\index{file!mime.types}
Fred Drakeb818b461998-05-19 15:03:45 +0000119\end{datadesc}
120
121\begin{datadesc}{suffix_map}
122Dictionary mapping suffixes to suffixes. This is used to allow
123recognition of encoded files for which the encoding and the type are
124indicated by the same extension. For example, the \file{.tgz}
125extension is mapped to \file{.tar.gz} to allow the encoding and type
126to be recognized separately.
127\end{datadesc}
128
129\begin{datadesc}{encodings_map}
130Dictionary mapping filename extensions to encoding types.
131\end{datadesc}
132
133\begin{datadesc}{types_map}
134Dictionary mapping filename extensions to MIME types.
135\end{datadesc}
Fred Draked5efb172001-08-03 21:03:14 +0000136
Barry Warsaw107771a2001-10-25 21:49:18 +0000137\begin{datadesc}{common_types}
138Dictionary mapping filename extensions to non-standard, but commonly
139found MIME types.
140\end{datadesc}
141
Fred Draked5efb172001-08-03 21:03:14 +0000142
143The \class{MimeTypes} class may be useful for applications which may
144want more than one MIME-type database:
145
146\begin{classdesc}{MimeTypes}{\optional{filenames}}
147 This class represents a MIME-types database. By default, it
148 provides access to the same database as the rest of this module.
149 The initial database is a copy of that provided by the module, and
150 may be extended by loading additional \file{mime.types}-style files
151 into the database using the \method{read()} or \method{readfp()}
152 methods. The mapping dictionaries may also be cleared before
153 loading additional data if the default data is not desired.
154
155 The optional \var{filenames} parameter can be used to cause
156 additional files to be loaded ``on top'' of the default database.
Fred Drakeb3cc29b2001-08-04 00:48:49 +0000157
158 \versionadded{2.2}
Fred Draked5efb172001-08-03 21:03:14 +0000159\end{classdesc}
160
161
162\subsection{MimeTypes Objects \label{mimetypes-objects}}
163
164\class{MimeTypes} instances provide an interface which is very like
165that of the \refmodule{mimetypes} module.
166
167\begin{datadesc}{suffix_map}
168 Dictionary mapping suffixes to suffixes. This is used to allow
169 recognition of encoded files for which the encoding and the type are
170 indicated by the same extension. For example, the \file{.tgz}
171 extension is mapped to \file{.tar.gz} to allow the encoding and type
172 to be recognized separately. This is initially a copy of the global
173 \code{suffix_map} defined in the module.
174\end{datadesc}
175
176\begin{datadesc}{encodings_map}
177 Dictionary mapping filename extensions to encoding types. This is
178 initially a copy of the global \code{encodings_map} defined in the
179 module.
180\end{datadesc}
181
182\begin{datadesc}{types_map}
183 Dictionary mapping filename extensions to MIME types. This is
184 initially a copy of the global \code{types_map} defined in the
185 module.
186\end{datadesc}
187
Barry Warsaw107771a2001-10-25 21:49:18 +0000188\begin{datadesc}{common_types}
189 Dictionary mapping filename extensions to non-standard, but commonly
190 found MIME types. This is initially a copy of the global
191 \code{common_types} defined in the module.
192\end{datadesc}
193
194\begin{methoddesc}{guess_extension}{type\optional{, strict}}
Fred Draked5efb172001-08-03 21:03:14 +0000195 Similar to the \function{guess_extension()} function, using the
196 tables stored as part of the object.
197\end{methoddesc}
198
Barry Warsaw107771a2001-10-25 21:49:18 +0000199\begin{methoddesc}{guess_type}{url\optional{, strict}}
Fred Draked5efb172001-08-03 21:03:14 +0000200 Similar to the \function{guess_type()} function, using the tables
201 stored as part of the object.
202\end{methoddesc}
203
204\begin{methoddesc}{read}{path}
205 Load MIME information from a file named \var{path}. This uses
206 \method{readfp()} to parse the file.
207\end{methoddesc}
208
209\begin{methoddesc}{readfp}{file}
210 Load MIME type information from an open file. The file must have
211 the format of the standard \file{mime.types} files.
212\end{methoddesc}