Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Doc / lib / libmimetypes.tex
blob64296f0301232073a5e3708729e3c8884aea037d
1 \section{\module{mimetypes} ---
2 Map filenames to MIME types}
4 \declaremodule{standard}{mimetypes}
5 \modulesynopsis{Mapping of filename extensions to MIME types.}
6 \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
9 \indexii{MIME}{content type}
11 The \module{mimetypes} module converts between a filename or URL and
12 the MIME type associated with the filename extension. Conversions are
13 provided from filename to MIME type and from MIME type to filename
14 extension; encodings are not supported for the latter conversion.
16 The module provides one class and a number of convenience functions.
17 The functions are the normal interface to this module, but some
18 applications may be interested in the class as well.
20 The functions described below provide the primary interface for this
21 module. If the module has not been initialized, they will call
22 \function{init()} if they rely on the information \function{init()}
23 sets up.
26 \begin{funcdesc}{guess_type}{filename\optional{, strict}}
27 Guess the type of a file based on its filename or URL, given by
28 \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
30 guessed (missing or unknown suffix) or a string of the form
31 \code{'\var{type}/\var{subtype}'}, usable for a MIME
32 \mailheader{content-type} header\indexii{MIME}{headers}.
34 \var{encoding} is \code{None} for no encoding or the name of the
35 program used to encode (e.g. \program{compress} or \program{gzip}).
36 The encoding is suitable for use as a \mailheader{Content-Encoding}
37 header, \emph{not} as a \mailheader{Content-Transfer-Encoding} header.
38 The mappings are table driven. Encoding suffixes are case sensitive;
39 type suffixes are first tried case sensitively, then case
40 insensitively.
42 Optional \var{strict} is a flag specifying whether the list of known
43 MIME types is limited to only the official types \ulink{registered
44 with IANA}{http://www.isi.edu/in-notes/iana/assignments/media-types}
45 are recognized. When \var{strict} is true (the default), only the
46 IANA types are supported; when \var{strict} is false, some additional
47 non-standard but commonly used MIME types are also recognized.
48 \end{funcdesc}
50 \begin{funcdesc}{guess_all_extensions}{type\optional{, strict}}
51 Guess the extensions for a file based on its MIME type, given by
52 \var{type}.
53 The return value is a list of strings giving all possible filename extensions,
54 including the leading dot (\character{.}). The extensions are not guaranteed
55 to have been associated with any particular data stream, but would be mapped
56 to the MIME type \var{type} by \function{guess_type()}. If no extension can
57 be guessed for \var{type}, \code{None} is returned.
59 Optional \var{strict} has the same meaning as with the
60 \function{guess_type()} function.
61 \end{funcdesc}
64 \begin{funcdesc}{guess_extension}{type\optional{, strict}}
65 Guess the extension for a file based on its MIME type, given by
66 \var{type}.
67 The return value is a string giving a filename extension, including the
68 leading dot (\character{.}). The extension is not guaranteed to have been
69 associated with any particular data stream, but would be mapped to the
70 MIME type \var{type} by \function{guess_type()}. If no extension can
71 be guessed for \var{type}, \code{None} is returned.
73 Optional \var{strict} has the same meaning as with the
74 \function{guess_type()} function.
75 \end{funcdesc}
78 Some additional functions and data items are available for controlling
79 the behavior of the module.
82 \begin{funcdesc}{init}{\optional{files}}
83 Initialize the internal data structures. If given, \var{files} must
84 be a sequence of file names which should be used to augment the
85 default type map. If omitted, the file names to use are taken from
86 \constant{knownfiles}. Each file named in \var{files} or
87 \constant{knownfiles} takes precedence over those named before it.
88 Calling \function{init()} repeatedly is allowed.
89 \end{funcdesc}
91 \begin{funcdesc}{read_mime_types}{filename}
92 Load the type map given in the file \var{filename}, if it exists. The
93 type map is returned as a dictionary mapping filename extensions,
94 including the leading dot (\character{.}), to strings of the form
95 \code{'\var{type}/\var{subtype}'}. If the file \var{filename} does
96 not exist or cannot be read, \code{None} is returned.
97 \end{funcdesc}
100 \begin{funcdesc}{add_type}{type, ext\optional{, strict}}
101 Add a mapping from the mimetype \var{type} to the extension \var{ext}.
102 When the extension is already known, the new type will replace the old
103 one. When the type is already known the extension will be added
104 to the list of known extensions.
106 When \var{strict} is the mapping will added to the official
107 MIME types, otherwise to the non-standard ones.
108 \end{funcdesc}
111 \begin{datadesc}{inited}
112 Flag indicating whether or not the global data structures have been
113 initialized. This is set to true by \function{init()}.
114 \end{datadesc}
116 \begin{datadesc}{knownfiles}
117 List of type map file names commonly installed. These files are
118 typically named \file{mime.types} and are installed in different
119 locations by different packages.\index{file!mime.types}
120 \end{datadesc}
122 \begin{datadesc}{suffix_map}
123 Dictionary mapping suffixes to suffixes. This is used to allow
124 recognition of encoded files for which the encoding and the type are
125 indicated by the same extension. For example, the \file{.tgz}
126 extension is mapped to \file{.tar.gz} to allow the encoding and type
127 to be recognized separately.
128 \end{datadesc}
130 \begin{datadesc}{encodings_map}
131 Dictionary mapping filename extensions to encoding types.
132 \end{datadesc}
134 \begin{datadesc}{types_map}
135 Dictionary mapping filename extensions to MIME types.
136 \end{datadesc}
138 \begin{datadesc}{common_types}
139 Dictionary mapping filename extensions to non-standard, but commonly
140 found MIME types.
141 \end{datadesc}
144 The \class{MimeTypes} class may be useful for applications which may
145 want more than one MIME-type database:
147 \begin{classdesc}{MimeTypes}{\optional{filenames}}
148 This class represents a MIME-types database. By default, it
149 provides access to the same database as the rest of this module.
150 The initial database is a copy of that provided by the module, and
151 may be extended by loading additional \file{mime.types}-style files
152 into the database using the \method{read()} or \method{readfp()}
153 methods. The mapping dictionaries may also be cleared before
154 loading additional data if the default data is not desired.
156 The optional \var{filenames} parameter can be used to cause
157 additional files to be loaded ``on top'' of the default database.
159 \versionadded{2.2}
160 \end{classdesc}
163 \subsection{MimeTypes Objects \label{mimetypes-objects}}
165 \class{MimeTypes} instances provide an interface which is very like
166 that of the \refmodule{mimetypes} module.
168 \begin{datadesc}{suffix_map}
169 Dictionary mapping suffixes to suffixes. This is used to allow
170 recognition of encoded files for which the encoding and the type are
171 indicated by the same extension. For example, the \file{.tgz}
172 extension is mapped to \file{.tar.gz} to allow the encoding and type
173 to be recognized separately. This is initially a copy of the global
174 \code{suffix_map} defined in the module.
175 \end{datadesc}
177 \begin{datadesc}{encodings_map}
178 Dictionary mapping filename extensions to encoding types. This is
179 initially a copy of the global \code{encodings_map} defined in the
180 module.
181 \end{datadesc}
183 \begin{datadesc}{types_map}
184 Dictionary mapping filename extensions to MIME types. This is
185 initially a copy of the global \code{types_map} defined in the
186 module.
187 \end{datadesc}
189 \begin{datadesc}{common_types}
190 Dictionary mapping filename extensions to non-standard, but commonly
191 found MIME types. This is initially a copy of the global
192 \code{common_types} defined in the module.
193 \end{datadesc}
195 \begin{methoddesc}{guess_extension}{type\optional{, strict}}
196 Similar to the \function{guess_extension()} function, using the
197 tables stored as part of the object.
198 \end{methoddesc}
200 \begin{methoddesc}{guess_type}{url\optional{, strict}}
201 Similar to the \function{guess_type()} function, using the tables
202 stored as part of the object.
203 \end{methoddesc}
205 \begin{methoddesc}{read}{path}
206 Load MIME information from a file named \var{path}. This uses
207 \method{readfp()} to parse the file.
208 \end{methoddesc}
210 \begin{methoddesc}{readfp}{file}
211 Load MIME type information from an open file. The file must have
212 the format of the standard \file{mime.types} files.
213 \end{methoddesc}