From 3f3e89f852ca6dcfb0e120cad79f0672a7e276d9 Mon Sep 17 00:00:00 2001 From: Markus Demleitner Date: Wed, 12 Jun 2024 15:57:34 +0200 Subject: [PATCH] Now passing strings to mimetypes.guess_extension. 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. --- CHANGES.rst | 6 ++++++ pyvo/dal/mimetype.py | 12 +++++++++--- pyvo/dal/sia.py | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index f9c338f4a..f4d669929 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,12 @@ 1.6 (unreleased) ================ +Bug Fixes +--------- + +- cachedataset() and friends again produce reasonable file extensions. + [#553] + Enhancements and Fixes ---------------------- diff --git a/pyvo/dal/mimetype.py b/pyvo/dal/mimetype.py index a78fb44ec..863b861fd 100644 --- a/pyvo/dal/mimetype.py +++ b/pyvo/dal/mimetype.py @@ -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 ---------- @@ -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): diff --git a/pyvo/dal/sia.py b/pyvo/dal/sia.py index dc861f368..e42f34dbc 100644 --- a/pyvo/dal/sia.py +++ b/pyvo/dal/sia.py @@ -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