Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion shotgun_api3/lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/tests_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()