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