mailcap.py 7.26 KB
Newer Older
1
"""Mailcap file handling.  See RFC 1524."""
Guido van Rossum's avatar
Guido van Rossum committed
2 3 4

import os

5
__all__ = ["getcaps","findmatch"]
Guido van Rossum's avatar
Guido van Rossum committed
6 7 8 9

# Part 1: top-level interface.

def getcaps():
10
    """Return a dictionary containing the mailcap database.
Tim Peters's avatar
Tim Peters committed
11

12 13 14 15 16
    The dictionary maps a MIME type (in all lowercase, e.g. 'text/plain')
    to a list of dictionaries corresponding to mailcap entries.  The list
    collects all the entries for that MIME type from all available mailcap
    files.  Each dictionary contains key-value pairs for that MIME type,
    where the viewing command is stored with the key "view".
17 18

    """
Guido van Rossum's avatar
Guido van Rossum committed
19 20
    caps = {}
    for mailcap in listmailcapfiles():
21 22
        try:
            fp = open(mailcap, 'r')
23
        except IOError:
24 25 26
            continue
        morecaps = readmailcapfile(fp)
        fp.close()
27
        for key, value in morecaps.items():
28
            if not key in caps:
29
                caps[key] = value
30
            else:
31
                caps[key] = caps[key] + value
Guido van Rossum's avatar
Guido van Rossum committed
32 33 34
    return caps

def listmailcapfiles():
35
    """Return a list of all mailcap files found on the system."""
Guido van Rossum's avatar
Guido van Rossum committed
36
    # XXX Actually, this is Unix-specific
37
    if 'MAILCAPS' in os.environ:
38
        str = os.environ['MAILCAPS']
39
        mailcaps = str.split(':')
Guido van Rossum's avatar
Guido van Rossum committed
40
    else:
41
        if 'HOME' in os.environ:
42 43 44 45 46 47
            home = os.environ['HOME']
        else:
            # Don't bother with getpwuid()
            home = '.' # Last resort
        mailcaps = [home + '/.mailcap', '/etc/mailcap',
                '/usr/etc/mailcap', '/usr/local/etc/mailcap']
Guido van Rossum's avatar
Guido van Rossum committed
48 49 50 51 52 53
    return mailcaps


# Part 2: the parser.

def readmailcapfile(fp):
54 55 56 57 58 59 60 61
    """Read a mailcap file and return a dictionary keyed by MIME type.

    Each MIME type is mapped to an entry consisting of a list of
    dictionaries; the list will contain more than one such dictionary
    if a given MIME type appears more than once in the mailcap file.
    Each dictionary contains key-value pairs for that MIME type, where
    the viewing command is stored with the key "view".
    """
Guido van Rossum's avatar
Guido van Rossum committed
62 63
    caps = {}
    while 1:
64 65 66
        line = fp.readline()
        if not line: break
        # Ignore comments and blank lines
67
        if line[0] == '#' or line.strip() == '':
68 69 70 71 72 73 74 75 76 77 78 79
            continue
        nextline = line
        # Join continuation lines
        while nextline[-2:] == '\\\n':
            nextline = fp.readline()
            if not nextline: nextline = '\n'
            line = line[:-2] + nextline
        # Parse the line
        key, fields = parseline(line)
        if not (key and fields):
            continue
        # Normalize the key
80
        types = key.split('/')
81
        for j in range(len(types)):
82 83
            types[j] = types[j].strip()
        key = '/'.join(types).lower()
84
        # Update the database
85
        if key in caps:
86 87 88
            caps[key].append(fields)
        else:
            caps[key] = [fields]
Guido van Rossum's avatar
Guido van Rossum committed
89 90 91
    return caps

def parseline(line):
92 93 94 95 96
    """Parse one entry in a mailcap file and return a dictionary.

    The viewing command is stored as the value with the key "view",
    and the rest of the fields produce key-value pairs in the dict.
    """
Guido van Rossum's avatar
Guido van Rossum committed
97 98 99
    fields = []
    i, n = 0, len(line)
    while i < n:
100 101 102
        field, i = parsefield(line, i, n)
        fields.append(field)
        i = i+1 # Skip semicolon
Guido van Rossum's avatar
Guido van Rossum committed
103
    if len(fields) < 2:
104
        return None, None
Guido van Rossum's avatar
Guido van Rossum committed
105 106 107
    key, view, rest = fields[0], fields[1], fields[2:]
    fields = {'view': view}
    for field in rest:
108
        i = field.find('=')
109 110 111 112
        if i < 0:
            fkey = field
            fvalue = ""
        else:
113 114
            fkey = field[:i].strip()
            fvalue = field[i+1:].strip()
115
        if fkey in fields:
116 117 118 119
            # Ignore it
            pass
        else:
            fields[fkey] = fvalue
Guido van Rossum's avatar
Guido van Rossum committed
120 121 122
    return key, fields

def parsefield(line, i, n):
123
    """Separate one key-value pair in a mailcap entry."""
Guido van Rossum's avatar
Guido van Rossum committed
124 125
    start = i
    while i < n:
126 127 128 129 130 131 132
        c = line[i]
        if c == ';':
            break
        elif c == '\\':
            i = i+2
        else:
            i = i+1
133
    return line[start:i].strip(), i
Guido van Rossum's avatar
Guido van Rossum committed
134 135 136 137


# Part 3: using the database.

138 139
def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]):
    """Find a match for a mailcap entry.
Tim Peters's avatar
Tim Peters committed
140

141 142 143 144 145 146 147
    Return a tuple containing the command line, and the mailcap entry
    used; (None, None) if no match is found.  This may invoke the
    'test' command of several matching entries before deciding which
    entry to use.

    """
    entries = lookup(caps, MIMEtype, key)
Tim Peters's avatar
Tim Peters committed
148
    # XXX This code should somehow check for the needsterminal flag.
Guido van Rossum's avatar
Guido van Rossum committed
149
    for e in entries:
150
        if 'test' in e:
151 152 153 154 155
            test = subst(e['test'], filename, plist)
            if test and os.system(test) != 0:
                continue
        command = subst(e[key], MIMEtype, filename, plist)
        return command, e
Guido van Rossum's avatar
Guido van Rossum committed
156 157
    return None, None

158
def lookup(caps, MIMEtype, key=None):
Guido van Rossum's avatar
Guido van Rossum committed
159
    entries = []
160
    if MIMEtype in caps:
161
        entries = entries + caps[MIMEtype]
162
    MIMEtypes = MIMEtype.split('/')
163
    MIMEtype = MIMEtypes[0] + '/*'
164
    if MIMEtype in caps:
165
        entries = entries + caps[MIMEtype]
Guido van Rossum's avatar
Guido van Rossum committed
166
    if key is not None:
167
        entries = filter(lambda e, key=key: key in e, entries)
Guido van Rossum's avatar
Guido van Rossum committed
168 169
    return entries

170
def subst(field, MIMEtype, filename, plist=[]):
Guido van Rossum's avatar
Guido van Rossum committed
171 172 173 174
    # XXX Actually, this is Unix-specific
    res = ''
    i, n = 0, len(field)
    while i < n:
175
        c = field[i]; i = i+1
176
        if c != '%':
177 178 179 180 181 182 183 184 185 186 187 188 189
            if c == '\\':
                c = field[i:i+1]; i = i+1
            res = res + c
        else:
            c = field[i]; i = i+1
            if c == '%':
                res = res + c
            elif c == 's':
                res = res + filename
            elif c == 't':
                res = res + MIMEtype
            elif c == '{':
                start = i
190
                while i < n and field[i] != '}':
191 192 193 194 195 196 197 198 199
                    i = i+1
                name = field[start:i]
                i = i+1
                res = res + findparam(name, plist)
            # XXX To do:
            # %n == number of parts if type is multipart/*
            # %F == list of alternating type and filename for parts
            else:
                res = res + '%' + c
Guido van Rossum's avatar
Guido van Rossum committed
200 201 202
    return res

def findparam(name, plist):
203
    name = name.lower() + '='
Guido van Rossum's avatar
Guido van Rossum committed
204 205
    n = len(name)
    for p in plist:
206
        if p[:n].lower() == name:
207
            return p[n:]
Guido van Rossum's avatar
Guido van Rossum committed
208 209 210 211 212 213 214 215 216
    return ''


# Part 4: test program.

def test():
    import sys
    caps = getcaps()
    if not sys.argv[1:]:
217 218
        show(caps)
        return
Guido van Rossum's avatar
Guido van Rossum committed
219
    for i in range(1, len(sys.argv), 2):
220 221
        args = sys.argv[i:i+2]
        if len(args) < 2:
222
            print("usage: mailcap [MIMEtype file] ...")
223 224 225 226 227
            return
        MIMEtype = args[0]
        file = args[1]
        command, e = findmatch(caps, MIMEtype, 'view', file)
        if not command:
228
            print("No viewer found for", type)
229
        else:
230
            print("Executing:", command)
231 232
            sts = os.system(command)
            if sts:
233
                print("Exit status:", sts)
Guido van Rossum's avatar
Guido van Rossum committed
234 235

def show(caps):
236 237 238
    print("Mailcap files:")
    for fn in listmailcapfiles(): print("\t" + fn)
    print()
Guido van Rossum's avatar
Guido van Rossum committed
239
    if not caps: caps = getcaps()
240 241
    print("Mailcap entries:")
    print()
Guido van Rossum's avatar
Guido van Rossum committed
242 243 244
    ckeys = caps.keys()
    ckeys.sort()
    for type in ckeys:
245
        print(type)
246 247 248 249 250
        entries = caps[type]
        for e in entries:
            keys = e.keys()
            keys.sort()
            for k in keys:
251 252
                print("  %-15s" % k, e[k])
            print()
Guido van Rossum's avatar
Guido van Rossum committed
253 254 255

if __name__ == '__main__':
    test()