Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Now passing strings to mimetypes.guess_extension. #553

Merged
merged 1 commit into from
Jun 27, 2024
Merged
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
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
1.6 (unreleased)
================

Bug Fixes
---------

- cachedataset() and friends again produce reasonable file extensions.
[#553]

Enhancements and Fixes
----------------------

Expand Down
12 changes: 9 additions & 3 deletions pyvo/dal/mimetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ def mime2extension(mimetype, default=None):
implementations of ``suggest_extension()`` in ``Record`` subclasses.

>>> mime2extension('application/fits')
'fits'
>>> mime2extension('image/jpeg')
'jpg'
>>> mime2extension('application/x-zed', 'dat')
'dat'

Parameters
----------
Expand All @@ -46,11 +49,14 @@ def mime2extension(mimetype, default=None):
if not mimetype:
return default

if isinstance(mimetype, str):
mimetype = mimetype.encode('utf-8')
if isinstance(mimetype, bytes):
mimetype = mimetype.decode('utf-8')

ext = mimetypes.guess_extension(mimetype, strict=False)
return ext
if ext is None:
return default

return ext.lstrip(".")


def mime_object_maker(url, mimetype, *, session=None):
Expand Down
2 changes: 1 addition & 1 deletion pyvo/dal/sia.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ def suggest_dataset_basename(self):
out = re.sub(r'\s+', '_', out.strip())
return out

def suggest_extension(self, *, default=None):
def suggest_extension(self, *, default='dat'):
"""
returns a recommended filename extension for the dataset described
by this record. Typically, this would look at the column describing
Expand Down
Loading