|
| 1 | +% This document section was written by Fred L. Drake, Jr. |
| 2 | +% <[email protected]>, based in part on original docstrings in the |
| 3 | +% mimetypes module. |
| 4 | + |
| 5 | +\section{Standard Module \module{mimetypes}} |
| 6 | +\label{module-mimetypes} |
| 7 | +\stmodindex{mimetypes} |
| 8 | +\indexii{MIME}{content type} |
| 9 | + |
| 10 | +The \module{mimetypes} converts between a filename or URL and the MIME |
| 11 | +type associated with the filename extension. Conversions are provided |
| 12 | +from filename to MIME type and from MIME type to filename extension; |
| 13 | +encodings are not supported for the later conversion. |
| 14 | + |
| 15 | +The functions described below provide the primary interface for this |
| 16 | +module. If the module has not been initialized, they will call the |
| 17 | +\function{init()}. |
| 18 | + |
| 19 | + |
| 20 | +\begin{funcdesc}{guess_type}{filename} |
| 21 | +Guess the type of a file based on its filename or URL, given by |
| 22 | +\var{filename}. |
| 23 | +The return value is a tuple \code{(\var{type}, \var{encoding})} where |
| 24 | +\var{type} is \code{None} if the type can't be guessed (no or unknown |
| 25 | +suffix) or a string of the form \code{'\var{type}/\var{subtype}'}, |
| 26 | +usable for a MIME \code{content-type} header\indexii{MIME}{headers}; and |
| 27 | +encoding is \code{None} for no encoding or the name of the program used |
| 28 | +to encode (e.g. \program{compress} or \program{gzip}). The encoding |
| 29 | +is suitable for use as a \code{content-encoding} header, |
| 30 | +\emph{not} as a \code{content-transfer-encoding} header. The mappings |
| 31 | +are table driven. Encoding suffixes are case sensitive; type suffixes |
| 32 | +are first tried case sensitive, then case insensitive. |
| 33 | +\end{funcdesc} |
| 34 | + |
| 35 | +\begin{funcdesc}{guess_extension}{type} |
| 36 | +Guess the extension for a file based on its MIME type, given by |
| 37 | +\var{type}. |
| 38 | +The return value is a string giving a filename extension, including the |
| 39 | +leading dot (\character{.}). The extension is not guaranteed to have been |
| 40 | +associated with any particular data stream, but would be mapped to the |
| 41 | +MIME type \var{type} by \function{guess_type()}. If no extension can |
| 42 | +be guessed for \var{type}, \code{None} is returned. |
| 43 | +\end{funcdesc} |
| 44 | + |
| 45 | + |
| 46 | +Some additional functions and data items are available for controlling |
| 47 | +the behavior of the module. |
| 48 | + |
| 49 | + |
| 50 | +\begin{funcdesc}{init}{\optional{files}} |
| 51 | +Initialize the internal data structures. If given, \var{files} must |
| 52 | +be a sequence of file names which should be used to augment the |
| 53 | +default type map. If omitted, the file names to use are taken from |
| 54 | +\code{knownfiles}. Each file named in \var{files} or |
| 55 | +\code{knownfiles} takes precedence over those named before it. |
| 56 | +Calling \function{init()} repeatedly is allowed. |
| 57 | +\end{funcdesc} |
| 58 | + |
| 59 | +\begin{funcdesc}{read_mime_types}{filename} |
| 60 | +Load the type map given in the file \var{filename}, if it exists. The |
| 61 | +type map is returned as a dictionary mapping filename extensions, |
| 62 | +including the leading dot (\character{.}), to strings of the form |
| 63 | +\code{'\var{type}/\var{subtype}'}. If the file \var{filename} does |
| 64 | +not exist or cannot be read, \code{None} is returned. |
| 65 | +\end{funcdesc} |
| 66 | + |
| 67 | + |
| 68 | +\begin{datadesc}{inited} |
| 69 | +Flag indicating whether or not the global data structures have been |
| 70 | +initialized. This is set to true by \function{init()}. |
| 71 | +\end{datadesc} |
| 72 | + |
| 73 | +\begin{datadesc}{knownfiles} |
| 74 | +List of type map file names commonly installed. These files are |
| 75 | +typically names \file{mime.types} and are installed in different |
| 76 | +locations by different packages.% |
| 77 | +\index{file!mime.types} |
| 78 | +\end{datadesc} |
| 79 | + |
| 80 | +\begin{datadesc}{suffix_map} |
| 81 | +Dictionary mapping suffixes to suffixes. This is used to allow |
| 82 | +recognition of encoded files for which the encoding and the type are |
| 83 | +indicated by the same extension. For example, the \file{.tgz} |
| 84 | +extension is mapped to \file{.tar.gz} to allow the encoding and type |
| 85 | +to be recognized separately. |
| 86 | +\end{datadesc} |
| 87 | + |
| 88 | +\begin{datadesc}{encodings_map} |
| 89 | +Dictionary mapping filename extensions to encoding types. |
| 90 | +\end{datadesc} |
| 91 | + |
| 92 | +\begin{datadesc}{types_map} |
| 93 | +Dictionary mapping filename extensions to MIME types. |
| 94 | +\end{datadesc} |
0 commit comments