Coverage for python/lsst/daf/butler/butler.py : 76%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# This file is part of daf_butler. # # Developed for the LSST Data Management System. # This product includes software developed by the LSST Project # (http://www.lsst.org). # See the COPYRIGHT file at the top-level directory of this distribution # for details of code ownership. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>.
Butler top level classes. """
"""Main entry point for the data access system.
Attributes ---------- config : `str`, `ButlerConfig` or `Config`, optional (filename to) configuration. If this is not a `ButlerConfig`, defaults will be read. If a `str`, may be the path to a directory containing a "butler.yaml" file. datastore : `Datastore` Datastore to use for storage. registry : `Registry` Registry to use for lookups.
Parameters ---------- config : `Config` Configuration. collection : `str`, optional Collection to use for all input lookups, overriding config["collection"] if provided. run : `str`, `Run`, optional Collection associated with the `Run` to use for outputs, overriding config["run"]. If a `Run` associated with the given Collection does not exist, it will be created. If "collection" is None, this collection will be used for input lookups as well; if not, it must have the same value as "run".
Raises ------ ValueError Raised if neither "collection" nor "run" are provided by argument or config, or if both are provided and are inconsistent. """
"""Create an empty data repository by adding a butler.yaml config to a repository root directory.
Parameters ---------- root : `str` Filesystem path to the root of the new repository. Will be created if it does not exist. config : `Config`, optional Configuration to write to the repository, after setting any root-dependent Registry or Datastore config options. If `None`, default configuration will be used. standalone : `bool` If True, write all expanded defaults, not just customized or repository-specific settings. This (mostly) decouples the repository from the default configuration, insulating it from changes to the defaults (which may be good or bad, depending on the nature of the changes). Future *additions* to the defaults will still be picked up when initializing `Butlers` to repos created with ``standalone=True``. createRegistry : `bool` If `True` create a new Registry.
Note that when ``standalone=False`` (the default), the configuration search path (see `ConfigSubset.defaultSearchPaths`) that was used to construct the repository should also be used to construct any Butlers to it to avoid configuration inconsistencies.
Returns ------- config : `Config` The updated `Config` instance written to the repo.
Raises ------ ValueError Raised if a ButlerConfig or ConfigSubset is passed instead of a regular Config (as these subclasses would make it impossible to support ``standalone=False``). os.error Raised if the directory does not exist, exists but is not a directory, or cannot be created. """ raise ValueError("makeRepo must be passed a regular Config without defaults applied.") os.makedirs(root) # Create Registry and populate tables
else: if isinstance(run, Run): self.run = run runCollection = self.run.collection else: runCollection = run self.run = None # if run *arg* is not None and collection arg is, use run for collecion. if collection is None: collection = runCollection raise ValueError("No run or collection provided.") raise ValueError( "Run ({}) and collection ({}) are inconsistent.".format(runCollection, collection) )
"""Support pickling. """
self.collection, self.datastore, self.registry)
def transaction(self): """Context manager supporting `Butler` transactions.
Transactions can be nested. """
"""Store and register a dataset.
Parameters ---------- obj : `object` The dataset. datasetRefOrType : `DatasetRef`, `DatasetType` instance or `str` When `DatasetRef` the `dataId` should be `None`. Otherwise the `DatasetType` or name thereof. dataId : `dict`, optional An identifier with `DataUnit` names and values. When `None` a `DatasetRef` should be supplied as the second argument. producer : `Quantum`, optional The producer.
Returns ------- ref : `DatasetRef` A reference to the stored dataset, updated with the correct id if given.
Raises ------ TypeError Raised if the butler was not constructed with a Run, and is hence read-only. """ raise TypeError("Butler is read-only.") raise ValueError("DatasetRef given, cannot use dataId as well") else: raise ValueError("Must provide a dataId if first argument is not a DatasetRef")
# Add Registry Dataset entry. If not a virtual composite, add # and attach components at the same time. recursive=not isVirtualComposite)
# Check to see if this datasetType requires disassembly else: # This is an entity without a disassembler.
"""Retrieve a stored dataset.
Unlike `Butler.get`, this method allows datasets outside the Butler's collection to be read as long as the `DatasetRef` that identifies them can be obtained separately.
Parameters ---------- ref : `DatasetRef` Reference to an already stored dataset. parameters : `dict` Additional StorageClass-defined options to control reading, typically used to efficiently read only a subset of the dataset.
Returns ------- obj : `object` The dataset. """ # if the ref exists in the store we return it directly # Reconstruct the composite else: # make a dictionary of parameters containing only the subset # supported by the StorageClass of the components compParams = {k: v for k, v in parameters.items() if k in compRef.datasetType.storageClass.components}
# Assemble the components else: # single entity in datastore raise ValueError("Unable to locate ref {} in datastore {}".format(ref.id, self.datastore.name))
"""Retrieve a stored dataset.
Parameters ---------- datasetRefOrType : `DatasetRef`, `DatasetType` instance or `str` When `DatasetRef` the `dataId` should be `None`. Otherwise the `DatasetType` or name thereof. dataId : `dict` A `dict` of `DataUnit` link name, value pairs that label the `DatasetRef` within a Collection. When `None` a `DatasetRef` should be supplied as the second argument. parameters : `dict` Additional StorageClass-defined options to control reading, typically used to efficiently read only a subset of the dataset.
Returns ------- obj : `object` The dataset. """ else: # Always lookup the DatasetRef, even if one is given, to ensure it is # present in the current collection. raise LookupError("Dataset {} with data ID {} could not be found in {}".format( datasetType.name, dataId, self.collection))
"""Return the URI to the Dataset.
Parameters ---------- datasetType : `DatasetType` instance or `str` The `DatasetType`. dataId : `dict` A `dict` of `DataUnit` link name, value pairs that label the `DatasetRef` within a Collection. predict : `bool` If `True`, allow URIs to be returned of datasets that have not been written.
Returns ------- uri : `str` URI string pointing to the Dataset within the datastore. If the Dataset does not exist in the datastore, and if ``predict`` is `True`, the URI will be a prediction and will include a URI fragment "#predicted". If the datastore does not have entities that relate well to the concept of a URI the returned URI string will be descriptive. The returned URI is not guaranteed to be obtainable.
Raises ------ FileNotFoundError A URI has been requested for a dataset that does not exist and guessing is not allowed. """ datasetType = self.registry.getDatasetType(datasetType) ref = self.registry.find(self.collection, datasetType, dataId) return self.datastore.getUri(ref, predict)
"""Return True if the Dataset is actually present in the Datastore.
Parameters ---------- datasetType : `DatasetType` instance or `str` The `DatasetType`. dataId : `dict` A `dict` of `DataUnit` link name, value pairs that label the `DatasetRef` within a Collection.
Raises ------ LookupError Raised if the Dataset is not even present in the Registry. """ datasetType = self.registry.getDatasetType(datasetType) ref = self.registry.find(self.collection, datasetType, dataId) if ref is None: raise LookupError( "{} with {} not found in collection {}".format(datasetType, dataId, self.collection) ) return self.datastore.exists(ref) |