| 1 | """Mailcap file handling. See RFC 1524."""
|
|---|
| 2 |
|
|---|
| 3 | import os
|
|---|
| 4 |
|
|---|
| 5 | __all__ = ["getcaps","findmatch"]
|
|---|
| 6 |
|
|---|
| 7 | # Part 1: top-level interface.
|
|---|
| 8 |
|
|---|
| 9 | def getcaps():
|
|---|
| 10 | """Return a dictionary containing the mailcap database.
|
|---|
| 11 |
|
|---|
| 12 | The dictionary maps a MIME type (in all lowercase, e.g. 'text/plain')
|
|---|
| 13 | to a list of dictionaries corresponding to mailcap entries. The list
|
|---|
| 14 | collects all the entries for that MIME type from all available mailcap
|
|---|
| 15 | files. Each dictionary contains key-value pairs for that MIME type,
|
|---|
| 16 | where the viewing command is stored with the key "view".
|
|---|
| 17 |
|
|---|
| 18 | """
|
|---|
| 19 | caps = {}
|
|---|
| 20 | for mailcap in listmailcapfiles():
|
|---|
| 21 | try:
|
|---|
| 22 | fp = open(mailcap, 'r')
|
|---|
| 23 | except IOError:
|
|---|
| 24 | continue
|
|---|
| 25 | morecaps = readmailcapfile(fp)
|
|---|
| 26 | fp.close()
|
|---|
| 27 | for key, value in morecaps.iteritems():
|
|---|
| 28 | if not key in caps:
|
|---|
| 29 | caps[key] = value
|
|---|
| 30 | else:
|
|---|
| 31 | caps[key] = caps[key] + value
|
|---|
| 32 | return caps
|
|---|
| 33 |
|
|---|
| 34 | def listmailcapfiles():
|
|---|
| 35 | """Return a list of all mailcap files found on the system."""
|
|---|
| 36 | # XXX Actually, this is Unix-specific
|
|---|
| 37 | if 'MAILCAPS' in os.environ:
|
|---|
| 38 | str = os.environ['MAILCAPS']
|
|---|
| 39 | mailcaps = str.split(':')
|
|---|
| 40 | else:
|
|---|
| 41 | if 'HOME' in os.environ:
|
|---|
| 42 | home = os.environ['HOME']
|
|---|
| 43 | else:
|
|---|
| 44 | # Don't bother with getpwuid()
|
|---|
| 45 | home = '.' # Last resort
|
|---|
|
|---|