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 

22__all__ = ("DatasetTestHelper", "DatastoreTestHelper", 

23 "BadWriteFormatter", "BadNoWriteFormatter", "MultiDetectorFormatter") 

24 

25import os 

26from lsst.daf.butler import DatasetType, DatasetRef 

27from lsst.daf.butler.formatters.yaml import YamlFormatter 

28 

29 

30class DatasetTestHelper: 

31 """Helper methods for Datasets""" 

32 

33 def makeDatasetRef(self, datasetTypeName, dimensions, storageClass, dataId, *, id=None, run=None, 

34 conform=True): 

35 """Make a DatasetType and wrap it in a DatasetRef for a test""" 

36 return self._makeDatasetRef(datasetTypeName, dimensions, storageClass, dataId, id=id, run=run, 

37 conform=conform) 

38 

39 def _makeDatasetRef(self, datasetTypeName, dimensions, storageClass, dataId, *, id=None, run=None, 

40 conform=True): 

41 # helper for makeDatasetRef 

42 datasetType = DatasetType(datasetTypeName, dimensions, storageClass) 

43 if id is None: 

44 self.id += 1 

45 id = self.id 

46 if run is None: 

47 run = "dummy" 

48 return DatasetRef(datasetType, dataId, id=id, run=run, conform=conform) 

49 

50 

51class DatastoreTestHelper: 

52 """Helper methods for Datastore tests""" 

53 

54 def setUpDatastoreTests(self, registryClass, configClass): 

55 """Shared setUp code for all Datastore tests""" 

56 self.registry = registryClass() 

57 

58 # Need to keep ID for each datasetRef since we have no butler 

59 # for these tests 

60 self.id = 1 

61 

62 self.config = configClass(self.configFile) 

63 

64 # Some subclasses override the working root directory 

65 if self.root is not None: 

66 self.datastoreType.setConfigRoot(self.root, self.config, self.config.copy()) 

67 

68 def makeDatastore(self, sub=None): 

69 """Make a new Datastore instance of the appropriate type. 

70 

71 Parameters 

72 ---------- 

73 sub : str, optional 

74 If not None, the returned Datastore will be distinct from any 

75 Datastore constructed with a different value of ``sub``. For 

76 PosixDatastore, for example, the converse is also true, and ``sub`` 

77 is used as a subdirectory to form the new root. 

78 

79 Returns 

80 ------- 

81 datastore : `Datastore` 

82 Datastore constructed by this routine using the supplied 

83 optional subdirectory if supported. 

84 """ 

85 config = self.config.copy() 

86 if sub is not None and self.root is not None: 

87 self.datastoreType.setConfigRoot(os.path.join(self.root, sub), config, self.config) 

88 if sub is not None: 

89 # Ensure that each datastore gets its own registry 

90 registryClass = type(self.registry) 

91 registry = registryClass() 

92 else: 

93 registry = self.registry 

94 return self.datastoreType(config=config, bridgeManager=registry.getDatastoreBridgeManager()) 

95 

96 

97class BadWriteFormatter(YamlFormatter): 

98 """A formatter that never works but does leave a file behind.""" 

99 

100 def _readFile(self, path, pytype=None): 

101 raise NotImplementedError("This formatter can not read anything") 

102 

103 def _writeFile(self, inMemoryDataset): 

104 """Write an empty file and then raise an exception.""" 

105 with open(self.fileDescriptor.location.path, "wb"): 

106 pass 

107 raise RuntimeError("Did not succeed in writing file") 

108 

109 

110class BadNoWriteFormatter(BadWriteFormatter): 

111 """A formatter that always fails without writing anything.""" 

112 

113 def _writeFile(self, inMemoryDataset): 

114 raise RuntimeError("Did not writing anything at all") 

115 

116 

117class MultiDetectorFormatter(YamlFormatter): 

118 

119 def _writeFile(self, inMemoryDataset): 

120 raise NotImplementedError("Can not write") 

121 

122 def _fromBytes(self, serializedDataset, pytype=None): 

123 data = super()._fromBytes(serializedDataset) 

124 if self.dataId is None: 

125 raise RuntimeError("This formatter requires a dataId") 

126 if "detector" not in self.dataId: 

127 raise RuntimeError("This formatter requires detector to be present in dataId") 

128 key = f"detector{self.dataId['detector']}" 

129 if key in data: 

130 return pytype(data[key]) 

131 raise RuntimeError(f"Could not find '{key}' in data file")