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