Skip to content

Commit

Permalink
Use consistent error message for getURI if multiple URIs retrieved
Browse files Browse the repository at this point in the history
  • Loading branch information
timj committed May 20, 2020
1 parent ad7b7cf commit 63623ff
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
13 changes: 9 additions & 4 deletions python/lsst/daf/butler/_butler.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,11 +930,16 @@ def getURI(self, datasetRefOrType: Union[DatasetRef, DatasetType, str],
differs from the one found in the registry.
TypeError
Raised if no collections were provided.
RuntimeError
Raised if a URI is requested for a dataset that consists of
multiple artifacts.
"""
primary, _ = self.getURIs(datasetRefOrType, dataId=dataId, predict=predict,
collections=collections, run=run, **kwds)
if primary is None:
raise RuntimeError(f"Found dataset but no single URI retrieved for it {datasetRefOrType}")
primary, components = self.getURIs(datasetRefOrType, dataId=dataId, predict=predict,
collections=collections, run=run, **kwds)

if primary is None or components:
raise RuntimeError(f"Dataset ({datasetRefOrType}) includes distinct URIs for components. "
"Use Butler.getURIs() instead.")
return primary

def datasetExists(self, datasetRefOrType: Union[DatasetRef, DatasetType, str],
Expand Down
13 changes: 8 additions & 5 deletions python/lsst/daf/butler/datastores/chainedDatastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,16 @@ def getURI(self, ref: DatasetRef, predict: bool = False) -> ButlerURI:
------
FileNotFoundError
A URI has been requested for a dataset that does not exist and
guessing is not allowed. Can also raise if the dataset is
present but it has been disassembled into multiple artifacts.
guessing is not allowed.
RuntimeError
Raised if a request is made for a single URI but multiple URIs
are associated with this dataset.
"""
log.debug("Requesting URI for %s", ref)
primary, _ = self.getURIs(ref, predict)
if primary is None:
raise FileNotFoundError(f"Found dataset but no single URI possible for {ref}")
primary, components = self.getURIs(ref, predict)
if primary is None or components:
raise RuntimeError(f"Dataset ({ref}) includes distinct URIs for components. "
"Use Dataastore.getURIs() instead.")
return primary

def remove(self, ref: DatasetRef) -> None:
Expand Down
17 changes: 10 additions & 7 deletions python/lsst/daf/butler/datastores/fileLikeDatastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,19 +914,22 @@ def getURI(self, ref: DatasetRef, predict: bool = False) -> ButlerURI:
Raises
------
FileNotFoundError
A URI has been requested for a dataset that does not exist and
guessing is not allowed. Can also raise if the dataset is
present but it has been disassembled into multiple artifacts.
Raised if a URI has been requested for a dataset that does not
exist and guessing is not allowed.
RuntimeError
Raised if a request is made for a single URI but multiple URIs
are associated with this dataset.
Notes
-----
When a predicted URI is requested an attempt will be made to form
a reasonable URI based on file templates and the expected formatter.
"""
uri, components = self.getURIs(ref, predict)
if uri is None:
raise FileNotFoundError(f"Found dataset but no single URI possible for {ref}")
return uri
primary, components = self.getURIs(ref, predict)
if primary is None or components:
raise RuntimeError(f"Dataset ({ref}) includes distinct URIs for components. "
"Use Dataastore.getURIs() instead.")
return primary

def get(self, ref: DatasetRef, parameters: Optional[Mapping[str, Any]] = None) -> Any:
"""Load an InMemoryDataset from the store.
Expand Down
13 changes: 8 additions & 5 deletions python/lsst/daf/butler/datastores/inMemoryDatastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,15 @@ def getURI(self, ref: DatasetRef, predict: bool = False) -> ButlerURI:
FileNotFoundError
A URI has been requested for a dataset that does not exist and
guessing is not allowed.
AssertionError
Raised if an internal error occurs.
"""
uri, _ = self.getURIs(ref, predict)
if uri is None:
raise RuntimeError(f"Unexpectedly got no URI for in-memory datastore for {ref}")
return uri
primary, _ = self.getURIs(ref, predict)
if primary is None:
# This should be impossible since this datastore does
# not disassemble. This check also helps mypy.
raise AssertionError(f"Unexpectedly got no URI for in-memory datastore for {ref}")
return primary

def trash(self, ref: DatasetRef, ignore_errors: bool = False) -> None:
"""Indicate to the Datastore that a dataset can be removed.
Expand Down

0 comments on commit 63623ff

Please sign in to comment.