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