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

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 yaml
29import logging
31from typing import (
32 TYPE_CHECKING,
33 Union,
34)
36from .configSupport import processLookupConfigs
37from .config import ConfigSubset
39if TYPE_CHECKING: 39 ↛ 40line 39 didn't jump to line 40, because the condition on line 39 was never true
40 from .dimensions import DimensionUniverse
41 from .._butlerConfig import ButlerConfig
42 from .datasets import DatasetRef, DatasetType
43 from .storageClass import StorageClass
44 from .configSupport import LookupKey
46log = logging.getLogger(__name__)
48# Key to access disassembly information
49DISASSEMBLY_KEY = "disassembled"
52class CompositesConfig(ConfigSubset):
53 component = "composites"
54 requiredKeys = ("default", DISASSEMBLY_KEY)
55 defaultConfigFile = "datastores/composites.yaml"
57 def validate(self) -> None:
58 """Validate entries have the correct type."""
59 super().validate()
60 # For now assume flat config with keys mapping to booleans
61 for k, v in self[DISASSEMBLY_KEY].items():
62 if not isinstance(v, bool):
63 raise ValueError(f"CompositesConfig: Key {k} is not a Boolean")
66class CompositesMap:
67 """Determine whether a specific datasetType or StorageClass should be
68 disassembled.
70 Parameters
71 ----------
72 config : `str`, `ButlerConfig`, or `CompositesConfig`
73 Configuration to control composites disassembly.
74 universe : `DimensionUniverse`
75 Set of all known dimensions, used to expand and validate any used
76 in lookup keys.
77 """
79 def __init__(self, config: Union[str, ButlerConfig, CompositesConfig], *,
80 universe: DimensionUniverse):
81 if not isinstance(config, CompositesConfig):
82 config = CompositesConfig(config)
83 assert isinstance(config, CompositesConfig)
84 self.config = config
86 # Pre-filter the disassembly lookup table to remove the
87 # placeholder __ key we added for documentation.
88 # It should be harmless but might confuse validation
89 # Retain the entry as a Config so change in place
90 disassemblyMap = self.config[DISASSEMBLY_KEY]
91 for k in set(disassemblyMap):
92 if k.startswith("__"):
93 del disassemblyMap[k]
95 # Calculate the disassembly lookup table -- no need to process
96 # the values
97 self._lut = processLookupConfigs(disassemblyMap, universe=universe)
99 def shouldBeDisassembled(self, entity: Union[DatasetRef, DatasetType, StorageClass]) -> bool:
100 """Given some choices, indicate whether the entity should be
101 disassembled.
103 Parameters
104 ----------
105 entity : `StorageClass` or `DatasetType` or `DatasetRef`
106 Thing to test against the configuration. The ``name`` property
107 is used to determine a match. A `DatasetType` will first check
108 its name, before checking its `StorageClass`. If there are no
109 matches the default will be returned. If the associated
110 `StorageClass` is not a composite, will always return `False`.
112 Returns
113 -------
114 disassemble : `bool`
115 Returns `True` if disassembly should occur; `False` otherwise.
117 Raises
118 ------
119 ValueError
120 The supplied argument is not understood.
121 """
123 if not hasattr(entity, "isComposite"):
124 raise ValueError(f"Supplied entity ({entity}) is not understood.")
126 # If this is not a composite there is nothing to disassemble.
127 if not entity.isComposite():
128 log.debug("%s will not be disassembled (not a composite)", entity)
129 return False
131 matchName: Union[LookupKey, str] = "{} (via default)".format(entity)
132 disassemble = self.config["default"]
134 for key in entity._lookupNames():
135 if key in self._lut:
136 disassemble = self._lut[key]
137 matchName = key
138 break
140 if not isinstance(disassemble, bool):
141 raise TypeError(
142 f"Got disassemble value {disassemble!r} for config entry {matchName!r}; expected bool."
143 )
145 log.debug("%s will%s be disassembled", matchName, "" if disassemble else " not")
146 return disassemble
148 def __str__(self) -> str:
149 result = {}
150 result["default"] = self.config["default"]
151 result["disassembled"] = {}
152 for key in self._lut:
153 result["disassembled"][str(key)] = self._lut[key]
154 return yaml.dump(result)