|
| 1 | +# This file is part of daf_butler. |
| 2 | +# |
| 3 | +# Developed for the LSST Data Management System. |
| 4 | +# This product includes software developed by the LSST Project |
| 5 | +# (http://www.lsst.org). |
| 6 | +# See the COPYRIGHT file at the top-level directory of this distribution |
| 7 | +# for details of code ownership. |
| 8 | +# |
| 9 | +# This software is dual licensed under the GNU General Public License and also |
| 10 | +# under a 3-clause BSD license. Recipients may choose which of these licenses |
| 11 | +# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, |
| 12 | +# respectively. If you choose the GPL option then the following text applies |
| 13 | +# (but note that there is still no warranty even if you opt for BSD instead): |
| 14 | +# |
| 15 | +# This program is free software: you can redistribute it and/or modify |
| 16 | +# it under the terms of the GNU General Public License as published by |
| 17 | +# the Free Software Foundation, either version 3 of the License, or |
| 18 | +# (at your option) any later version. |
| 19 | +# |
| 20 | +# This program is distributed in the hope that it will be useful, |
| 21 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | +# GNU General Public License for more details. |
| 24 | +# |
| 25 | +# You should have received a copy of the GNU General Public License |
| 26 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | + |
| 28 | +from __future__ import annotations |
| 29 | + |
| 30 | +__all__ = ("ButlerDatasetTypes",) |
| 31 | + |
| 32 | +from abc import ABC, abstractmethod |
| 33 | +from collections.abc import Iterable, Sequence, Set |
| 34 | +from typing import Any, overload |
| 35 | + |
| 36 | +from pydantic import BaseModel |
| 37 | + |
| 38 | +from ._dataset_type import DatasetType |
| 39 | +from ._storage_class import StorageClass |
| 40 | +from .dimensions import DimensionGroup |
| 41 | + |
| 42 | + |
| 43 | +class ButlerDatasetTypes(ABC, Sequence): |
| 44 | + """Methods for working with the dataset types known to the Butler.""" |
| 45 | + |
| 46 | + @abstractmethod |
| 47 | + def get(self, name: str) -> DatasetType: |
| 48 | + """Return the dataset type with the given name. |
| 49 | +
|
| 50 | + Returns |
| 51 | + ------- |
| 52 | + dataset_type : `DatasetType` |
| 53 | + Dataset type object with the given name. |
| 54 | +
|
| 55 | + Raises |
| 56 | + ------ |
| 57 | + MissingDatasetTypeError |
| 58 | + Raised if there is no dataset type with the given name. |
| 59 | + """ |
| 60 | + raise NotImplementedError() |
| 61 | + |
| 62 | + @abstractmethod |
| 63 | + def query( |
| 64 | + self, |
| 65 | + name: str | Iterable[str], |
| 66 | + *, |
| 67 | + at_least_dimensions: Iterable[str] | DimensionGroup | None = None, |
| 68 | + exact_dimensions: Iterable[str] | DimensionGroup | None = None, |
| 69 | + storage_class: str | Iterable[str] | StorageClass | Iterable[StorageClass] | None = None, |
| 70 | + is_calibration: bool | None = None, |
| 71 | + ) -> Iterable[DatasetType]: |
| 72 | + """Query for dataset types matching the given criteria. |
| 73 | +
|
| 74 | + Parameters |
| 75 | + ---------- |
| 76 | + name : `str` or `~collections.abc.Iterable` [ `str` ] |
| 77 | + Names or name patterns (glob-style) that returned dataset type |
| 78 | + names must match. If an iterable, items are OR'd together. |
| 79 | + at_least_dimensions : `Iterable` [ `str` ] or `DimensionGroup`,\ |
| 80 | + optional |
| 81 | + Dimensions that returned dataset types must have as a subset. |
| 82 | + at_least_dimensions : `Iterable` [ `str` ] or `DimensionGroup`,\ |
| 83 | + optional |
| 84 | + Dimensions that returned dataset types must have exactly. |
| 85 | + with_storage_class : `str` or `~collections.abc.Iterable` [ `str` ],\ |
| 86 | + or `StorageClass` or \ |
| 87 | + `~collections.abc.Iterable` [ `StorageClass` ], optional |
| 88 | + Storage classes or storage class names that returned dataset types |
| 89 | + must have. If an iterable, items are OR'd together. |
| 90 | + is_calibration : `bool` or `None`, optional |
| 91 | + If `None`, constrain returned dataset types to be or not be |
| 92 | + calibrations. |
| 93 | +
|
| 94 | + Returns |
| 95 | + ------- |
| 96 | + dataset_types : `~collections.abc.Iterable` [ `DatasetType` |
| 97 | + An iterable of dataset types. This is guaranteed to be a regular |
| 98 | + Python in-memory container, not a lazy single-pass iterator, but |
| 99 | + the type of container is currently left unspecified in order to |
| 100 | + leave room for future convenience behavior. |
| 101 | +
|
| 102 | + Notes |
| 103 | + ----- |
| 104 | + This method queries all registered dataset types in registry. To query |
| 105 | + for the types of datasets that are in a collection, instead use:: |
| 106 | +
|
| 107 | + info = butler.collections.query_info( |
| 108 | + collections, |
| 109 | + include_summaries=True, |
| 110 | + ) |
| 111 | +
|
| 112 | + for a simple summary of the dataset types in each collection (see |
| 113 | + `lsst.daf.butler.ButlerCollections.query_info`). Or, for |
| 114 | + more complex but powerful queries (including constraints on data IDs or |
| 115 | + dataset counts), use:: |
| 116 | +
|
| 117 | + with butler.query() as q: |
| 118 | + dataset_types = q.dataset_types(collections) |
| 119 | +
|
| 120 | + See `lsst.daf.butler.queries.Query.dataset_types` for details. |
| 121 | + """ |
| 122 | + raise NotImplementedError() |
| 123 | + |
| 124 | + @abstractmethod |
| 125 | + def query_names( |
| 126 | + self, |
| 127 | + name: str | Iterable[str], |
| 128 | + *, |
| 129 | + at_least_dimensions: Iterable[str] | DimensionGroup | None = None, |
| 130 | + exact_dimensions: Iterable[str] | DimensionGroup | None = None, |
| 131 | + storage_class: str | Iterable[str] | StorageClass | Iterable[StorageClass] | None = None, |
| 132 | + is_calibration: bool | None = None, |
| 133 | + ) -> Iterable[str]: |
| 134 | + """Query for the names of dataset types matching the given criteria. |
| 135 | +
|
| 136 | + See `query` for parameter descriptions. |
| 137 | + """ |
| 138 | + raise NotImplementedError() |
| 139 | + |
| 140 | + @abstractmethod |
| 141 | + def register( |
| 142 | + self, |
| 143 | + name_or_type: str, |
| 144 | + /, |
| 145 | + dimensions: Iterable[str] | DimensionGroup | None = None, |
| 146 | + storage_class: str | StorageClass | None = None, |
| 147 | + is_calibration: bool | None = None, |
| 148 | + ) -> bool: |
| 149 | + """Register a dataset type. |
| 150 | +
|
| 151 | + It is not an error to register the same `DatasetType` twice. |
| 152 | +
|
| 153 | + Parameters |
| 154 | + ---------- |
| 155 | + name_or_type : `str` or `DatasetType` |
| 156 | + The name of the dataset type to be added, or a complete |
| 157 | + `DatasetType` type object to add. |
| 158 | + dimensions : `~colletions.abc.Iterable` [ `str` ] or `DimensionGroup`,\ |
| 159 | + optional |
| 160 | + Dimensions for the dataset type. Required if the first argument |
| 161 | + is just a `str`, and overrides the dimensions if the first argument |
| 162 | + is a `DatasetType`. |
| 163 | + storage_class : `str` or `StorageClass`, optional |
| 164 | + Storage class for the dataset type. Required if the first argument |
| 165 | + is just a `str`, and overrides the storage class if the first |
| 166 | + arguemnt is a `DatasetType`. |
| 167 | + is_calibration: `bool`, optional |
| 168 | + Whether the dataset type is a calibration. If the first argument |
| 169 | + is a `str`, defaults to `False`. If the first argument is a |
| 170 | + `DatasetType` and this argument is not `None`, it overrides the |
| 171 | + value on the `DatasetType`. |
| 172 | +
|
| 173 | + Returns |
| 174 | + ------- |
| 175 | + inserted : `bool` |
| 176 | + `True` if a new dataset type was inserted, `False` if an identical |
| 177 | + existing dataset type was found. Note that in either case the |
| 178 | + dataset type is guaranteed to be defined in the repository |
| 179 | + consistently with the given definition. |
| 180 | +
|
| 181 | + Raises |
| 182 | + ------ |
| 183 | + ValueError |
| 184 | + Raised if the dimensions or storage class are invalid. |
| 185 | + lsst.daf.butler.registry.ConflictingDefinitionError |
| 186 | + Raised if this dataset type is already registered with a different |
| 187 | + definition. |
| 188 | +
|
| 189 | + """ |
| 190 | + raise NotImplementedError() |
| 191 | + |
| 192 | + @abstractmethod |
| 193 | + def remove(self, name: str) -> None: |
| 194 | + """Remove the dataset type with the given name. |
| 195 | +
|
| 196 | + .. warning:: |
| 197 | +
|
| 198 | + Butler implementations can cache the dataset type definitions. |
| 199 | + This means that deleting the dataset type definition may result in |
| 200 | + unexpected behavior from other butler processes that are active |
| 201 | + that have not seen the deletion. |
| 202 | +
|
| 203 | + Parameters |
| 204 | + ---------- |
| 205 | + name : `str` or `tuple` [`str`] |
| 206 | + Name of the type to be removed or tuple containing a list of type |
| 207 | + names to be removed. Wildcards are allowed. |
| 208 | +
|
| 209 | + Raises |
| 210 | + ------ |
| 211 | + lsst.daf.butler.registry.OrphanedRecordError |
| 212 | + Raised if an attempt is made to remove the dataset type definition |
| 213 | + when there are still datasets associated with it. |
| 214 | +
|
| 215 | + Notes |
| 216 | + ----- |
| 217 | + If the dataset type is not registered the method will return without |
| 218 | + action. |
| 219 | + """ |
| 220 | + raise NotImplementedError() |
0 commit comments