Mercurial > cpython-lru_cache-weakref
annotate Lib/mailcap.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | dbee2f628a90 |
| children | 4cdf21a0dbbc |
| rev | line source |
|---|---|
|
5216
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
1 """Mailcap file handling. See RFC 1524.""" |
| 3133 | 2 |
| 3 import os | |
| 4 | |
|
16198
32755b456369
a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents:
15868
diff
changeset
|
5 __all__ = ["getcaps","findmatch"] |
| 3133 | 6 |
| 7 # Part 1: top-level interface. | |
| 8 | |
| 9 def getcaps(): | |
|
5216
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
10 """Return a dictionary containing the mailcap database. |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
11 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
12 The dictionary maps a MIME type (in all lowercase, e.g. 'text/plain') |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
13 to a list of dictionaries corresponding to mailcap entries. The list |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
14 collects all the entries for that MIME type from all available mailcap |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
15 files. Each dictionary contains key-value pairs for that MIME type, |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
16 where the viewing command is stored with the key "view". |
|
5216
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
17 |
|
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
18 """ |
| 3133 | 19 caps = {} |
| 20 for mailcap in listmailcapfiles(): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
21 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
22 fp = open(mailcap, 'r') |
|
17796
3b5beb9277e0
Opening a file for reading can raise IOError, so only catch that.
Fred Drake <fdrake@acm.org>
parents:
16509
diff
changeset
|
23 except IOError: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
24 continue |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
25 morecaps = readmailcapfile(fp) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
26 fp.close() |
|
23634
dbee2f628a90
Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
27 for key, value in morecaps.iteritems(): |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
17796
diff
changeset
|
28 if not key in caps: |
|
23634
dbee2f628a90
Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
29 caps[key] = value |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
30 else: |
|
23634
dbee2f628a90
Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
31 caps[key] = caps[key] + value |
| 3133 | 32 return caps |
| 33 | |
| 34 def listmailcapfiles(): | |
|
5216
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
35 """Return a list of all mailcap files found on the system.""" |
| 3133 | 36 # XXX Actually, this is Unix-specific |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
17796
diff
changeset
|
37 if 'MAILCAPS' in os.environ: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
38 str = os.environ['MAILCAPS'] |
|
16509
5f588dd5439b
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16198
diff
changeset
|
39 mailcaps = str.split(':') |
| 3133 | 40 else: |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
17796
diff
changeset
|
41 if 'HOME' in os.environ: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
42 home = os.environ['HOME'] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
43 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
44 # Don't bother with getpwuid() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
45 home = '.' # Last resort |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
46 mailcaps = [home + '/.mailcap', '/etc/mailcap', |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
47 '/usr/etc/mailcap', '/usr/local/etc/mailcap'] |
| 3133 | 48 return mailcaps |
| 49 | |
| 50 | |
| 51 # Part 2: the parser. | |
| 52 | |
| 53 def readmailcapfile(fp): | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
54 """Read a mailcap file and return a dictionary keyed by MIME type. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
55 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
56 Each MIME type is mapped to an entry consisting of a list of |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
57 dictionaries; the list will contain more than one such dictionary |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
58 if a given MIME type appears more than once in the mailcap file. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
59 Each dictionary contains key-value pairs for that MIME type, where |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
60 the viewing command is stored with the key "view". |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
61 """ |
| 3133 | 62 caps = {} |
| 63 while 1: | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
64 line = fp.readline() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
65 if not line: break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
66 # Ignore comments and blank lines |
|
16509
5f588dd5439b
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16198
diff
changeset
|
67 if line[0] == '#' or line.strip() == '': |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
68 continue |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
69 nextline = line |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
70 # Join continuation lines |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
71 while nextline[-2:] == '\\\n': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
72 nextline = fp.readline() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
73 if not nextline: nextline = '\n' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
74 line = line[:-2] + nextline |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
75 # Parse the line |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
76 key, fields = parseline(line) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
77 if not (key and fields): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
78 continue |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
79 # Normalize the key |
|
16509
5f588dd5439b
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16198
diff
changeset
|
80 types = key.split('/') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
81 for j in range(len(types)): |
|
16509
5f588dd5439b
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16198
diff
changeset
|
82 types[j] = types[j].strip() |
|
5f588dd5439b
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16198
diff
changeset
|
83 key = '/'.join(types).lower() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
84 # Update the database |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
17796
diff
changeset
|
85 if key in caps: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
86 caps[key].append(fields) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
87 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
88 caps[key] = [fields] |
| 3133 | 89 return caps |
| 90 | |
| 91 def parseline(line): | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
92 """Parse one entry in a mailcap file and return a dictionary. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
93 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
94 The viewing command is stored as the value with the key "view", |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
95 and the rest of the fields produce key-value pairs in the dict. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
96 """ |
| 3133 | 97 fields = [] |
| 98 i, n = 0, len(line) | |
| 99 while i < n: | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
100 field, i = parsefield(line, i, n) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
101 fields.append(field) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
102 i = i+1 # Skip semicolon |
| 3133 | 103 if len(fields) < 2: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
104 return None, None |
| 3133 | 105 key, view, rest = fields[0], fields[1], fields[2:] |
| 106 fields = {'view': view} | |
| 107 for field in rest: | |
|
16509
5f588dd5439b
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16198
diff
changeset
|
108 i = field.find('=') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
109 if i < 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
110 fkey = field |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
111 fvalue = "" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
112 else: |
|
16509
5f588dd5439b
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16198
diff
changeset
|
113 fkey = field[:i].strip() |
|
5f588dd5439b
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16198
diff
changeset
|
114 fvalue = field[i+1:].strip() |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
17796
diff
changeset
|
115 if fkey in fields: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
116 # Ignore it |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
117 pass |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
118 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
119 fields[fkey] = fvalue |
| 3133 | 120 return key, fields |
| 121 | |
| 122 def parsefield(line, i, n): | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
123 """Separate one key-value pair in a mailcap entry.""" |
| 3133 | 124 start = i |
| 125 while i < n: | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
126 c = line[i] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
127 if c == ';': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
128 break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
129 elif c == '\\': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
130 i = i+2 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
131 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
132 i = i+1 |
|
16509
5f588dd5439b
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16198
diff
changeset
|
133 return line[start:i].strip(), i |
| 3133 | 134 |
| 135 | |
| 136 # Part 3: using the database. | |
| 137 | |
|
5216
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
138 def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]): |
|
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
139 """Find a match for a mailcap entry. |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
140 |
|
5216
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
141 Return a tuple containing the command line, and the mailcap entry |
|
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
142 used; (None, None) if no match is found. This may invoke the |
|
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
143 'test' command of several matching entries before deciding which |
|
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
144 entry to use. |
|
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
145 |
|
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
146 """ |
|
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
147 entries = lookup(caps, MIMEtype, key) |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
148 # XXX This code should somehow check for the needsterminal flag. |
| 3133 | 149 for e in entries: |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
17796
diff
changeset
|
150 if 'test' in e: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
151 test = subst(e['test'], filename, plist) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
152 if test and os.system(test) != 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
153 continue |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
154 command = subst(e[key], MIMEtype, filename, plist) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
155 return command, e |
| 3133 | 156 return None, None |
| 157 | |
|
5216
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
158 def lookup(caps, MIMEtype, key=None): |
| 3133 | 159 entries = [] |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
17796
diff
changeset
|
160 if MIMEtype in caps: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
161 entries = entries + caps[MIMEtype] |
|
16509
5f588dd5439b
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16198
diff
changeset
|
162 MIMEtypes = MIMEtype.split('/') |
|
5216
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
163 MIMEtype = MIMEtypes[0] + '/*' |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
17796
diff
changeset
|
164 if MIMEtype in caps: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
165 entries = entries + caps[MIMEtype] |
| 3133 | 166 if key is not None: |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
17796
diff
changeset
|
167 entries = filter(lambda e, key=key: key in e, entries) |
| 3133 | 168 return entries |
| 169 | |
|
5216
9bf650f23da5
Change by Andrew Kuchling (edited by Guido):
Guido van Rossum <guido@python.org>
parents:
4148
diff
changeset
|
170 def subst(field, MIMEtype, filename, plist=[]): |
| 3133 | 171 # XXX Actually, this is Unix-specific |
| 172 res = '' | |
| 173 i, n = 0, len(field) | |
| 174 while i < n: | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
175 c = field[i]; i = i+1 |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
176 if c != '%': |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
177 if c == '\\': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
178 c = field[i:i+1]; i = i+1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
179 res = res + c |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
180 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
181 c = field[i]; i = i+1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
182 if c == '%': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
183 res = res + c |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
184 elif c == 's': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
185 res = res + filename |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
186 elif c == 't': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
187 res = res + MIMEtype |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
188 elif c == '{': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
189 start = i |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
11706
diff
changeset
|
190 while i < n and field[i] != '}': |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
191 i = i+1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
192 name = field[start:i] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
193 i = i+1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
194 res = res + findparam(name, plist) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
195 # XXX To do: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
196 # %n == number of parts if type is multipart/* |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
197 # %F == list of alternating type and filename for parts |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
198 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
199 res = res + '%' + c |
| 3133 | 200 return res |
| 201 | |
| 202 def findparam(name, plist): | |
|
16509
5f588dd5439b
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16198
diff
changeset
|
203 name = name.lower() + '=' |
| 3133 | 204 n = len(name) |
| 205 for p in plist: | |
|
16509
5f588dd5439b
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16198
diff
changeset
|
206 if p[:n].lower() == name: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
207 return p[n:] |
| 3133 | 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:]: | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
217 show(caps) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
218 return |
| 3133 | 219 for i in range(1, len(sys.argv), 2): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
220 args = sys.argv[i:i+2] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
221 if len(args) < 2: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
222 print "usage: mailcap [MIMEtype file] ..." |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
223 return |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
224 MIMEtype = args[0] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
225 file = args[1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
226 command, e = findmatch(caps, MIMEtype, 'view', file) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
227 if not command: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
228 print "No viewer found for", type |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
229 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
230 print "Executing:", command |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
231 sts = os.system(command) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
232 if sts: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
233 print "Exit status:", sts |
| 3133 | 234 |
| 235 def show(caps): | |
| 236 print "Mailcap files:" | |
| 237 for fn in listmailcapfiles(): print "\t" + fn | |
| 238 print | |
| 239 if not caps: caps = getcaps() | |
| 240 print "Mailcap entries:" | |
| 241 print | |
| 242 ckeys = caps.keys() | |
| 243 ckeys.sort() | |
| 244 for type in ckeys: | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
245 print type |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
246 entries = caps[type] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
247 for e in entries: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
248 keys = e.keys() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
249 keys.sort() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
250 for k in keys: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
251 print " %-15s" % k, e[k] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5216
diff
changeset
|
252 print |
| 3133 | 253 |
| 254 if __name__ == '__main__': | |
| 255 test() |
