Skip to content

Commit

Permalink
Merge pull request #613 from d-giles/getdatalink-328-2
Browse files Browse the repository at this point in the history
Getdatalink - guess or return more informative error
  • Loading branch information
bsipocz authored Oct 25, 2024
2 parents 6cac7c9 + 382b50b commit c49124a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Enhancements and Fixes

- New sub-package discover for global dataset discovery. [#470]

- Updated getdatalink to be consistent with iter_datalinks. [#613]


Deprecations and Removals
-------------------------
Expand Down
31 changes: 29 additions & 2 deletions pyvo/dal/adhoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ def _guess_access_url(row):
if access_url:
return access_url

@staticmethod
def _guess_datalink(row, **kwargs):
# TODO: we should be more careful about whitespace, case
# and perhaps more parameters in the following comparison
if row._results._guess_access_format(row) == DATALINK_MIME_TYPE:
access_url = row._results._guess_access_url(row)
if access_url is not None:
return DatalinkResults.from_result_url(access_url, **kwargs)

def _iter_datalinks_from_product_rows(self):
"""yield datalinks from self's rows if they describe datalink-valued
products.
Expand Down Expand Up @@ -309,13 +318,31 @@ class DatalinkRecordMixin:
"""

def getdatalink(self):
"""
Retrieve the datalink information for this record.
Returns
-------
DatalinkResults
The datalink results for this record.
Raises
------
DALServiceError
If no datalink information is found for this record.
"""
try:
datalink = self._results.get_adhocservice_by_ivoid(DATALINK_IVOID)

query = DatalinkQuery.from_resource(self, datalink, session=self._session)
return query.execute()
except DALServiceError:
return DatalinkResults.from_result_url(self.getdataurl(), session=self._session)
except DALServiceError as error:
datalink = self._results._guess_datalink(self, session=self._session)
if datalink is not None:
return datalink
else:
# re-raise the original error if nothing works
raise DALServiceError("No datalink found for record.") from error

@stream_decode_content
def getdataset(self, timeout=None):
Expand Down
36 changes: 35 additions & 1 deletion pyvo/dal/tests/test_datalink.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
Tests for pyvo.dal.datalink
"""
from functools import partial
import re

import pytest

import pyvo as vo
from pyvo.dal.adhoc import DatalinkResults
from pyvo.dal.adhoc import DatalinkResults, DALServiceError
from pyvo.dal.sia2 import SIA2Results
from pyvo.dal.tap import TAPResults
from pyvo.utils import testing, vocabularies
from pyvo.dal.sia import search

from astropy.utils.data import get_pkg_data_contents, get_pkg_data_filename

Expand All @@ -30,6 +32,27 @@ def callback(request, context):
yield matcher


sia_re = re.compile("http://example.com/sia.*")


@pytest.fixture()
def register_mocks(mocker):
with mocker.register_uri(
"GET",
"http://example.com/querydata/image.fits",
content=get_pkg_data_contents("data/querydata/image.fits"),
) as matcher:
yield matcher


@pytest.fixture()
def sia(mocker):
with mocker.register_uri(
"GET", sia_re, content=get_pkg_data_contents("data/sia/dataset.xml")
) as matcher:
yield matcher


@pytest.fixture()
def datalink(mocker):
def callback(request, context):
Expand Down Expand Up @@ -307,3 +330,14 @@ def test_generic_record(self):
assert len(links) == 1
assert (next(links[0].bysemantics("#this"))["access_url"]
== "http://dc.zah.uni-heidelberg.de/getproduct/flashheros/data/ca90/f0011.mt")


@pytest.mark.usefixtures("sia", "register_mocks")
@pytest.mark.filterwarnings("ignore::astropy.io.votable.exceptions.W06")
def test_no_datalink():
# for issue #328 getdatalink() exits messily when there isn't a datalink

results = search("http://example.com/sia", pos=(288, 15), format="all")
result = results[0]
with pytest.raises(DALServiceError, match="No datalink found for record."):
result.getdatalink()

0 comments on commit c49124a

Please sign in to comment.