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