Guido van Rossum | bfc3944 | 1997-03-25 21:58:08 +0000 | [diff] [blame] | 1 | """Mailcap file handling. See RFC 1524.""" |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 2 | |
| 3 | import os |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 4 | |
Skip Montanaro | 17ab123 | 2001-01-24 06:27:27 +0000 | [diff] [blame] | 5 | __all__ = ["getcaps","findmatch"] |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 6 | |
| 7 | # Part 1: top-level interface. |
| 8 | |
| 9 | def getcaps(): |
Guido van Rossum | bfc3944 | 1997-03-25 21:58:08 +0000 | [diff] [blame] | 10 | """Return a dictionary containing the mailcap database. |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 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". |
Guido van Rossum | bfc3944 | 1997-03-25 21:58:08 +0000 | [diff] [blame] | 17 | |
| 18 | """ |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 19 | caps = {} |
| 20 | for mailcap in listmailcapfiles(): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 21 | try: |
| 22 | fp = open(mailcap, 'r') |
Fred Drake | 776d39e | 2001-05-11 18:47:54 +0000 | [diff] [blame] | 23 | except IOError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 24 | continue |
| 25 | morecaps = readmailcapfile(fp) |
| 26 | fp.close() |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 27 | for key, value in morecaps.items(): |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 28 | if not key in caps: |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 29 | caps[key] = value |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 30 | else: |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 31 | caps[key] = caps[key] + value |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 32 | return caps |
| 33 | |
| 34 | def listmailcapfiles(): |
Guido van Rossum | bfc3944 | 1997-03-25 21:58:08 +0000 | [diff] [blame] | 35 | """Return a list of all mailcap files found on the system.""" |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 36 | # XXX Actually, this is Unix-specific |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 37 | if 'MAILCAPS' in os.environ: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 38 | str = os.environ['MAILCAPS'] |
Eric S. Raymond | 0c03cc2 | 2001-02-09 10:23:55 +0000 | [diff] [blame] | 39 | mailcaps = str.split(':') |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 40 | else: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 41 | if 'HOME' in os.environ: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 42 | home = os.environ['HOME'] |
| 43 | else: |
| 44 | # Don't bother with getpwuid() |
| 45 | home = '.' # Last resort |
| 46 | mailcaps = [home + '/.mailcap', '/etc/mailcap', |
| 47 | '/usr/etc/mailcap', '/usr/local/etc/mailcap'] |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 48 | return mailcaps |
| 49 | |
| 50 | |
| 51 | # Part 2: the parser. |
| 52 | |
| 53 | def readmailcapfile(fp): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 54 | """Read a mailcap file and return a dictionary keyed by MIME type. |
| 55 | |
| 56 | Each MIME type is mapped to an entry consisting of a list of |
| 57 | dictionaries; the list will contain more than one such dictionary |
| 58 | if a given MIME type appears more than once in the mailcap file. |
| 59 | Each dictionary contains key-value pairs for that MIME type, where |
| 60 | the viewing command is stored with the key "view". |
| 61 | """ |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 62 | caps = {} |
| 63 | while 1: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 64 | line = fp.readline() |
| 65 | if not line: break |
| 66 | # Ignore comments and blank lines |
Eric S. Raymond | 0c03cc2 | 2001-02-09 10:23:55 +0000 | [diff] [blame] | 67 | if line[0] == '#' or line.strip() == '': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 68 | continue |
| 69 | nextline = line |
| 70 | # Join continuation lines |
| 71 | while nextline[-2:] == '\\\n': |
| 72 | nextline = fp.readline() |
| 73 | if not nextline: nextline = '\n' |
| 74 | line = line[:-2] + nextline |
| 75 | # Parse the line |
| 76 | key, fields = parseline(line) |
| 77 | if not (key and fields): |
| 78 | continue |
| 79 | # Normalize the key |
Eric S. Raymond | 0c03cc2 | 2001-02-09 10:23:55 +0000 | [diff] [blame] | 80 | types = key.split('/') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 81 | for j in range(len(types)): |
Eric S. Raymond | 0c03cc2 | 2001-02-09 10:23:55 +0000 | [diff] [blame] | 82 | types[j] = types[j].strip() |
| 83 | key = '/'.join(types).lower() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 84 | # Update the database |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 85 | if key in caps: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 86 | caps[key].append(fields) |
| 87 | else: |
| 88 | caps[key] = [fields] |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 89 | return caps |
| 90 | |
| 91 | def parseline(line): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 92 | """Parse one entry in a mailcap file and return a dictionary. |
| 93 | |
| 94 | The viewing command is stored as the value with the key "view", |
| 95 | and the rest of the fields produce key-value pairs in the dict. |
| 96 | """ |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 97 | fields = [] |
| 98 | i, n = 0, len(line) |
| 99 | while i < n: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 100 | field, i = parsefield(line, i, n) |
| 101 | fields.append(field) |
| 102 | i = i+1 # Skip semicolon |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 103 | if len(fields) < 2: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 104 | return None, None |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 105 | key, view, rest = fields[0], fields[1], fields[2:] |
| 106 | fields = {'view': view} |
| 107 | for field in rest: |
Eric S. Raymond | 0c03cc2 | 2001-02-09 10:23:55 +0000 | [diff] [blame] | 108 | i = field.find('=') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 109 | if i < 0: |
| 110 | fkey = field |
| 111 | fvalue = "" |
| 112 | else: |
Eric S. Raymond | 0c03cc2 | 2001-02-09 10:23:55 +0000 | [diff] [blame] | 113 | fkey = field[:i].strip() |
| 114 | fvalue = field[i+1:].strip() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 115 | if fkey in fields: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 116 | # Ignore it |
| 117 | pass |
| 118 | else: |
| 119 | fields[fkey] = fvalue |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 120 | return key, fields |
| 121 | |
| 122 | def parsefield(line, i, n): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 123 | """Separate one key-value pair in a mailcap entry.""" |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 124 | start = i |
| 125 | while i < n: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 126 | c = line[i] |
| 127 | if c == ';': |
| 128 | break |
| 129 | elif c == '\\': |
| 130 | i = i+2 |
| 131 | else: |
| 132 | i = i+1 |
Eric S. Raymond | 0c03cc2 | 2001-02-09 10:23:55 +0000 | [diff] [blame] | 133 | return line[start:i].strip(), i |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 134 | |
| 135 | |
| 136 | # Part 3: using the database. |
| 137 | |
Guido van Rossum | bfc3944 | 1997-03-25 21:58:08 +0000 | [diff] [blame] | 138 | def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]): |
| 139 | """Find a match for a mailcap entry. |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | bfc3944 | 1997-03-25 21:58:08 +0000 | [diff] [blame] | 141 | Return a tuple containing the command line, and the mailcap entry |
| 142 | used; (None, None) if no match is found. This may invoke the |
| 143 | 'test' command of several matching entries before deciding which |
| 144 | entry to use. |
| 145 | |
| 146 | """ |
| 147 | entries = lookup(caps, MIMEtype, key) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 148 | # XXX This code should somehow check for the needsterminal flag. |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 149 | for e in entries: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 150 | if 'test' in e: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 151 | test = subst(e['test'], filename, plist) |
| 152 | if test and os.system(test) != 0: |
| 153 | continue |
| 154 | command = subst(e[key], MIMEtype, filename, plist) |
| 155 | return command, e |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 156 | return None, None |
| 157 | |
Guido van Rossum | bfc3944 | 1997-03-25 21:58:08 +0000 | [diff] [blame] | 158 | def lookup(caps, MIMEtype, key=None): |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 159 | entries = [] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 160 | if MIMEtype in caps: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 161 | entries = entries + caps[MIMEtype] |
Eric S. Raymond | 0c03cc2 | 2001-02-09 10:23:55 +0000 | [diff] [blame] | 162 | MIMEtypes = MIMEtype.split('/') |
Guido van Rossum | bfc3944 | 1997-03-25 21:58:08 +0000 | [diff] [blame] | 163 | MIMEtype = MIMEtypes[0] + '/*' |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 164 | if MIMEtype in caps: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 165 | entries = entries + caps[MIMEtype] |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 166 | if key is not None: |
Antoine Pitrou | 7062db8 | 2010-04-22 13:30:10 +0000 | [diff] [blame] | 167 | entries = [e for e in entries if key in e] |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 168 | return entries |
| 169 | |
Guido van Rossum | bfc3944 | 1997-03-25 21:58:08 +0000 | [diff] [blame] | 170 | def subst(field, MIMEtype, filename, plist=[]): |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 171 | # XXX Actually, this is Unix-specific |
| 172 | res = '' |
| 173 | i, n = 0, len(field) |
| 174 | while i < n: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 175 | c = field[i]; i = i+1 |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 176 | if c != '%': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 177 | if c == '\\': |
| 178 | c = field[i:i+1]; i = i+1 |
| 179 | res = res + c |
| 180 | else: |
| 181 | c = field[i]; i = i+1 |
| 182 | if c == '%': |
| 183 | res = res + c |
| 184 | elif c == 's': |
| 185 | res = res + filename |
| 186 | elif c == 't': |
| 187 | res = res + MIMEtype |
| 188 | elif c == '{': |
| 189 | start = i |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 190 | while i < n and field[i] != '}': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 191 | i = i+1 |
| 192 | name = field[start:i] |
| 193 | i = i+1 |
| 194 | res = res + findparam(name, plist) |
| 195 | # XXX To do: |
| 196 | # %n == number of parts if type is multipart/* |
| 197 | # %F == list of alternating type and filename for parts |
| 198 | else: |
| 199 | res = res + '%' + c |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 200 | return res |
| 201 | |
| 202 | def findparam(name, plist): |
Eric S. Raymond | 0c03cc2 | 2001-02-09 10:23:55 +0000 | [diff] [blame] | 203 | name = name.lower() + '=' |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 204 | n = len(name) |
| 205 | for p in plist: |
Eric S. Raymond | 0c03cc2 | 2001-02-09 10:23:55 +0000 | [diff] [blame] | 206 | if p[:n].lower() == name: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 207 | return p[n:] |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 208 | return '' |
| 209 | |
| 210 | |
| 211 | # Part 4: test program. |
| 212 | |
| 213 | def test(): |
| 214 | import sys |
| 215 | caps = getcaps() |
| 216 | if not sys.argv[1:]: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 217 | show(caps) |
| 218 | return |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 219 | for i in range(1, len(sys.argv), 2): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 220 | args = sys.argv[i:i+2] |
| 221 | if len(args) < 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 222 | print("usage: mailcap [MIMEtype file] ...") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 223 | return |
| 224 | MIMEtype = args[0] |
| 225 | file = args[1] |
| 226 | command, e = findmatch(caps, MIMEtype, 'view', file) |
| 227 | if not command: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 228 | print("No viewer found for", type) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 229 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 230 | print("Executing:", command) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 231 | sts = os.system(command) |
| 232 | if sts: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 233 | print("Exit status:", sts) |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 234 | |
| 235 | def show(caps): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 236 | print("Mailcap files:") |
| 237 | for fn in listmailcapfiles(): print("\t" + fn) |
| 238 | print() |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 239 | if not caps: caps = getcaps() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 240 | print("Mailcap entries:") |
| 241 | print() |
Antoine Pitrou | 945c17f | 2010-04-22 13:19:31 +0000 | [diff] [blame] | 242 | ckeys = sorted(caps) |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 243 | for type in ckeys: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 244 | print(type) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 245 | entries = caps[type] |
| 246 | for e in entries: |
Antoine Pitrou | 945c17f | 2010-04-22 13:19:31 +0000 | [diff] [blame] | 247 | keys = sorted(e) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 248 | for k in keys: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 249 | print(" %-15s" % k, e[k]) |
| 250 | print() |
Guido van Rossum | 8988fb2 | 1995-09-30 16:52:24 +0000 | [diff] [blame] | 251 | |
| 252 | if __name__ == '__main__': |
| 253 | test() |