guess_all_extensions(): Return the empty list instead of None when
there are no matching types. Updated the docs and docstrings. Added some unit tests.
This commit is contained in:
parent
e07b83591f
commit
9caa0d1642
|
@ -53,8 +53,7 @@ Guess the extensions for a file based on its MIME type, given by
|
|||
The return value is a list of strings giving all possible filename extensions,
|
||||
including the leading dot (\character{.}). The extensions are not guaranteed
|
||||
to have been associated with any particular data stream, but would be mapped
|
||||
to the MIME type \var{type} by \function{guess_type()}. If no extension can
|
||||
be guessed for \var{type}, \code{None} is returned.
|
||||
to the MIME type \var{type} by \function{guess_type()}.
|
||||
|
||||
Optional \var{strict} has the same meaning as with the
|
||||
\function{guess_type()} function.
|
||||
|
|
|
@ -148,10 +148,8 @@ class MimeTypes:
|
|||
|
||||
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.
|
||||
guaranteed to have been associated with any particular data stream,
|
||||
but would be mapped to the MIME type `type' by guess_type().
|
||||
|
||||
Optional `strict' argument when false adds a bunch of commonly found,
|
||||
but non-standard types.
|
||||
|
@ -162,8 +160,7 @@ class MimeTypes:
|
|||
for ext in self.types_map_inv[False].get(type, []):
|
||||
if ext not in extensions:
|
||||
extensions.append(ext)
|
||||
if len(extensions):
|
||||
return extensions
|
||||
return extensions
|
||||
|
||||
def guess_extension(self, type, strict=True):
|
||||
"""Guess the extension for a file based on its MIME type.
|
||||
|
@ -179,9 +176,9 @@ class MimeTypes:
|
|||
but non-standard types.
|
||||
"""
|
||||
extensions = self.guess_all_extensions(type, strict)
|
||||
if extensions is not None:
|
||||
extensions = extensions[0]
|
||||
return extensions
|
||||
if not extensions:
|
||||
return None
|
||||
return extensions[0]
|
||||
|
||||
def read(self, filename, strict=True):
|
||||
"""
|
||||
|
|
|
@ -13,42 +13,49 @@ class MimeTypesTestCase(unittest.TestCase):
|
|||
self.db = mimetypes.MimeTypes()
|
||||
|
||||
def test_default_data(self):
|
||||
self.assertEqual(self.db.guess_type("foo.html"),
|
||||
("text/html", None))
|
||||
self.assertEqual(self.db.guess_type("foo.tgz"),
|
||||
("application/x-tar", "gzip"))
|
||||
self.assertEqual(self.db.guess_type("foo.tar.gz"),
|
||||
("application/x-tar", "gzip"))
|
||||
self.assertEqual(self.db.guess_type("foo.tar.Z"),
|
||||
("application/x-tar", "compress"))
|
||||
eq = self.assertEqual
|
||||
eq(self.db.guess_type("foo.html"), ("text/html", None))
|
||||
eq(self.db.guess_type("foo.tgz"), ("application/x-tar", "gzip"))
|
||||
eq(self.db.guess_type("foo.tar.gz"), ("application/x-tar", "gzip"))
|
||||
eq(self.db.guess_type("foo.tar.Z"), ("application/x-tar", "compress"))
|
||||
|
||||
def test_data_urls(self):
|
||||
self.assertEqual(self.db.guess_type("data:,thisIsTextPlain"),
|
||||
("text/plain", None))
|
||||
self.assertEqual(self.db.guess_type("data:;base64,thisIsTextPlain"),
|
||||
("text/plain", None))
|
||||
self.assertEqual(self.db.guess_type("data:text/x-foo,thisIsTextXFoo"),
|
||||
("text/x-foo", None))
|
||||
eq = self.assertEqual
|
||||
guess_type = self.db.guess_type
|
||||
eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
|
||||
eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
|
||||
eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
|
||||
|
||||
def test_file_parsing(self):
|
||||
eq = self.assertEqual
|
||||
sio = StringIO.StringIO("x-application/x-unittest pyunit\n")
|
||||
self.db.readfp(sio)
|
||||
self.assertEqual(self.db.guess_type("foo.pyunit"),
|
||||
("x-application/x-unittest", None))
|
||||
self.assertEqual(self.db.guess_extension("x-application/x-unittest"),
|
||||
".pyunit")
|
||||
eq(self.db.guess_type("foo.pyunit"),
|
||||
("x-application/x-unittest", None))
|
||||
eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
|
||||
|
||||
def test_non_standard_types(self):
|
||||
eq = self.assertEqual
|
||||
# First try strict
|
||||
self.assertEqual(self.db.guess_type('foo.xul', strict=1),
|
||||
(None, None))
|
||||
self.assertEqual(self.db.guess_extension('image/jpg', strict=1),
|
||||
None)
|
||||
eq(self.db.guess_type('foo.xul', strict=True), (None, None))
|
||||
eq(self.db.guess_extension('image/jpg', strict=True), None)
|
||||
# And then non-strict
|
||||
self.assertEqual(self.db.guess_type('foo.xul', strict=0),
|
||||
('text/xul', None))
|
||||
self.assertEqual(self.db.guess_extension('image/jpg', strict=0),
|
||||
'.jpg')
|
||||
eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
|
||||
eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
|
||||
|
||||
def test_guess_all_types(self):
|
||||
eq = self.assertEqual
|
||||
# First try strict
|
||||
all = self.db.guess_all_extensions('text/plain', strict=True)
|
||||
all.sort()
|
||||
eq(all, ['.bat', '.c', '.h', '.ksh', '.pl', '.txt'])
|
||||
# And now non-strict
|
||||
all = self.db.guess_all_extensions('image/jpg', strict=False)
|
||||
all.sort()
|
||||
eq(all, ['.jpg'])
|
||||
# And now for no hits
|
||||
all = self.db.guess_all_extensions('image/jpg', strict=True)
|
||||
eq(all, [])
|
||||
|
||||
|
||||
def test_main():
|
||||
|
|
Loading…
Reference in New Issue