Map filenames to MIME types
===========================
Mapping of filename extensions to MIME types.
This manual section was written by Fred L. Drake, Jr. <fdrake@acm.org>.
The `mimetypes' converts between a filename or URL and the MIME type
associated with the filename extension. Conversions are provided from
filename to MIME type and from MIME type to filename extension;
encodings are not supported for the later conversion.
The functions described below provide the primary interface for this
module. If the module has not been initialized, they will call
`init()'.
`guess_type(filename)'
Guess the type of a file based on its filename or URL, given by
FILENAME. The return value is a tuple `(TYPE, ENCODING)' where
TYPE is `None' if the type can't be guessed (no or unknown suffix)
or a string of the form `'TYPE/SUBTYPE'', usable for a MIME
`content-type' header ; and encoding is `None' for no encoding or
the name of the program used to encode (e.g. `compress' or
`gzip'). The encoding is suitable for use as a `content-encoding'
header, _not_ as a `content-transfer-encoding' header. The
mappings are table driven. Encoding suffixes are case sensitive;
type suffixes are first tried case sensitive, then case
insensitive.
`guess_extension(type)'
Guess the extension for a file based on its MIME type, given by
TYPE. The return value is a string giving a filename extension,
including the leading dot (`.'). The extension is not guaranteed
to have been associated with any particular data stream, but would
be mapped to the MIME type TYPE by `guess_type()'. If no
extension can be guessed for TYPE, `None' is returned.
Some additional functions and data items are available for controlling
the behavior of the module.
`init([files])'
Initialize the internal data structures. If given, FILES must be
a sequence of file names which should be used to augment the
default type map. If omitted, the file names to use are taken from
`knownfiles'. Each file named in FILES or `knownfiles' takes
precedence over those named before it. Calling `init()'
repeatedly is allowed.
`read_mime_types(filename)'
Load the type map given in the file FILENAME, if it exists. The
type map is returned as a dictionary mapping filename extensions,
including the leading dot (`.'), to strings of the form
`'TYPE/SUBTYPE''. If the file FILENAME does not exist or cannot
be read, `None' is returned.
`inited'
Flag indicating whether or not the global data structures have been
initialized. This is set to true by `init()'.
`knownfiles'
List of type map file names commonly installed. These files are
typically named `mime.types' and are installed in different
locations by different packages.
`suffix_map'
Dictionary mapping suffixes to suffixes. This is used to allow
recognition of encoded files for which the encoding and the type
are indicated by the same extension. For example, the `.tgz'
extension is mapped to `.tar.gz' to allow the encoding and type to
be recognized separately.
`encodings_map'
Dictionary mapping filename extensions to encoding types.
`types_map'
Dictionary mapping filename extensions to MIME types.