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

25 

26import builtins 

27import yaml 

28 

29from typing import ( 

30 TYPE_CHECKING, 

31 Any, 

32 Optional, 

33 Type, 

34) 

35 

36from .file import FileFormatter 

37 

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

39 from lsst.daf.butler import StorageClass 

40 

41 

42class YamlFormatter(FileFormatter): 

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

44 """ 

45 extension = ".yaml" 

46 

47 unsupportedParameters = None 

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

49 

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

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

52 

53 Parameters 

54 ---------- 

55 path : `str` 

56 Path to use to open YAML format file. 

57 pytype : `class`, optional 

58 Not used by this implementation. 

59 

60 Returns 

61 ------- 

62 data : `object` 

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

64 if the file could not be opened. 

65 

66 Notes 

67 ----- 

68 The `~yaml.UnsafeLoader` is used when parsing the YAML file. 

69 """ 

70 try: 

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

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

73 except FileNotFoundError: 

74 data = None 

75 

76 return data 

77 

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

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

80 

81 Parameters 

82 ---------- 

83 serializedDataset : `bytes` 

84 Bytes object to unserialize. 

85 pytype : `class`, optional 

86 Not used by this implementation. 

87 

88 Returns 

89 ------- 

90 inMemoryDataset : `object` 

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

92 not be read. 

93 """ 

94 try: 

95 data = yaml.load(serializedDataset, Loader=yaml.FullLoader) 

96 except yaml.YAMLError: 

97 data = None 

98 try: 

99 data = data.exportAsDict() 

100 except AttributeError: 

101 pass 

102 return data 

103 

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

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

106 

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

108 the approach of the simplejson module. 

109 

110 Parameters 

111 ---------- 

112 inMemoryDataset : `object` 

113 Object to serialize. 

114 

115 Raises 

116 ------ 

117 Exception 

118 The file could not be written. 

119 """ 

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

121 if hasattr(inMemoryDataset, "_asdict"): 

122 inMemoryDataset = inMemoryDataset._asdict() 

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

124 

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

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

127 

128 Parameters 

129 ---------- 

130 inMemoryDataset : `object` 

131 Object to serialize 

132 

133 Returns 

134 ------- 

135 serializedDataset : `bytes` 

136 YAML string encoded to bytes. 

137 

138 Raises 

139 ------ 

140 Exception 

141 The object could not be serialized. 

142 """ 

143 return yaml.dump(inMemoryDataset).encode() 

144 

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

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

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

148 

149 Parameters 

150 ---------- 

151 inMemoryDataset : `object` 

152 Object to coerce to expected type. 

153 storageClass : `StorageClass` 

154 StorageClass associated with `inMemoryDataset`. 

155 pytype : `type`, optional 

156 Override type to use for conversion. 

157 

158 Returns 

159 ------- 

160 inMemoryDataset : `object` 

161 Object of expected type `pytype`. 

162 """ 

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

164 if storageClass.isComposite(): 

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

166 elif not isinstance(inMemoryDataset, pytype): 

167 # Hope that we can pass the arguments in directly 

168 inMemoryDataset = pytype(inMemoryDataset) 

169 return inMemoryDataset