Coverage for python/lsst/daf/butler/core/composites.py : 30%

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
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 program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22from __future__ import annotations
24"""Support for reading and writing composite objects."""
26__all__ = ("CompositesConfig", "CompositesMap")
28import logging
30from typing import (
31 TYPE_CHECKING,
32 Union,
33)
35from .configSupport import processLookupConfigs
36from .config import ConfigSubset
38if TYPE_CHECKING: 38 ↛ 39line 38 didn't jump to line 39, because the condition on line 38 was never true
39 from .dimensions import DimensionUniverse
40 from .._butlerConfig import ButlerConfig
41 from .datasets import DatasetRef, DatasetType
42 from .storageClass import StorageClass
43 from .configSupport import LookupKey
45log = logging.getLogger(__name__)
47# Key to access disassembly information
48DISASSEMBLY_KEY = "disassembled"
51class CompositesConfig(ConfigSubset):
52 component = "composites"
53 requiredKeys = ("default", DISASSEMBLY_KEY)
54 defaultConfigFile = "datastores/composites.yaml"
56 def validate(self) -> None:
57 """Validate entries have the correct type."""
58 super().validate()
59 # For now assume flat config with keys mapping to booleans
60 for k, v in self[DISASSEMBLY_KEY].items():
61 if not isinstance(v, bool):
62 raise ValueError(f"CompositesConfig: Key {k} is not a Boolean")
65class CompositesMap:
66 """Determine whether a specific datasetType or StorageClass should be
67 disassembled.
69 Parameters
70 ----------
71 config : `str`, `ButlerConfig`, or `CompositesConfig`
72 Configuration to control composites disassembly.
73 universe : `DimensionUniverse`
74 Set of all known dimensions, used to expand and validate any used
75 in lookup keys.
76 """
78 def __init__(self, config: Union[str, ButlerConfig, CompositesConfig], *,
79 universe: DimensionUniverse):
80 if not isinstance(config, CompositesConfig):
81 config = CompositesConfig(config)
82 assert isinstance(config, CompositesConfig)
83 self.config = config
85 # Calculate the disassembly lookup table -- no need to process
86 # the values
87 self._lut = processLookupConfigs(self.config[DISASSEMBLY_KEY], universe=universe)
89 def shouldBeDisassembled(self, entity: Union[DatasetRef, DatasetType, StorageClass]) -> bool:
90 """Given some choices, indicate whether the entity should be
91 disassembled.
93 Parameters
94 ----------
95 entity : `StorageClass` or `DatasetType` or `DatasetRef`
96 Thing to test against the configuration. The ``name`` property
97 is used to determine a match. A `DatasetType` will first check
98 its name, before checking its `StorageClass`. If there are no
99 matches the default will be returned. If the associated
100 `StorageClass` is not a composite, will always return `False`.
102 Returns
103 -------
104 disassemble : `bool`
105 Returns `True` if disassembly should occur; `False` otherwise.
107 Raises
108 ------
109 ValueError
110 The supplied argument is not understood.
111 """
113 if not hasattr(entity, "isComposite"):
114 raise ValueError(f"Supplied entity ({entity}) is not understood.")
116 # If this is not a composite there is nothing to disassemble.
117 if not entity.isComposite():
118 log.debug("%s will not be disassembled (not a composite)", entity)
119 return False
121 matchName: Union[LookupKey, str] = "{} (via default)".format(entity)
122 disassemble = self.config["default"]
124 for key in entity._lookupNames():
125 if key in self._lut:
126 disassemble = self._lut[key]
127 matchName = key
128 break
130 log.debug("%s will%s be disassembled", matchName, "" if disassemble else " not")
131 return disassemble