Quick update to the README file. For intros and books we now point to
[python/dscho.git] / Doc / lib / libmimetypes.tex
blobc85eb6f112a2d08e804daf028d0092228b64a439
1 % This document section was written by Fred L. Drake, Jr.
2 % <fdrake@acm.org>, based in part on original docstrings in the
3 % mimetypes module.
5 \section{\module{mimetypes} ---
6 Map filenames to MIME types}
8 \declaremodule{standard}{mimetypes}
9 \modulesynopsis{Mapping of filename extensions to MIME types.}
10 \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
13 \indexii{MIME}{content type}
15 The \module{mimetypes} converts between a filename or URL and the MIME
16 type associated with the filename extension. Conversions are provided
17 from filename to MIME type and from MIME type to filename extension;
18 encodings are not supported for the later conversion.
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()}.
25 \begin{funcdesc}{guess_type}{filename}
26 Guess the type of a file based on its filename or URL, given by
27 \var{filename}.
28 The return value is a tuple \code{(\var{type}, \var{encoding})} where
29 \var{type} is \code{None} if the type can't be guessed (no or unknown
30 suffix) or a string of the form \code{'\var{type}/\var{subtype}'},
31 usable for a MIME \code{content-type} header\indexii{MIME}{headers}; and
32 encoding is \code{None} for no encoding or the name of the program used
33 to encode (e.g. \program{compress} or \program{gzip}). The encoding
34 is suitable for use as a \code{content-encoding} header,
35 \emph{not} as a \code{content-transfer-encoding} header. The mappings
36 are table driven. Encoding suffixes are case sensitive; type suffixes
37 are first tried case sensitive, then case insensitive.
38 \end{funcdesc}
40 \begin{funcdesc}{guess_extension}{type}
41 Guess the extension for a file based on its MIME type, given by
42 \var{type}.
43 The return value is a string giving a filename extension, including the
44 leading dot (\character{.}). The extension is not guaranteed to have been
45 associated with any particular data stream, but would be mapped to the
46 MIME type \var{type} by \function{guess_type()}. If no extension can
47 be guessed for \var{type}, \code{None} is returned.
48 \end{funcdesc}
51 Some additional functions and data items are available for controlling
52 the behavior of the module.
55 \begin{funcdesc}{init}{\optional{files}}
56 Initialize the internal data structures. If given, \var{files} must
57 be a sequence of file names which should be used to augment the
58 default type map. If omitted, the file names to use are taken from
59 \code{knownfiles}. Each file named in \var{files} or
60 \code{knownfiles} takes precedence over those named before it.
61 Calling \function{init()} repeatedly is allowed.
62 \end{funcdesc}
64 \begin{funcdesc}{read_mime_types}{filename}
65 Load the type map given in the file \var{filename}, if it exists. The
66 type map is returned as a dictionary mapping filename extensions,
67 including the leading dot (\character{.}), to strings of the form
68 \code{'\var{type}/\var{subtype}'}. If the file \var{filename} does
69 not exist or cannot be read, \code{None} is returned.
70 \end{funcdesc}
73 \begin{datadesc}{inited}
74 Flag indicating whether or not the global data structures have been
75 initialized. This is set to true by \function{init()}.
76 \end{datadesc}
78 \begin{datadesc}{knownfiles}
79 List of type map file names commonly installed. These files are
80 typically named \file{mime.types} and are installed in different
81 locations by different packages.\index{file!mime.types}
82 \end{datadesc}
84 \begin{datadesc}{suffix_map}
85 Dictionary mapping suffixes to suffixes. This is used to allow
86 recognition of encoded files for which the encoding and the type are
87 indicated by the same extension. For example, the \file{.tgz}
88 extension is mapped to \file{.tar.gz} to allow the encoding and type
89 to be recognized separately.
90 \end{datadesc}
92 \begin{datadesc}{encodings_map}
93 Dictionary mapping filename extensions to encoding types.
94 \end{datadesc}
96 \begin{datadesc}{types_map}
97 Dictionary mapping filename extensions to MIME types.
98 \end{datadesc}