Skip to content

Commit

Permalink
Now passing strings to mimetypes.guess_extension.
Browse files Browse the repository at this point in the history
Also changing the default extension to .dat (from None).

Also, whatever was originally implementing mime2extension did not include
the leading dot that mimetypes includes.  We are doing away with that, too.

This is supposed to address bug #552.
  • Loading branch information
msdemlei authored and bsipocz committed Jun 27, 2024
1 parent a993527 commit 3f3e89f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
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

0 comments on commit 3f3e89f

Please sign in to comment.