diff --git a/shotgun_api3/lib/mimetypes.py b/shotgun_api3/lib/mimetypes.py index 8416aa630..236c33312 100644 --- a/shotgun_api3/lib/mimetypes.py +++ b/shotgun_api3/lib/mimetypes.py @@ -112,7 +112,12 @@ def guess_type(self, url, strict=True): Optional `strict' argument when False adds a bunch of commonly found, but non-standard types. """ - scheme, url = urllib.parse.splittype(url) + # urllib.splittype is no longer exposed in Python 3. To replicate its functionality, we'll + # use urlparse.urlsplit to find the scheme, and then truncate the head of the url to omit + # the scheme plus the following colon. This replicates the functionality of urllib.splittype. + scheme = urllib.parse.urlsplit(url)[0] + url = url[(len(scheme) + 1):] + if scheme == 'data': # syntax of data URLs: # dataurl := "data:" [ mediatype ] [ ";base64" ] "," data diff --git a/tests/tests_unit.py b/tests/tests_unit.py index 01555d188..d3c059c29 100644 --- a/tests/tests_unit.py +++ b/tests/tests_unit.py @@ -430,6 +430,17 @@ def test_correct_mimetypes_imported(self): self._test_mimetypes_import("win32", 3, 0, 0, False) self._test_mimetypes_import("darwin", 2, 7, 0, False) + def test_patched_mimetypes(self): + """ + Test patched mimetypes `guess_type` method to ensure it works as expected. + """ + from shotgun_api3.lib import mimetypes as mimetypes_patched + self.assertEqual(mimetypes_patched.guess_type("foo.html"), ("text/html", None)) + self.assertEqual(mimetypes_patched.guess_type("foo.tgz"), ("application/x-tar", "gzip")) + self.assertEqual(mimetypes_patched.guess_type("foo.tar.gz"), ("application/x-tar", "gzip")) + self.assertEqual(mimetypes_patched.guess_type("foo.tar.Z"), ("application/x-tar", "compress")) + self.assertEqual(mimetypes_patched.guess_type("foo.tar.bz2"), ("application/x-tar", "bzip2")) + self.assertEqual(mimetypes_patched.guess_type("foo.tar.xz"), ("application/x-tar", "xz")) if __name__ == '__main__': unittest.main()