Hide keyboard shortcuts

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

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 component = "composites" 

54 requiredKeys = ("default", DISASSEMBLY_KEY) 

55 defaultConfigFile = "datastores/composites.yaml" 

56 

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") 

64 

65 

66class CompositesMap: 

67 """Determine whether a specific datasetType or StorageClass should be 

68 disassembled. 

69 

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 """ 

78 

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 

85 

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

87 # the values 

88 self._lut = processLookupConfigs(self.config[DISASSEMBLY_KEY], universe=universe) 

89 

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

91 """Given some choices, indicate whether the entity should be 

92 disassembled. 

93 

94 Parameters 

95 ---------- 

96 entity : `StorageClass` or `DatasetType` or `DatasetRef` 

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

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

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

100 matches the default will be returned. If the associated 

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

102 

103 Returns 

104 ------- 

105 disassemble : `bool` 

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

107 

108 Raises 

109 ------ 

110 ValueError 

111 The supplied argument is not understood. 

112 """ 

113 

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

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

116 

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

118 if not entity.isComposite(): 

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

120 return False 

121 

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

123 disassemble = self.config["default"] 

124 

125 for key in entity._lookupNames(): 

126 if key in self._lut: 

127 disassemble = self._lut[key] 

128 matchName = key 

129 break 

130 

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

132 return disassemble 

133 

134 def __str__(self) -> str: 

135 result = {} 

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

137 result["disassembled"] = {} 

138 for key in self._lut: 

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

140 return yaml.dump(result)