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

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__all__ = ("FilterFormatter",) 

25 

26import yaml 

27from lsst.afw.image import Filter 

28 

29from typing import ( 

30 Any, 

31 Optional, 

32 Type, 

33) 

34 

35from lsst.daf.butler.formatters.file import FileFormatter 

36 

37 

38class FilterFormatter(FileFormatter): 

39 """Read and write `~lsst.afw.image.Filter` filter information.""" 

40 

41 extension = ".yaml" 

42 

43 unsupportedParameters = None 

44 """This formatter does not support any parameters.""" 

45 

46 def _readFile(self, path: str, pytype: Type[Any] = None) -> Any: 

47 """Read a file from the path in YAML format. 

48 

49 Parameters 

50 ---------- 

51 path : `str` 

52 Path to use to open the file. 

53 pytype : `class`, optional 

54 The type expected to be returned. 

55 

56 Returns 

57 ------- 

58 data : `object` 

59 Either data as Python object read from YAML file, or None 

60 if the file could not be opened. 

61 """ 

62 try: 

63 with open(path, "rb") as fd: 

64 data = self._fromBytes(fd.read(), pytype) 

65 except FileNotFoundError: 

66 data = None 

67 

68 return data 

69 

70 def _fromBytes(self, serializedDataset: bytes, pytype: Optional[Type[Any]] = None) -> Any: 

71 """Read the bytes object as a python object. 

72 

73 Parameters 

74 ---------- 

75 serializedDataset : `bytes` 

76 Bytes object to unserialize. 

77 pytype : `type`, optional 

78 Expected python type to be returned. 

79 

80 Returns 

81 ------- 

82 inMemoryDataset : `lsst.afw.image.Filter` 

83 The requested data as an object. 

84 """ 

85 data = yaml.load(serializedDataset, Loader=yaml.SafeLoader) 

86 

87 if pytype is None: 87 ↛ 88line 87 didn't jump to line 88, because the condition on line 87 was never true

88 pytype = Filter 

89 

90 # This will be a simple dict so we need to convert it to 

91 # the Filter type -- just needs the name 

92 filter = pytype(data["canonicalName"], force=True) 

93 

94 return filter 

95 

96 def _writeFile(self, inMemoryDataset: Any) -> None: 

97 """Write the in memory dataset to file on disk. 

98 

99 Parameters 

100 ---------- 

101 inMemoryDataset : `lsst.afw.image.Filter` 

102 Filter to serialize. 

103 

104 Raises 

105 ------ 

106 Exception 

107 Raised if the file could not be written or the dataset could not be 

108 serialized. 

109 """ 

110 with open(self.fileDescriptor.location.path, "wb") as fd: 

111 fd.write(self._toBytes(inMemoryDataset)) 

112 

113 def _toBytes(self, inMemoryDataset: Any) -> bytes: 

114 """Write the in memory dataset to a bytestring. 

115 

116 Parameters 

117 ---------- 

118 inMemoryDataset : `lsst.afw.image.Filter` 

119 Object to serialize. 

120 

121 Returns 

122 ------- 

123 serializedDataset : `bytes` 

124 YAML string encoded to bytes. 

125 

126 Raises 

127 ------ 

128 Exception 

129 Raised if the object could not be serialized. 

130 """ 

131 

132 # Convert the Filter to a dict for dumping 

133 # Given the singleton situation, only the name is really 

134 # needed but it does not hurt to put some detail in the file 

135 # to aid debugging. 

136 filter = {} 

137 filter["canonicalName"] = inMemoryDataset.getCanonicalName() 

138 filter["name"] = inMemoryDataset.getName() 

139 filter["aliases"] = inMemoryDataset.getAliases() 

140 

141 return yaml.dump(filter).encode()