Mailing List Archive

python/dist/src/Lib mimetypes.py,1.22,1.23
Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv16079/Lib

Modified Files:
mimetypes.py
Log Message:
Apply diff3.txt (plus additional documentation)
from SF patch http://www.python.org/sf/554192

This adds two new functions to mimetypes:
guess_all_extensions() which returns a list of all known
extensions for a mime type, and add_type() which adds one
mapping between a mime type and an extension.


Index: mimetypes.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/mimetypes.py,v
retrieving revision 1.22
retrieving revision 1.23
diff -C2 -d -r1.22 -r1.23
*** mimetypes.py 1 Jun 2002 14:18:46 -0000 1.22
--- mimetypes.py 6 Sep 2002 16:15:58 -0000 1.23
***************
*** 27,33 ****
import urllib

! __all__ = ["guess_type","guess_extension","read_mime_types","init"]

knownfiles = [
"/usr/local/etc/httpd/conf/mime.types",
"/usr/local/lib/netscape/mime.types",
--- 27,37 ----
import urllib

! __all__ = [
! "guess_type","guess_extension","guess_all_extensions",
! "add_type","read_mime_types","init"
! ]

knownfiles = [
+ "/etc/mime.types",
"/usr/local/etc/httpd/conf/mime.types",
"/usr/local/lib/netscape/mime.types",
***************
*** 36,40 ****
]

! inited = 0


--- 40,44 ----
]

! inited = False


***************
*** 47,61 ****
"""

! def __init__(self, filenames=()):
if not inited:
init()
self.encodings_map = encodings_map.copy()
self.suffix_map = suffix_map.copy()
! self.types_map = types_map.copy()
! self.common_types = common_types.copy()
for name in filenames:
! self.read(name)

! def guess_type(self, url, strict=1):
"""Guess the type of a file based on its URL.

--- 51,86 ----
"""

! def __init__(self, filenames=(), strict=True):
if not inited:
init()
self.encodings_map = encodings_map.copy()
self.suffix_map = suffix_map.copy()
! self.types_map = ({}, {}) # dict for (non-strict, strict)
! self.types_map_inv = ({}, {})
! for (ext, type) in types_map.items():
! self.add_type(type, ext, True)
! for (ext, type) in common_types.items():
! self.add_type(type, ext, False)
for name in filenames:
! self.read(name, strict)

! def add_type(self, type, ext, strict=True):
! """Add a mapping between a type and and extension.
!
! When the extension is already known, the new
! type will replace the old one. When the type
! is already known the extension will be added
! to the list of known extensions.
!
! If strict is true, information will be added to
! list of standard types, else to the list of non-standard
! types.
! """
! self.types_map[strict][ext] = type
! exts = self.types_map_inv[strict].setdefault(type, [])
! if ext not in exts:
! exts.append(ext)
!
! def guess_type(self, url, strict=True):
"""Guess the type of a file based on its URL.

***************
*** 73,77 ****
dictionary suffix_map.)

! Optional `strict' argument when false adds a bunch of commonly found,
but non-standard types.
"""
--- 98,102 ----
dictionary suffix_map.)

! Optional `strict' argument when False adds a bunch of commonly found,
but non-standard types.
"""
***************
*** 104,109 ****
else:
encoding = None
! types_map = self.types_map
! common_types = self.common_types
if ext in types_map:
return types_map[ext], encoding
--- 129,133 ----
else:
encoding = None
! types_map = self.types_map[True]
if ext in types_map:
return types_map[ext], encoding
***************
*** 112,123 ****
elif strict:
return None, encoding
! elif ext in common_types:
! return common_types[ext], encoding
! elif ext.lower() in common_types:
! return common_types[ext.lower()], encoding
else:
return None, encoding

! def guess_extension(self, type, strict=1):
"""Guess the extension for a file based on its MIME type.

--- 136,170 ----
elif strict:
return None, encoding
! types_map = self.types_map[False]
! if ext in types_map:
! return types_map[ext], encoding
! elif ext.lower() in types_map:
! return types_map[ext.lower()], encoding
else:
return None, encoding

! def guess_all_extensions(self, type, strict=True):
! """Guess the extensions for a file based on its MIME type.
!
! Return value is a list of strings giving the possible filename
! extensions, including the leading dot ('.'). The extension is not
! guaranteed to have been associated with any particular data
! stream, but would be mapped to the MIME type `type' by
! guess_type(). If no extension can be guessed for `type', None
! is returned.
!
! Optional `strict' argument when false adds a bunch of commonly found,
! but non-standard types.
! """
! type = type.lower()
! extensions = self.types_map_inv[True].get(type, [])
! if not strict:
! for ext in self.types_map_inv[False].get(type, []):
! if ext not in extensions:
! extensions.append(ext)
! if len(extensions):
! return extensions
!
! def guess_extension(self, type, strict=True):
"""Guess the extension for a file based on its MIME type.

***************
*** 132,154 ****
but non-standard types.
"""
! type = type.lower()
! for ext, stype in self.types_map.items():
! if type == stype:
! return ext
! if not strict:
! for ext, stype in common_types.items():
! if type == stype:
! return ext
! return None

! def read(self, filename):
! """Read a single mime.types-format file, specified by pathname."""
fp = open(filename)
self.readfp(fp)
fp.close()

! def readfp(self, fp):
! """Read a single mime.types-format file."""
! map = self.types_map
while 1:
line = fp.readline()
--- 179,207 ----
but non-standard types.
"""
! extensions = self.guess_all_extensions(type, strict)
! if extensions is not None:
! extensions = extensions[0]
! return extensions

! def read(self, filename, strict=True):
! """
! Read a single mime.types-format file, specified by pathname.
!
! If strict is true, information will be added to
! list of standard types, else to the list of non-standard
! types.
! """
fp = open(filename)
self.readfp(fp)
fp.close()

! def readfp(self, fp, strict=True):
! """
! Read a single mime.types-format file.
!
! If strict is true, information will be added to
! list of standard types, else to the list of non-standard
! types.
! """
while 1:
line = fp.readline()
***************
*** 163,171 ****
continue
type, suffixes = words[0], words[1:]
for suff in suffixes:
! map['.' + suff] = type
!

! def guess_type(url, strict=1):
"""Guess the type of a file based on its URL.

--- 216,224 ----
continue
type, suffixes = words[0], words[1:]
+ suffixes = [ '.' + suff for suff in suffixes ]
for suff in suffixes:
! self.add_type(type, suff, strict)

! def guess_type(url, strict=True):
"""Guess the type of a file based on its URL.

***************
*** 189,193 ****


! def guess_extension(type, strict=1):
"""Guess the extension for a file based on its MIME type.

--- 242,262 ----


! def guess_all_extensions(type, strict=True):
! """Guess the extensions for a file based on its MIME type.
!
! Return value is a list of strings giving the possible filename
! extensions, including the leading dot ('.'). The extension is not
! guaranteed to have been associated with any particular data
! stream, but would be mapped to the MIME type `type' by
! guess_type(). If no extension can be guessed for `type', None
! is returned.
!
! Optional `strict' argument when false adds a bunch of commonly found,
! but non-standard types.
! """
! init()
! return guess_all_extensions(type, strict)
!
! def guess_extension(type, strict=True):
"""Guess the extension for a file based on its MIME type.

***************
*** 204,213 ****
return guess_extension(type, strict)


def init(files=None):
! global guess_extension, guess_type
global suffix_map, types_map, encodings_map, common_types
! global inited
! inited = 1
db = MimeTypes()
if files is None:
--- 273,297 ----
return guess_extension(type, strict)

+ def add_type(self, type, ext, strict=True):
+ """Add a mapping between a type and and extension.
+
+ When the extension is already known, the new
+ type will replace the old one. When the type
+ is already known the extension will be added
+ to the list of known extensions.
+
+ If strict is true, information will be added to
+ list of standard types, else to the list of non-standard
+ types.
+ """
+ init()
+ return add_type(type, ext, strict)
+

def init(files=None):
! global guess_all_extensions, guess_extension, guess_type
global suffix_map, types_map, encodings_map, common_types
! global add_type, inited
! inited = True
db = MimeTypes()
if files is None:
***************
*** 218,225 ****
encodings_map = db.encodings_map
suffix_map = db.suffix_map
! types_map = db.types_map
guess_extension = db.guess_extension
guess_type = db.guess_type
! common_types = db.common_types


--- 302,311 ----
encodings_map = db.encodings_map
suffix_map = db.suffix_map
! types_map = db.types_map[True]
! guess_all_extensions = db.guess_all_extensions
guess_extension = db.guess_extension
guess_type = db.guess_type
! add_type = db.add_type
! common_types = db.types_map[False]