Coverage for python/lsst/obs/base/formatters/packages.py: 62%

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

29 statements  

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 

22__all__ = ("PackagesFormatter",) 

23 

24import os.path 

25 

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

27 

28 

29class PackagesFormatter(FileFormatter): 

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

31 

32 This formatter supports write parameters: 

33 

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

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

36 """ 

37 

38 supportedWriteParameters = frozenset({"format"}) 

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

40 

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

42 # attribute while this derived class uses a property. 

43 @property 

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

45 # Default to YAML but allow configuration via write parameter 

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

47 ext = "." + format 

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

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

50 return ext 

51 

52 def _readFile(self, path, pytype): 

53 """Read a file from the path in FITS format. 

54 

55 Parameters 

56 ---------- 

57 path : `str` 

58 Path to use to open the file. 

59 pytype : `type` 

60 Class to use to read the serialized file. 

61 

62 Returns 

63 ------- 

64 data : `object` 

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

66 if the file could not be opened. 

67 """ 

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

69 return None 

70 

71 return pytype.read(path) 

72 

73 def _fromBytes(self, serializedDataset, pytype=None): 

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

75 

76 Parameters 

77 ---------- 

78 serializedDataset : `bytes` 

79 Bytes object to unserialize. 

80 pytype : `type` 

81 The Python type to be instantiated. Required. 

82 

83 Returns 

84 ------- 

85 inMemoryDataset : `object` 

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

87 not be read. 

88 """ 

89 # The format can not come from the formatter configuration 

90 # because the current configuration has no connection to how 

91 # the data were stored. 

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

93 format = "yaml" 

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

95 format = "json" 

96 else: 

97 format = "pickle" 

98 return pytype.fromBytes(serializedDataset, format) 

99 

100 def _writeFile(self, inMemoryDataset): 

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

102 

103 Parameters 

104 ---------- 

105 inMemoryDataset : `object` 

106 Object to serialize. 

107 

108 Raises 

109 ------ 

110 Exception 

111 The file could not be written. 

112 """ 

113 inMemoryDataset.write(self.fileDescriptor.location.path) 

114 

115 def _toBytes(self, inMemoryDataset): 

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

117 

118 Parameters 

119 ---------- 

120 inMemoryDataset : `object` 

121 Object to serialize 

122 

123 Returns 

124 ------- 

125 serializedDataset : `bytes` 

126 YAML string encoded to bytes. 

127 

128 Raises 

129 ------ 

130 Exception 

131 The object could not be serialized. 

132 """ 

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

134 return inMemoryDataset.toBytes(format)