Commit cc2b3236 authored by Fred Drake's avatar Fred Drake

guess_extension(): New function. Performs a reverse mapping from MIME type

	to filename extension.
parent f2d2375f
"""Guess the MIME type of a file. """Guess the MIME type of a file.
This module defines one useful function: This module defines two useful functions:
guess_type(url) -- guess the MIME type and encoding of a URL. guess_type(url) -- guess the MIME type and encoding of a URL.
guess_extension(type) -- guess the extension for a given MIME type.
It also contains the following, for tuning the behavior: It also contains the following, for tuning the behavior:
Data: Data:
...@@ -64,6 +66,21 @@ def guess_type(url): ...@@ -64,6 +66,21 @@ def guess_type(url):
else: else:
return None, encoding return None, encoding
def guess_extension(type):
"""Guess the extension for a file based on its MIME type.
Return value is a string giving a filename extension, including the
leading dot ('.'). The extension is not guaranteed to have been
associated with any particular data stream, but has been known to be
used for streams of the MIME type given by `type'. If `type' is not
known, None is returned.
"""
type = string.lower(type)
for ext, stype in types_map.items():
if type == stype:
return ext
return None
def init(files=None): def init(files=None):
global inited global inited
for file in files or knownfiles: for file in files or knownfiles:
......
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