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

43 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-04-07 00:58 -0700

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 dataclasses 

27from typing import Any, Optional, Type 

28 

29import yaml 

30 

31from .file import FileFormatter 

32 

33 

34class YamlFormatter(FileFormatter): 

35 """Formatter implementation for YAML files.""" 

36 

37 extension = ".yaml" 

38 

39 unsupportedParameters = None 

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

41 

42 supportedWriteParameters = frozenset({"unsafe_dump"}) 

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

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

45 

46 def _readFile(self, path: str, pytype: Type[Any] | None = 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 YAML format file. 

53 pytype : `class`, optional 

54 Not used by this implementation. 

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 Notes 

63 ----- 

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

65 """ 

66 try: 

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

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

69 except FileNotFoundError: 

70 data = None 

71 

72 return data 

73 

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

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

76 

77 Parameters 

78 ---------- 

79 serializedDataset : `bytes` 

80 Bytes object to unserialize. 

81 pytype : `class`, optional 

82 Not used by this implementation. 

83 

84 Returns 

85 ------- 

86 inMemoryDataset : `object` 

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

88 not be read. 

89 

90 Notes 

91 ----- 

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

93 """ 

94 data = yaml.safe_load(serializedDataset) 

95 

96 try: 

97 data = data.exportAsDict() 

98 except AttributeError: 

99 pass 

100 return data 

101 

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

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

104 

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

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

107 to the relevant constructor on read. 

108 

109 Parameters 

110 ---------- 

111 inMemoryDataset : `object` 

112 Object to serialize. 

113 

114 Raises 

115 ------ 

116 Exception 

117 The file could not be written. 

118 

119 Notes 

120 ----- 

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

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

123 without a registered YAML representer. 

124 """ 

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

126 

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

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

129 

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

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

132 to the relevant constructor on read. 

133 

134 Parameters 

135 ---------- 

136 inMemoryDataset : `object` 

137 Object to serialize 

138 

139 Returns 

140 ------- 

141 serializedDataset : `bytes` 

142 YAML string encoded to bytes. 

143 

144 Raises 

145 ------ 

146 Exception 

147 The object could not be serialized. 

148 

149 Notes 

150 ----- 

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

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

153 without a registered YAML representer. 

154 """ 

155 if hasattr(inMemoryDataset, "dict") and hasattr(inMemoryDataset, "json"): 

156 # Pydantic-like model if both dict() and json() exist. 

157 try: 

158 inMemoryDataset = inMemoryDataset.dict() 

159 except Exception: 

160 pass 

161 

162 if dataclasses.is_dataclass(inMemoryDataset): 

163 inMemoryDataset = dataclasses.asdict(inMemoryDataset) 

164 elif hasattr(inMemoryDataset, "_asdict"): 

165 inMemoryDataset = inMemoryDataset._asdict() 

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

167 if unsafe_dump: 

168 serialized = yaml.dump(inMemoryDataset) 

169 else: 

170 serialized = yaml.safe_dump(inMemoryDataset) 

171 return serialized.encode()