From 9caa0d1642b79020a1665560e6efaedabfadfba3 Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Mon, 9 Jun 2003 22:27:41 +0000 Subject: 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. --- Doc/lib/libmimetypes.tex | 3 +-- Lib/mimetypes.py | 15 +++++------- Lib/test/test_mimetypes.py | 59 ++++++++++++++++++++++++++-------------------- 3 files changed, 40 insertions(+), 37 deletions(-) diff --git a/Doc/lib/libmimetypes.tex b/Doc/lib/libmimetypes.tex index 64296f0..8e07768 100644 --- a/Doc/lib/libmimetypes.tex +++ b/Doc/lib/libmimetypes.tex @@ -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. diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index a909366..5784e23 100644 --- a/Lib/mimetypes.py +++ b/Lib/mimetypes.py @@ -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): """ diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py index 6953966..81829c1 100644 --- a/Lib/test/test_mimetypes.py +++ b/Lib/test/test_mimetypes.py @@ -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(): -- cgit v0.12