blob: e1168ec27063d39259ee624ef09fa96798fb085e [file] [log] [blame]
Guido van Rossum20af95b1997-03-25 22:01:35 +00001\section{Standard Module \sectcode{mailcap}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-mailcap}
Fred Drakec5891241998-02-09 19:16:20 +00003\rfcindex{1524}
Guido van Rossum20af95b1997-03-25 22:01:35 +00004\stmodindex{mailcap}
5\renewcommand{\indexsubitem}{(in module mailcap)}
6
7Mailcap files are used to configure how MIME-aware applications such
8as mail readers and Web browsers react to files with different MIME
9types. (The name ``mailcap'' is derived from the phrase ``mail
10capability''.) For example, a mailcap file might contain a line like
Fred Drake1656d171997-12-29 16:55:50 +000011\samp{video/mpeg; xmpeg \%s}. Then, if the user encounters an email
12message or Web document with the MIME type video/mpeg, \code{\%s} will be
Guido van Rossum20af95b1997-03-25 22:01:35 +000013replaced by a filename (usually one belonging to a temporary file) and
14the xmpeg program can be automatically started to view the file.
15
Fred Drakec5891241998-02-09 19:16:20 +000016The mailcap format is documented in \rfc{1524}, ``A User Agent
Guido van Rossum20af95b1997-03-25 22:01:35 +000017Configuration Mechanism For Multimedia Mail Format Information'', but
18is not an Internet standard. However, mailcap files are supported on
Fred Drake6862b461998-01-13 19:03:36 +000019most \UNIX{} systems.
Guido van Rossum20af95b1997-03-25 22:01:35 +000020
21\begin{funcdesc}{findmatch}{caps\, MIMEtype\, key\, filename\, plist}
22Return a 2-tuple; the first element is a string containing the command
23line to be executed
24(which can be passed to \code{os.system()}), and the second element is
25the mailcap entry for a given MIME type. If no matching MIME
26type can be found, \code{(None, None)} is returned.
27
Fred Drakec5891241998-02-09 19:16:20 +000028\var{key} is the name of the field desired, which represents the type
29of activity to be performed; the default value is 'view', since in the
Guido van Rossum20af95b1997-03-25 22:01:35 +000030most common case you simply want to view the body of the MIME-typed
31data. Other possible values might be 'compose' and 'edit', if you
32wanted to create a new body of the given MIME type or alter the
Fred Drakec5891241998-02-09 19:16:20 +000033existing body data. See \rfc{1524} for a complete list of these
34fields.
Guido van Rossum20af95b1997-03-25 22:01:35 +000035
36\var{filename} is the filename to be substituted for \%s in the
37command line; the default value is
38\file{/dev/null} which is almost certainly not what you want, so
39usually you'll override it by specifying a filename.
40
41\var{plist} can be a list containing named parameters; the default
42value is simply an empty list. Each entry in the list must be a
Fred Drake1656d171997-12-29 16:55:50 +000043string containing the parameter name, an equals sign (\code{=}), and the
Guido van Rossum20af95b1997-03-25 22:01:35 +000044parameter's value. Mailcap entries can contain
Fred Drake1656d171997-12-29 16:55:50 +000045named parameters like \code{\%\{foo\}}, which will be replaced by the
Guido van Rossum20af95b1997-03-25 22:01:35 +000046value of the parameter named 'foo'. For example, if the command line
Fred Drake1656d171997-12-29 16:55:50 +000047\samp{showpartial \%\{id\} \%\{number\} \%\{total\}}
Guido van Rossum20af95b1997-03-25 22:01:35 +000048was in a mailcap file, and \var{plist} was set to \code{['id=1',
49'number=2', 'total=3']}, the resulting command line would be
50\code{"showpartial 1 2 3"}.
51
52In a mailcap file, the "test" field can optionally be specified to
53test some external condition (e.g., the machine architecture, or the
54window system in use) to determine whether or not the mailcap line
55applies. \code{findmatch()} will automatically check such conditions
56and skip the entry if the check fails.
57\end{funcdesc}
58
59\begin{funcdesc}{getcaps}{}
60Returns a dictionary mapping MIME types to a list of mailcap file
Fred Drake1656d171997-12-29 16:55:50 +000061entries. This dictionary must be passed to the \code{findmatch()}
Guido van Rossum20af95b1997-03-25 22:01:35 +000062function. An entry is stored as a list of dictionaries, but it
63shouldn't be necessary to know the details of this representation.
64
65The information is derived from all of the mailcap files found on the
66system. Settings in the user's mailcap file \file{\$HOME/.mailcap}
67will override settings in the system mailcap files
68\file{/etc/mailcap}, \file{/usr/etc/mailcap}, and
69\file{/usr/local/etc/mailcap}.
70\end{funcdesc}
71
72An example usage:
Guido van Rossume47da0a1997-07-17 16:34:52 +000073\bcode\begin{verbatim}
Guido van Rossum20af95b1997-03-25 22:01:35 +000074>>> import mailcap
75>>> d=mailcap.getcaps()
76>>> mailcap.findmatch(d, 'video/mpeg', filename='/tmp/tmp1223')
77('xmpeg /tmp/tmp1223', {'view': 'xmpeg %s'})
Guido van Rossume47da0a1997-07-17 16:34:52 +000078\end{verbatim}\ecode