Skip to content

Commit

Permalink
Change det_dataset_from_uri so it returns a Butler
Browse files Browse the repository at this point in the history
Otherwise there is no easy way for the caller to get the
dataset:

butler, ref = Butler.get_dataset_from_uri(uri)
thing = butler.get(ref)
  • Loading branch information
timj committed Nov 26, 2024
1 parent c243a12 commit 8be2fc4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
9 changes: 6 additions & 3 deletions python/lsst/daf/butler/_butler.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def parse_dataset_uri(cls, uri: str) -> tuple[str, DatasetId]:
@classmethod
def get_dataset_from_uri(
cls, uri: str, factory: LabeledButlerFactoryProtocol | None = None
) -> DatasetRef | None:
) -> tuple[Butler, DatasetRef | None]:
"""Get the dataset associated with the given dataset URI.
Parameters
Expand All @@ -596,10 +596,13 @@ def get_dataset_from_uri(
The URI associated with a dataset.
factory : `LabeledButlerFactoryProtocol` or `None`, optional
Bound factory function that will be given the butler label
and receive a `Butler`.
and receive a `Butler`. If this is not provided the label
will be tried directly.
Returns
-------
butler : `Butler`
Butler object associated with this URI.
ref : `DatasetRef` or `None`
The dataset associated with that URI, or `None` if the UUID
is valid but the dataset is not known to this butler.
Expand All @@ -614,7 +617,7 @@ def get_dataset_from_uri(
pass
if butler is None:
butler = cls.from_config(label)
return butler.get_dataset(dataset_id)
return butler, butler.get_dataset(dataset_id)

@abstractmethod
def _caching_context(self) -> AbstractContextManager[None]:
Expand Down
14 changes: 11 additions & 3 deletions tests/test_simpleButler.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,15 +917,23 @@ def test_dataset_uris(self):
f"butler://{label}/{ref.id}",
f"ivo://rubin.lsst/datasets?{label}/{ref.id}",
):
ref2 = Butler.get_dataset_from_uri(dataset_uri)
new_butler, ref2 = Butler.get_dataset_from_uri(dataset_uri)
self.assertEqual(ref, ref2)
# The returned butler needs to have the datastore mocked.
DatastoreMock.apply(new_butler)
dataset_id, _ = butler.get(ref2)
self.assertEqual(dataset_id, ref.id)

ref2 = Butler.get_dataset_from_uri(dataset_uri, factory=factory)
factory_butler, ref2 = Butler.get_dataset_from_uri(dataset_uri, factory=factory)
self.assertEqual(ref, ref2)
# The returned butler needs to have the datastore mocked.
DatastoreMock.apply(factory_butler)
dataset_id, _ = factory_butler.get(ref2)
self.assertEqual(dataset_id, ref.id)

# Non existent dataset.
missing_id = str(ref.id).replace("2", "3")
no_ref = Butler.get_dataset_from_uri(f"butler://{label}/{missing_id}")
_, no_ref = Butler.get_dataset_from_uri(f"butler://{label}/{missing_id}")
self.assertIsNone(no_ref)

# Test some failure modes.
Expand Down

0 comments on commit 8be2fc4

Please sign in to comment.