diff options
author | Guido van Rossum <guido@python.org> | 1998-03-26 21:13:24 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-03-26 21:13:24 (GMT) |
commit | 45e2fbc2e70ef28b1f0327207f33dab3a4e825c5 (patch) | |
tree | 24cafdb6ffb07170188292a02440935291327cde /Lib/mailcap.py | |
parent | 9ea7024754f0e42d7fc70fd1c8f6f6cfbf7e1cf0 (diff) | |
download | cpython-45e2fbc2e70ef28b1f0327207f33dab3a4e825c5.zip cpython-45e2fbc2e70ef28b1f0327207f33dab3a4e825c5.tar.gz cpython-45e2fbc2e70ef28b1f0327207f33dab3a4e825c5.tar.bz2 |
Mass check-in after untabifying all files that need it.
Diffstat (limited to 'Lib/mailcap.py')
-rw-r--r-- | Lib/mailcap.py | 256 |
1 files changed, 128 insertions, 128 deletions
diff --git a/Lib/mailcap.py b/Lib/mailcap.py index 8caa46d..e19a746 100644 --- a/Lib/mailcap.py +++ b/Lib/mailcap.py @@ -15,33 +15,33 @@ def getcaps(): """ caps = {} for mailcap in listmailcapfiles(): - try: - fp = open(mailcap, 'r') - except: - continue - morecaps = readmailcapfile(fp) - fp.close() - for key in morecaps.keys(): - if not caps.has_key(key): - caps[key] = morecaps[key] - else: - caps[key] = caps[key] + morecaps[key] + try: + fp = open(mailcap, 'r') + except: + continue + morecaps = readmailcapfile(fp) + fp.close() + for key in morecaps.keys(): + if not caps.has_key(key): + caps[key] = morecaps[key] + else: + caps[key] = caps[key] + morecaps[key] return caps def listmailcapfiles(): """Return a list of all mailcap files found on the system.""" # XXX Actually, this is Unix-specific if os.environ.has_key('MAILCAPS'): - str = os.environ['MAILCAPS'] - mailcaps = string.splitfields(str, ':') + str = os.environ['MAILCAPS'] + mailcaps = string.splitfields(str, ':') else: - if os.environ.has_key('HOME'): - 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'] + if os.environ.has_key('HOME'): + 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'] return mailcaps @@ -50,69 +50,69 @@ def listmailcapfiles(): def readmailcapfile(fp): caps = {} while 1: - line = fp.readline() - if not line: break - # Ignore comments and blank lines - if line[0] == '#' or string.strip(line) == '': - 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 - types = string.splitfields(key, '/') - for j in range(len(types)): - types[j] = string.strip(types[j]) - key = string.lower(string.joinfields(types, '/')) - # Update the database - if caps.has_key(key): - caps[key].append(fields) - else: - caps[key] = [fields] + line = fp.readline() + if not line: break + # Ignore comments and blank lines + if line[0] == '#' or string.strip(line) == '': + 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 + types = string.splitfields(key, '/') + for j in range(len(types)): + types[j] = string.strip(types[j]) + key = string.lower(string.joinfields(types, '/')) + # Update the database + if caps.has_key(key): + caps[key].append(fields) + else: + caps[key] = [fields] return caps def parseline(line): fields = [] i, n = 0, len(line) while i < n: - field, i = parsefield(line, i, n) - fields.append(field) - i = i+1 # Skip semicolon + field, i = parsefield(line, i, n) + fields.append(field) + i = i+1 # Skip semicolon if len(fields) < 2: - return None, None + return None, None key, view, rest = fields[0], fields[1], fields[2:] fields = {'view': view} for field in rest: - i = string.find(field, '=') - if i < 0: - fkey = field - fvalue = "" - else: - fkey = string.strip(field[:i]) - fvalue = string.strip(field[i+1:]) - if fields.has_key(fkey): - # Ignore it - pass - else: - fields[fkey] = fvalue + i = string.find(field, '=') + if i < 0: + fkey = field + fvalue = "" + else: + fkey = string.strip(field[:i]) + fvalue = string.strip(field[i+1:]) + if fields.has_key(fkey): + # Ignore it + pass + else: + fields[fkey] = fvalue return key, fields def parsefield(line, i, n): start = i while i < n: - c = line[i] - if c == ';': - break - elif c == '\\': - i = i+2 - else: - i = i+1 + c = line[i] + if c == ';': + break + elif c == '\\': + i = i+2 + else: + i = i+1 return string.strip(line[start:i]), i @@ -130,24 +130,24 @@ def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]): entries = lookup(caps, MIMEtype, key) # XXX This code should somehow check for the needsterminal flag. for e in entries: - if e.has_key('test'): - test = subst(e['test'], filename, plist) - if test and os.system(test) != 0: - continue - command = subst(e[key], MIMEtype, filename, plist) - return command, e + if e.has_key('test'): + test = subst(e['test'], filename, plist) + if test and os.system(test) != 0: + continue + command = subst(e[key], MIMEtype, filename, plist) + return command, e return None, None def lookup(caps, MIMEtype, key=None): entries = [] if caps.has_key(MIMEtype): - entries = entries + caps[MIMEtype] + entries = entries + caps[MIMEtype] MIMEtypes = string.splitfields(MIMEtype, '/') MIMEtype = MIMEtypes[0] + '/*' if caps.has_key(MIMEtype): - entries = entries + caps[MIMEtype] + entries = entries + caps[MIMEtype] 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 def subst(field, MIMEtype, filename, plist=[]): @@ -155,39 +155,39 @@ def subst(field, MIMEtype, filename, plist=[]): res = '' i, n = 0, len(field) while i < n: - c = field[i]; i = i+1 - if c <> '%': - 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 - while i < n and field[i] <> '}': - 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 + c = field[i]; i = i+1 + if c <> '%': + 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 + while i < n and field[i] <> '}': + 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 return res def findparam(name, plist): name = string.lower(name) + '=' n = len(name) for p in plist: - if string.lower(p[:n]) == name: - return p[n:] + if string.lower(p[:n]) == name: + return p[n:] return '' @@ -197,23 +197,23 @@ def test(): import sys caps = getcaps() if not sys.argv[1:]: - show(caps) - return + show(caps) + return for i in range(1, len(sys.argv), 2): - args = sys.argv[i:i+2] - if len(args) < 2: - print "usage: mailcap [MIMEtype file] ..." - return - MIMEtype = args[0] - file = args[1] - command, e = findmatch(caps, MIMEtype, 'view', file) - if not command: - print "No viewer found for", type - else: - print "Executing:", command - sts = os.system(command) - if sts: - print "Exit status:", sts + args = sys.argv[i:i+2] + if len(args) < 2: + print "usage: mailcap [MIMEtype file] ..." + return + MIMEtype = args[0] + file = args[1] + command, e = findmatch(caps, MIMEtype, 'view', file) + if not command: + print "No viewer found for", type + else: + print "Executing:", command + sts = os.system(command) + if sts: + print "Exit status:", sts def show(caps): print "Mailcap files:" @@ -225,14 +225,14 @@ def show(caps): ckeys = caps.keys() ckeys.sort() for type in ckeys: - print type - entries = caps[type] - for e in entries: - keys = e.keys() - keys.sort() - for k in keys: - print " %-15s" % k, e[k] - print + print type + entries = caps[type] + for e in entries: + keys = e.keys() + keys.sort() + for k in keys: + print " %-15s" % k, e[k] + print if __name__ == '__main__': test() |