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