Commit 9caa0d16 authored by Barry Warsaw's avatar Barry Warsaw

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