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 for images to .img (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 committed Jun 13, 2024
1 parent bfd75c9 commit 16bc54f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
8 changes: 7 additions & 1 deletion 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 All @@ -23,7 +29,7 @@ Enhancements and Fixes
them. [#517]

- Introducing the new MIVOT module, enabling processed VOTable data mapped to
any model serialized in VO-DML. This package dynamically generates python objects
any model serialized in VO-DML. This package dynamically generates python objects
whose structure corresponds to the classes of the mapped models. [#497]

Deprecations and Removals
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 @@ -903,7 +903,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='img'):
"""
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 16bc54f

Please sign in to comment.