Added a test for the ability to specify a class attribute in Formatter configuration...
[python.git] / Doc / lib / libmimetypes.tex
blob8e07768f75cb03d622da0d7542f3e06204eab40c
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()}.
58 Optional \var{strict} has the same meaning as with the
59 \function{guess_type()} function.
60 \end{funcdesc}
63 \begin{funcdesc}{guess_extension}{type\optional{, strict}}
64 Guess the extension for a file based on its MIME type, given by
65 \var{type}.
66 The return value is a string giving a filename extension, including the
67 leading dot (\character{.}). The extension is not guaranteed to have been
68 associated with any particular data stream, but would be mapped to the
69 MIME type \var{type} by \function{guess_type()}. If no extension can
70 be guessed for \var{type}, \code{None} is returned.
72 Optional \var{strict} has the same meaning as with the
73 \function{guess_type()} function.
74 \end{funcdesc}
77 Some additional functions and data items are available for controlling
78 the behavior of the module.
81 \begin{funcdesc}{init}{\optional{files}}
82 Initialize the internal data structures. If given, \var{files} must
83 be a sequence of file names which should be used to augment the
84 default type map. If omitted, the file names to use are taken from
85 \constant{knownfiles}. Each file named in \var{files} or
86 \constant{knownfiles} takes precedence over those named before it.
87 Calling \function{init()} repeatedly is allowed.
88 \end{funcdesc}
90 \begin{funcdesc}{read_mime_types}{filename}
91 Load the type map given in the file \var{filename}, if it exists. The
92 type map is returned as a dictionary mapping filename extensions,
93 including the leading dot (\character{.}), to strings of the form
94 \code{'\var{type}/\var{subtype}'}. If the file \var{filename} does
95 not exist or cannot be read, \code{None} is returned.
96 \end{funcdesc}
99 \begin{funcdesc}{add_type}{type, ext\optional{, strict}}
100 Add a mapping from the mimetype \var{type} to the extension \var{ext}.
101 When the extension is already known, the new type will replace the old
102 one. When the type is already known the extension will be added
103 to the list of known extensions.
105 When \var{strict} is the mapping will added to the official
106 MIME types, otherwise to the non-standard ones.
107 \end{funcdesc}
110 \begin{datadesc}{inited}
111 Flag indicating whether or not the global data structures have been
112 initialized. This is set to true by \function{init()}.
113 \end{datadesc}
115 \begin{datadesc}{knownfiles}
116 List of type map file names commonly installed. These files are
117 typically named \file{mime.types} and are installed in different
118 locations by different packages.\index{file!mime.types}
119 \end{datadesc}
121 \begin{datadesc}{suffix_map}
122 Dictionary mapping suffixes to suffixes. This is used to allow
123 recognition of encoded files for which the encoding and the type are
124 indicated by the same extension. For example, the \file{.tgz}
125 extension is mapped to \file{.tar.gz} to allow the encoding and type
126 to be recognized separately.
127 \end{datadesc}
129 \begin{datadesc}{encodings_map}
130 Dictionary mapping filename extensions to encoding types.
131 \end{datadesc}
133 \begin{datadesc}{types_map}
134 Dictionary mapping filename extensions to MIME types.
135 \end{datadesc}
137 \begin{datadesc}{common_types}
138 Dictionary mapping filename extensions to non-standard, but commonly
139 found MIME types.
140 \end{datadesc}
143 The \class{MimeTypes} class may be useful for applications which may
144 want more than one MIME-type database:
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.
155 The optional \var{filenames} parameter can be used to cause
156 additional files to be loaded ``on top'' of the default database.
158 \versionadded{2.2}
159 \end{classdesc}
162 \subsection{MimeTypes Objects \label{mimetypes-objects}}
164 \class{MimeTypes} instances provide an interface which is very like
165 that of the \refmodule{mimetypes} module.
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}
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}
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}
188 \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}
194 \begin{methoddesc}{guess_extension}{type\optional{, strict}}
195 Similar to the \function{guess_extension()} function, using the
196 tables stored as part of the object.
197 \end{methoddesc}
199 \begin{methoddesc}{guess_type}{url\optional{, strict}}
200 Similar to the \function{guess_type()} function, using the tables
201 stored as part of the object.
202 \end{methoddesc}
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}
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}