blob: 3747fe1e1bd123dc19846a43e5d07fc8bbd695e6 [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
Barry Warsaw107771a2001-10-25 21:49:18 +000050\begin{funcdesc}{guess_extension}{type\optional{, strict}}
Fred Drakeb818b461998-05-19 15:03:45 +000051Guess the extension for a file based on its MIME type, given by
52\var{type}.
53The return value is a string giving a filename extension, including the
54leading dot (\character{.}). The extension is not guaranteed to have been
55associated with any particular data stream, but would be mapped to the
56MIME type \var{type} by \function{guess_type()}. If no extension can
57be guessed for \var{type}, \code{None} is returned.
Barry Warsaw107771a2001-10-25 21:49:18 +000058
59Optional \var{strict} has the same meaning as with the
60\function{guess_type()} function.
Fred Drakeb818b461998-05-19 15:03:45 +000061\end{funcdesc}
62
63
64Some additional functions and data items are available for controlling
65the behavior of the module.
66
67
68\begin{funcdesc}{init}{\optional{files}}
69Initialize the internal data structures. If given, \var{files} must
70be a sequence of file names which should be used to augment the
71default type map. If omitted, the file names to use are taken from
Fred Draked86038d2001-08-03 18:39:36 +000072\constant{knownfiles}. Each file named in \var{files} or
73\constant{knownfiles} takes precedence over those named before it.
Fred Drakeb818b461998-05-19 15:03:45 +000074Calling \function{init()} repeatedly is allowed.
75\end{funcdesc}
76
77\begin{funcdesc}{read_mime_types}{filename}
78Load the type map given in the file \var{filename}, if it exists. The
79type map is returned as a dictionary mapping filename extensions,
80including the leading dot (\character{.}), to strings of the form
81\code{'\var{type}/\var{subtype}'}. If the file \var{filename} does
82not exist or cannot be read, \code{None} is returned.
83\end{funcdesc}
84
85
86\begin{datadesc}{inited}
87Flag indicating whether or not the global data structures have been
88initialized. This is set to true by \function{init()}.
89\end{datadesc}
90
91\begin{datadesc}{knownfiles}
92List of type map file names commonly installed. These files are
Fred Drake86bd5e41999-04-23 16:02:30 +000093typically named \file{mime.types} and are installed in different
94locations by different packages.\index{file!mime.types}
Fred Drakeb818b461998-05-19 15:03:45 +000095\end{datadesc}
96
97\begin{datadesc}{suffix_map}
98Dictionary mapping suffixes to suffixes. This is used to allow
99recognition of encoded files for which the encoding and the type are
100indicated by the same extension. For example, the \file{.tgz}
101extension is mapped to \file{.tar.gz} to allow the encoding and type
102to be recognized separately.
103\end{datadesc}
104
105\begin{datadesc}{encodings_map}
106Dictionary mapping filename extensions to encoding types.
107\end{datadesc}
108
109\begin{datadesc}{types_map}
110Dictionary mapping filename extensions to MIME types.
111\end{datadesc}
Fred Draked5efb172001-08-03 21:03:14 +0000112
Barry Warsaw107771a2001-10-25 21:49:18 +0000113\begin{datadesc}{common_types}
114Dictionary mapping filename extensions to non-standard, but commonly
115found MIME types.
116\end{datadesc}
117
Fred Draked5efb172001-08-03 21:03:14 +0000118
119The \class{MimeTypes} class may be useful for applications which may
120want more than one MIME-type database:
121
122\begin{classdesc}{MimeTypes}{\optional{filenames}}
123 This class represents a MIME-types database. By default, it
124 provides access to the same database as the rest of this module.
125 The initial database is a copy of that provided by the module, and
126 may be extended by loading additional \file{mime.types}-style files
127 into the database using the \method{read()} or \method{readfp()}
128 methods. The mapping dictionaries may also be cleared before
129 loading additional data if the default data is not desired.
130
131 The optional \var{filenames} parameter can be used to cause
132 additional files to be loaded ``on top'' of the default database.
Fred Drakeb3cc29b2001-08-04 00:48:49 +0000133
134 \versionadded{2.2}
Fred Draked5efb172001-08-03 21:03:14 +0000135\end{classdesc}
136
137
138\subsection{MimeTypes Objects \label{mimetypes-objects}}
139
140\class{MimeTypes} instances provide an interface which is very like
141that of the \refmodule{mimetypes} module.
142
143\begin{datadesc}{suffix_map}
144 Dictionary mapping suffixes to suffixes. This is used to allow
145 recognition of encoded files for which the encoding and the type are
146 indicated by the same extension. For example, the \file{.tgz}
147 extension is mapped to \file{.tar.gz} to allow the encoding and type
148 to be recognized separately. This is initially a copy of the global
149 \code{suffix_map} defined in the module.
150\end{datadesc}
151
152\begin{datadesc}{encodings_map}
153 Dictionary mapping filename extensions to encoding types. This is
154 initially a copy of the global \code{encodings_map} defined in the
155 module.
156\end{datadesc}
157
158\begin{datadesc}{types_map}
159 Dictionary mapping filename extensions to MIME types. This is
160 initially a copy of the global \code{types_map} defined in the
161 module.
162\end{datadesc}
163
Barry Warsaw107771a2001-10-25 21:49:18 +0000164\begin{datadesc}{common_types}
165 Dictionary mapping filename extensions to non-standard, but commonly
166 found MIME types. This is initially a copy of the global
167 \code{common_types} defined in the module.
168\end{datadesc}
169
170\begin{methoddesc}{guess_extension}{type\optional{, strict}}
Fred Draked5efb172001-08-03 21:03:14 +0000171 Similar to the \function{guess_extension()} function, using the
172 tables stored as part of the object.
173\end{methoddesc}
174
Barry Warsaw107771a2001-10-25 21:49:18 +0000175\begin{methoddesc}{guess_type}{url\optional{, strict}}
Fred Draked5efb172001-08-03 21:03:14 +0000176 Similar to the \function{guess_type()} function, using the tables
177 stored as part of the object.
178\end{methoddesc}
179
180\begin{methoddesc}{read}{path}
181 Load MIME information from a file named \var{path}. This uses
182 \method{readfp()} to parse the file.
183\end{methoddesc}
184
185\begin{methoddesc}{readfp}{file}
186 Load MIME type information from an open file. The file must have
187 the format of the standard \file{mime.types} files.
188\end{methoddesc}