Coverage for python/lsst/daf/butler/formatters/pickle.py: 66%

28 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-23 09:29 +0000

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"""Formatter associated with Python pickled objects.""" 

25 

26__all__ = ("PickleFormatter",) 

27 

28import pickle 

29from typing import Any 

30 

31from .file import FileFormatter 

32 

33 

34class PickleFormatter(FileFormatter): 

35 """Interface for reading and writing Python objects to and from pickle 

36 files. 

37 """ 

38 

39 extension = ".pickle" 

40 

41 unsupportedParameters = None 

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

43 

44 def _readFile(self, path: str, pytype: type[Any] | None = None) -> Any: 

45 """Read a file from the path in pickle format. 

46 

47 Parameters 

48 ---------- 

49 path : `str` 

50 Path to use to open the file. 

51 pytype : `class`, optional 

52 Not used by this implementation. 

53 

54 Returns 

55 ------- 

56 data : `object` 

57 Either data as Python object read from the pickle file, or None 

58 if the file could not be opened. 

59 """ 

60 try: 

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

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

63 except FileNotFoundError: 

64 data = None 

65 

66 return data 

67 

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

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

70 

71 Parameters 

72 ---------- 

73 inMemoryDataset : `object` 

74 Object to serialize. 

75 

76 Raises 

77 ------ 

78 Exception 

79 The file could not be written. 

80 """ 

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

82 pickle.dump(inMemoryDataset, fd, protocol=-1) 

83 

84 def _fromBytes(self, serializedDataset: bytes, pytype: type[Any] | None = None) -> Any: 

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

86 

87 Parameters 

88 ---------- 

89 serializedDataset : `bytes` 

90 Bytes object to unserialize. 

91 pytype : `class`, optional 

92 Not used by this implementation. 

93 

94 Returns 

95 ------- 

96 inMemoryDataset : `object` 

97 The requested data as a object, or None if the string could 

98 not be read. 

99 """ 

100 try: 

101 data = pickle.loads(serializedDataset) 

102 except pickle.PicklingError: 

103 data = None 

104 

105 return data 

106 

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

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

109 

110 Parameters 

111 ---------- 

112 inMemoryDataset : `object` 

113 Object to serialize 

114 

115 Returns 

116 ------- 

117 serializedDataset : `bytes` 

118 Bytes object representing the pickled object. 

119 

120 Raises 

121 ------ 

122 Exception 

123 The object could not be pickled. 

124 """ 

125 return pickle.dumps(inMemoryDataset, protocol=-1)