Coverage for python/lsst/daf/butler/formatters/yaml.py: 28%

Shortcuts 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

48 statements  

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

25 

26import builtins 

27import yaml 

28import dataclasses 

29 

30from typing import ( 

31 TYPE_CHECKING, 

32 Any, 

33 Optional, 

34 Type, 

35) 

36 

37from .file import FileFormatter 

38 

39if TYPE_CHECKING: 39 ↛ 40line 39 didn't jump to line 40, because the condition on line 39 was never true

40 from lsst.daf.butler import StorageClass 

41 

42 

43class YamlFormatter(FileFormatter): 

44 """Interface for reading and writing Python objects to and from YAML files. 

45 """ 

46 extension = ".yaml" 

47 

48 unsupportedParameters = None 

49 """This formatter does not support any parameters""" 

50 

51 supportedWriteParameters = frozenset({"unsafe_dump"}) 

52 """Allow the normal yaml.dump to be used to write the YAML. Use this 

53 if you know that your class has registered representers.""" 

54 

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

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

57 

58 Parameters 

59 ---------- 

60 path : `str` 

61 Path to use to open YAML format file. 

62 pytype : `class`, optional 

63 Not used by this implementation. 

64 

65 Returns 

66 ------- 

67 data : `object` 

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

69 if the file could not be opened. 

70 

71 Notes 

72 ----- 

73 The `~yaml.SafeLoader` is used when parsing the YAML file. 

74 """ 

75 try: 

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

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

78 except FileNotFoundError: 

79 data = None 

80 

81 return data 

82 

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

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

85 

86 Parameters 

87 ---------- 

88 serializedDataset : `bytes` 

89 Bytes object to unserialize. 

90 pytype : `class`, optional 

91 Not used by this implementation. 

92 

93 Returns 

94 ------- 

95 inMemoryDataset : `object` 

96 The requested data as an object, or None if the string could 

97 not be read. 

98 

99 Notes 

100 ----- 

101 The `~yaml.SafeLoader` is used when parsing the YAML. 

102 """ 

103 data = yaml.safe_load(serializedDataset) 

104 

105 try: 

106 data = data.exportAsDict() 

107 except AttributeError: 

108 pass 

109 return data 

110 

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

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

113 

114 Will look for `_asdict()` method to aid YAML serialization, following 

115 the approach of the simplejson module. The `dict` will be passed 

116 to the relevant constructor on read. 

117 

118 Parameters 

119 ---------- 

120 inMemoryDataset : `object` 

121 Object to serialize. 

122 

123 Raises 

124 ------ 

125 Exception 

126 The file could not be written. 

127 

128 Notes 

129 ----- 

130 The `~yaml.SafeDumper` is used when generating the YAML serialization. 

131 This will fail for data structures that have complex python classes 

132 without a registered YAML representer. 

133 """ 

134 self.fileDescriptor.location.uri.write(self._toBytes(inMemoryDataset)) 

135 

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

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

138 

139 Will look for `_asdict()` method to aid YAML serialization, following 

140 the approach of the simplejson module. The `dict` will be passed 

141 to the relevant constructor on read. 

142 

143 Parameters 

144 ---------- 

145 inMemoryDataset : `object` 

146 Object to serialize 

147 

148 Returns 

149 ------- 

150 serializedDataset : `bytes` 

151 YAML string encoded to bytes. 

152 

153 Raises 

154 ------ 

155 Exception 

156 The object could not be serialized. 

157 

158 Notes 

159 ----- 

160 The `~yaml.SafeDumper` is used when generating the YAML serialization. 

161 This will fail for data structures that have complex python classes 

162 without a registered YAML representer. 

163 """ 

164 if dataclasses.is_dataclass(inMemoryDataset): 

165 inMemoryDataset = dataclasses.asdict(inMemoryDataset) 

166 elif hasattr(inMemoryDataset, "_asdict"): 

167 inMemoryDataset = inMemoryDataset._asdict() 

168 unsafe_dump = self.writeParameters.get("unsafe_dump", False) 

169 if unsafe_dump: 

170 serialized = yaml.dump(inMemoryDataset) 

171 else: 

172 serialized = yaml.safe_dump(inMemoryDataset) 

173 return serialized.encode() 

174 

175 def _coerceType(self, inMemoryDataset: Any, storageClass: StorageClass, 

176 pytype: Optional[Type[Any]] = None) -> Any: 

177 """Coerce the supplied inMemoryDataset to type `pytype`. 

178 

179 Parameters 

180 ---------- 

181 inMemoryDataset : `object` 

182 Object to coerce to expected type. 

183 storageClass : `StorageClass` 

184 StorageClass associated with `inMemoryDataset`. 

185 pytype : `type`, optional 

186 Override type to use for conversion. 

187 

188 Returns 

189 ------- 

190 inMemoryDataset : `object` 

191 Object of expected type `pytype`. 

192 """ 

193 if inMemoryDataset is not None and pytype is not None and not hasattr(builtins, pytype.__name__): 

194 if storageClass.isComposite(): 

195 inMemoryDataset = storageClass.delegate().assemble(inMemoryDataset, pytype=pytype) 

196 elif not isinstance(inMemoryDataset, pytype): 

197 if dataclasses.is_dataclass(pytype): 

198 # dataclasses accept key/value parameters 

199 inMemoryDataset = pytype(**inMemoryDataset) 

200 else: 

201 # Hope that we can pass the arguments in directly 

202 inMemoryDataset = pytype(inMemoryDataset) 

203 return inMemoryDataset