同步操作将从 OpenHarmony-SIG/python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
"""Mailcap file handling. See RFC 1524."""import osimport warnings__all__ = ["getcaps","findmatch"]def lineno_sort_key(entry):# Sort in ascending order, with unspecified entries at the endif 'lineno' in entry:return 0, entry['lineno']else:return 1, 0# Part 1: top-level interface.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 dictionaries corresponding to mailcap entries. The listcollects all the entries for that MIME type from all available mailcapfiles. Each dictionary contains key-value pairs for that MIME type,where the viewing command is stored with the key "view"."""caps = {}lineno = 0for mailcap in listmailcapfiles():try:fp = open(mailcap, 'r')except OSError:continuewith fp:morecaps, lineno = _readmailcapfile(fp, lineno)for key, value in morecaps.items():if not key in caps:caps[key] = valueelse:caps[key] = caps[key] + valuereturn capsdef listmailcapfiles():"""Return a list of all mailcap files found on the system."""# This is mostly a Unix thing, but we use the OS path separator anywayif 'MAILCAPS' in os.environ:pathstr = os.environ['MAILCAPS']mailcaps = pathstr.split(os.pathsep)else:if 'HOME' in os.environ:home = os.environ['HOME']else:# Don't bother with getpwuid()home = '.' # Last resortmailcaps = [home + '/.mailcap', '/etc/mailcap','/usr/etc/mailcap', '/usr/local/etc/mailcap']return mailcaps# Part 2: the parser.def readmailcapfile(fp):"""Read a mailcap file and return a dictionary keyed by MIME type."""warnings.warn('readmailcapfile is deprecated, use getcaps instead',DeprecationWarning, 2)caps, _ = _readmailcapfile(fp, None)return capsdef _readmailcapfile(fp, lineno):"""Read a mailcap file and return a dictionary keyed by MIME type.Each MIME type is mapped to an entry consisting of a list ofdictionaries; the list will contain more than one such dictionaryif a given MIME type appears more than once in the mailcap file.Each dictionary contains key-value pairs for that MIME type, wherethe viewing command is stored with the key "view"."""caps = {}while 1:line = fp.readline()if not line: break# Ignore comments and blank linesif line[0] == '#' or line.strip() == '':continuenextline = line# Join continuation lineswhile nextline[-2:] == '\\\n':nextline = fp.readline()if not nextline: nextline = '\n'line = line[:-2] + nextline# Parse the linekey, fields = parseline(line)if not (key and fields):continueif lineno is not None:fields['lineno'] = linenolineno += 1# Normalize the keytypes = key.split('/')for j in range(len(types)):types[j] = types[j].strip()key = '/'.join(types).lower()# Update the databaseif key in caps:caps[key].append(fields)else:caps[key] = [fields]return caps, linenodef parseline(line):"""Parse one entry in a mailcap file and return a dictionary.The viewing command is stored as the value with the key "view",and the rest of the fields produce key-value pairs in the dict."""fields = []i, n = 0, len(line)while i < n:field, i = parsefield(line, i, n)fields.append(field)i = i+1 # Skip semicolonif len(fields) < 2:return None, Nonekey, view, rest = fields[0], fields[1], fields[2:]fields = {'view': view}for field in rest:i = field.find('=')if i < 0:fkey = fieldfvalue = ""else:fkey = field[:i].strip()fvalue = field[i+1:].strip()if fkey in fields:# Ignore itpasselse:fields[fkey] = fvaluereturn key, fieldsdef parsefield(line, i, n):"""Separate one key-value pair in a mailcap entry."""start = iwhile i < n:c = line[i]if c == ';':breakelif c == '\\':i = i+2else:i = i+1return line[start:i].strip(), i# Part 3: using the database.def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]):"""Find a match for a mailcap entry.Return a tuple containing the command line, and the mailcap entryused; (None, None) if no match is found. This may invoke the'test' command of several matching entries before deciding whichentry to use."""entries = lookup(caps, MIMEtype, key)# XXX This code should somehow check for the needsterminal flag.for e in entries:if 'test' in e:test = subst(e['test'], filename, plist)if test and os.system(test) != 0:continuecommand = subst(e[key], MIMEtype, filename, plist)return command, ereturn None, Nonedef lookup(caps, MIMEtype, key=None):entries = []if MIMEtype in caps:entries = entries + caps[MIMEtype]MIMEtypes = MIMEtype.split('/')MIMEtype = MIMEtypes[0] + '/*'if MIMEtype in caps:entries = entries + caps[MIMEtype]if key is not None:entries = [e for e in entries if key in e]entries = sorted(entries, key=lineno_sort_key)return entriesdef subst(field, MIMEtype, filename, plist=[]):# XXX Actually, this is Unix-specificres = ''i, n = 0, len(field)while i < n:c = field[i]; i = i+1if c != '%':if c == '\\':c = field[i:i+1]; i = i+1res = res + celse:c = field[i]; i = i+1if c == '%':res = res + celif c == 's':res = res + filenameelif c == 't':res = res + MIMEtypeelif c == '{':start = iwhile i < n and field[i] != '}':i = i+1name = field[start:i]i = i+1res = res + findparam(name, plist)# XXX To do:# %n == number of parts if type is multipart/*# %F == list of alternating type and filename for partselse:res = res + '%' + creturn resdef findparam(name, plist):name = name.lower() + '='n = len(name)for p in plist:if p[:n].lower() == name:return p[n:]return ''# Part 4: test program.def test():import syscaps = getcaps()if not sys.argv[1:]:show(caps)returnfor i in range(1, len(sys.argv), 2):args = sys.argv[i:i+2]if len(args) < 2:print("usage: mailcap [MIMEtype file] ...")returnMIMEtype = 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:")for fn in listmailcapfiles(): print("\t" + fn)print()if not caps: caps = getcaps()print("Mailcap entries:")print()ckeys = sorted(caps)for type in ckeys:print(type)entries = caps[type]for e in entries:keys = sorted(e)for k in keys:print(" %-15s" % k, e[k])print()if __name__ == '__main__':test()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。