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