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 committed Jun 19, 2024
1 parent bfd75c9 commit 17ac11e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 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
17 changes: 16 additions & 1 deletion pyvo/dal/adhoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def get_adhocservice_by_id(self, id_):

class DatalinkResultsMixin(AdhocServiceResultsMixin):
"""
Mixin for datalink functionallity for results classes.
Mixin for datalink functionality for results classes.
"""

def iter_datalinks(self):
Expand Down Expand Up @@ -486,7 +486,13 @@ class DatalinkResults(DatalinkResultsMixin, DALResults):
argument is a string, it is interpreted as the name of a column,
and the data from the column matching that name is returned as
a Numpy array.
Where DatalinkResult instances were created from result table rows,
the row the DatalinkResult was generated from is available as
`original_row`. DatalinkResult instances generated by other means
have a ``None`` here.
"""
original_row = None

def getrecord(self, index):
"""
Expand Down Expand Up @@ -629,6 +635,15 @@ def get_first_proc(self):
return proc
raise IndexError("No processing service found in datalink result")

def set_original_row(self, row):
"""
note the table row this DatalinkResults instance was generated
from.
This is mostly for pyVO-internal use.
"""
self.original_row = row


class SodaRecordMixin:
"""
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='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 17ac11e

Please sign in to comment.