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

59 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-12-01 19:55 +0000

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/>. 

21 

22from __future__ import annotations 

23 

24"""Support for reading and writing composite objects.""" 

25 

26__all__ = ("CompositesConfig", "CompositesMap") 

27 

28import yaml 

29import logging 

30 

31from typing import ( 

32 TYPE_CHECKING, 

33 Union, 

34) 

35 

36from .configSupport import processLookupConfigs 

37from .config import ConfigSubset 

38 

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 

45 

46log = logging.getLogger(__name__) 

47 

48# Key to access disassembly information 

49DISASSEMBLY_KEY = "disassembled" 

50 

51 

52class CompositesConfig(ConfigSubset): 

53 """Configuration specifics for Composites.""" 

54 

55 component = "composites" 

56 requiredKeys = ("default", DISASSEMBLY_KEY) 

57 defaultConfigFile = "datastores/composites.yaml" 

58 

59 def validate(self) -> None: 

60 """Validate entries have the correct type.""" 

61 super().validate() 

62 # For now assume flat config with keys mapping to booleans 

63 for k, v in self[DISASSEMBLY_KEY].items(): 

64 if not isinstance(v, bool): 

65 raise ValueError(f"CompositesConfig: Key {k} is not a Boolean") 

66 

67 

68class CompositesMap: 

69 """Determine whether something should be disassembled. 

70 

71 Compares a `DatasetType` or `StorageClass` with the map and determines 

72 whether disassembly is requested. 

73 

74 Parameters 

75 ---------- 

76 config : `str`, `ButlerConfig`, or `CompositesConfig` 

77 Configuration to control composites disassembly. 

78 universe : `DimensionUniverse` 

79 Set of all known dimensions, used to expand and validate any used 

80 in lookup keys. 

81 """ 

82 

83 def __init__(self, config: Union[str, ButlerConfig, CompositesConfig], *, 

84 universe: DimensionUniverse): 

85 if not isinstance(config, CompositesConfig): 

86 config = CompositesConfig(config) 

87 assert isinstance(config, CompositesConfig) 

88 self.config = config 

89 

90 # Pre-filter the disassembly lookup table to remove the 

91 # placeholder __ key we added for documentation. 

92 # It should be harmless but might confuse validation 

93 # Retain the entry as a Config so change in place 

94 disassemblyMap = self.config[DISASSEMBLY_KEY] 

95 for k in set(disassemblyMap): 

96 if k.startswith("__"): 

97 del disassemblyMap[k] 

98 

99 # Calculate the disassembly lookup table -- no need to process 

100 # the values 

101 self._lut = processLookupConfigs(disassemblyMap, universe=universe) 

102 

103 def shouldBeDisassembled(self, entity: Union[DatasetRef, DatasetType, StorageClass]) -> bool: 

104 """Indicate whether the entity should be disassembled. 

105 

106 Parameters 

107 ---------- 

108 entity : `StorageClass` or `DatasetType` or `DatasetRef` 

109 Thing to test against the configuration. The ``name`` property 

110 is used to determine a match. A `DatasetType` will first check 

111 its name, before checking its `StorageClass`. If there are no 

112 matches the default will be returned. If the associated 

113 `StorageClass` is not a composite, will always return `False`. 

114 

115 Returns 

116 ------- 

117 disassemble : `bool` 

118 Returns `True` if disassembly should occur; `False` otherwise. 

119 

120 Raises 

121 ------ 

122 ValueError 

123 The supplied argument is not understood. 

124 """ 

125 if not hasattr(entity, "isComposite"): 

126 raise ValueError(f"Supplied entity ({entity}) is not understood.") 

127 

128 # If this is not a composite there is nothing to disassemble. 

129 if not entity.isComposite(): 

130 log.debug("%s will not be disassembled (not a composite)", entity) 

131 return False 

132 

133 matchName: Union[LookupKey, str] = "{} (via default)".format(entity) 

134 disassemble = self.config["default"] 

135 

136 for key in entity._lookupNames(): 

137 if key in self._lut: 

138 disassemble = self._lut[key] 

139 matchName = key 

140 break 

141 

142 if not isinstance(disassemble, bool): 

143 raise TypeError( 

144 f"Got disassemble value {disassemble!r} for config entry {matchName!r}; expected bool." 

145 ) 

146 

147 log.debug("%s will%s be disassembled", matchName, "" if disassemble else " not") 

148 return disassemble 

149 

150 def __str__(self) -> str: 

151 result = {} 

152 result["default"] = self.config["default"] 

153 result["disassembled"] = {} 

154 for key in self._lut: 

155 result["disassembled"][str(key)] = self._lut[key] 

156 return yaml.dump(result)