Coverage for python/lsst/daf/butler/formatters/packages.py: 60%

35 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-12-15 02:03 -0800

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__ = ("PackagesFormatter",) 

23 

24import os.path 

25from typing import Any, Optional, Type 

26 

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

28from lsst.utils.packages import Packages 

29 

30 

31class PackagesFormatter(FileFormatter): 

32 """Interface for reading and writing `~lsst.utils.packages.Packages`. 

33 

34 This formatter supports write parameters: 

35 

36 * ``format``: The file format to use to write the package data. Allowed 

37 options are ``yaml``, ``json``, and ``pickle``. 

38 """ 

39 

40 supportedWriteParameters = frozenset({"format"}) 

41 supportedExtensions = frozenset({".yaml", ".pickle", ".pkl", ".json"}) 

42 

43 # MyPy does't like the fact that the base declares this an instance 

44 # attribute while this derived class uses a property. 

45 @property 

46 def extension(self) -> str: # type: ignore 

47 # Default to YAML but allow configuration via write parameter 

48 format = self.writeParameters.get("format", "yaml") 

49 ext = "." + format 

50 if ext not in self.supportedExtensions: 50 ↛ 51line 50 didn't jump to line 51, because the condition on line 50 was never true

51 raise RuntimeError(f"Requested file format '{format}' is not supported for Packages") 

52 return ext 

53 

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

55 """Read a file from the path. 

56 

57 Parameters 

58 ---------- 

59 path : `str` 

60 Path to use to open the file. 

61 pytype : `type` 

62 Class to use to read the serialized file. 

63 

64 Returns 

65 ------- 

66 data : `object` 

67 Instance of class ``pytype`` read from serialized file. None 

68 if the file could not be opened. 

69 """ 

70 if not os.path.exists(path): 

71 return None 

72 

73 assert pytype is not None 

74 assert issubclass(pytype, Packages) 

75 return pytype.read(path) 

76 

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

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

79 

80 Parameters 

81 ---------- 

82 serializedDataset : `bytes` 

83 Bytes object to unserialize. 

84 pytype : `type` 

85 The Python type to be instantiated. Required. 

86 

87 Returns 

88 ------- 

89 inMemoryDataset : `object` 

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

91 not be read. 

92 """ 

93 # The format can not come from the formatter configuration 

94 # because the current configuration has no connection to how 

95 # the data were stored. 

96 if serializedDataset.startswith(b"!<lsst."): 96 ↛ 98line 96 didn't jump to line 98, because the condition on line 96 was never false

97 format = "yaml" 

98 elif serializedDataset.startswith(b'{"'): 

99 format = "json" 

100 else: 

101 format = "pickle" 

102 

103 assert pytype is not None 

104 assert issubclass(pytype, Packages) 

105 return pytype.fromBytes(serializedDataset, format) 

106 

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

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

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 inMemoryDataset.write(self.fileDescriptor.location.path) 

121 

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

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

124 

125 Parameters 

126 ---------- 

127 inMemoryDataset : `object` 

128 Object to serialize 

129 

130 Returns 

131 ------- 

132 serializedDataset : `bytes` 

133 YAML string encoded to bytes. 

134 

135 Raises 

136 ------ 

137 Exception 

138 The object could not be serialized. 

139 """ 

140 format = self.extension.lstrip(".") 

141 return inMemoryDataset.toBytes(format)