Kaydet (Commit) bfc3944b authored tarafından Guido van Rossum's avatar Guido van Rossum

Change by Andrew Kuchling (edited by Guido):

Removed unused import tempfile.

Added some docstrings.
üst 31ef35b8
# Mailcap file handling. See RFC 1524. """Mailcap file handling. See RFC 1524."""
import os import os
import string import string
import tempfile
# Part 1: top-level interface. # Part 1: top-level interface.
def getcaps(): def getcaps():
"""Return a dictionary containing the mailcap database.
The dictionary maps a MIME type (in all lowercase,
e.g. 'text/plain') to a list of corresponding mailcap entries.
"""
caps = {} caps = {}
for mailcap in listmailcapfiles(): for mailcap in listmailcapfiles():
try: try:
...@@ -24,6 +29,7 @@ def getcaps(): ...@@ -24,6 +29,7 @@ def getcaps():
return caps return caps
def listmailcapfiles(): def listmailcapfiles():
"""Return a list of all mailcap files found on the system."""
# XXX Actually, this is Unix-specific # XXX Actually, this is Unix-specific
if os.environ.has_key('MAILCAPS'): if os.environ.has_key('MAILCAPS'):
str = os.environ['MAILCAPS'] str = os.environ['MAILCAPS']
...@@ -112,30 +118,39 @@ def parsefield(line, i, n): ...@@ -112,30 +118,39 @@ def parsefield(line, i, n):
# Part 3: using the database. # Part 3: using the database.
def findmatch(caps, type, key='view', filename="/dev/null", plist=[]): def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]):
entries = lookup(caps, type, key) """Find a match for a mailcap entry.
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)
# XXX This code should somehow check for the needsterminal flag.
for e in entries: for e in entries:
if e.has_key('test'): if e.has_key('test'):
test = subst(e['test'], filename, plist) test = subst(e['test'], filename, plist)
if test and os.system(test) != 0: if test and os.system(test) != 0:
continue continue
command = subst(e[key], type, filename, plist) command = subst(e[key], MIMEtype, filename, plist)
return command, e return command, e
return None, None return None, None
def lookup(caps, type, key=None): def lookup(caps, MIMEtype, key=None):
entries = [] entries = []
if caps.has_key(type): if caps.has_key(MIMEtype):
entries = entries + caps[type] entries = entries + caps[MIMEtype]
types = string.splitfields(type, '/') MIMEtypes = string.splitfields(MIMEtype, '/')
type = types[0] + '/*' MIMEtype = MIMEtypes[0] + '/*'
if caps.has_key(type): if caps.has_key(MIMEtype):
entries = entries + caps[type] entries = entries + caps[MIMEtype]
if key is not None: if key is not None:
entries = filter(lambda e, key=key: e.has_key(key), entries) entries = filter(lambda e, key=key: e.has_key(key), entries)
return entries return entries
def subst(field, type, filename, plist=[]): def subst(field, MIMEtype, filename, plist=[]):
# XXX Actually, this is Unix-specific # XXX Actually, this is Unix-specific
res = '' res = ''
i, n = 0, len(field) i, n = 0, len(field)
...@@ -152,7 +167,7 @@ def subst(field, type, filename, plist=[]): ...@@ -152,7 +167,7 @@ def subst(field, type, filename, plist=[]):
elif c == 's': elif c == 's':
res = res + filename res = res + filename
elif c == 't': elif c == 't':
res = res + type res = res + MIMEtype
elif c == '{': elif c == '{':
start = i start = i
while i < n and field[i] <> '}': while i < n and field[i] <> '}':
...@@ -187,11 +202,11 @@ def test(): ...@@ -187,11 +202,11 @@ def test():
for i in range(1, len(sys.argv), 2): for i in range(1, len(sys.argv), 2):
args = sys.argv[i:i+2] args = sys.argv[i:i+2]
if len(args) < 2: if len(args) < 2:
print "usage: mailcap [type file] ..." print "usage: mailcap [MIMEtype file] ..."
return return
type = args[0] MIMEtype = args[0]
file = args[1] file = args[1]
command, e = findmatch(caps, type, 'view', file) command, e = findmatch(caps, MIMEtype, 'view', file)
if not command: if not command:
print "No viewer found for", type print "No viewer found for", type
else: else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment