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
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
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/>.
22__all__ = ("PackagesFormatter",)
24import os.path
26from lsst.daf.butler.formatters.file import FileFormatter
29class PackagesFormatter(FileFormatter):
30 """Interface for reading and writing objects that support the standard
31 afw I/O readFits/writeFits methods.
32 """
34 supportedWriteParameters = frozenset({"format"})
35 supportedExtensions = frozenset({".yaml", ".pickle", ".pkl"})
37 @property
38 def extension(self) -> str:
39 # Default to YAML but allow configuration via write parameter
40 format = self.writeParameters.get("format", "yaml")
41 if format == "yaml": 41 ↛ 43line 41 didn't jump to line 43, because the condition on line 41 was never false
42 return ".yaml"
43 elif format == "pickle":
44 return ".pickle"
45 raise RuntimeError(f"Requested file format '{format}' is not supported for Packages")
47 def _readFile(self, path, pytype):
48 """Read a file from the path in FITS format.
50 Parameters
51 ----------
52 path : `str`
53 Path to use to open the file.
54 pytype : `type`
55 Class to use to read the serialized file.
57 Returns
58 -------
59 data : `object`
60 Instance of class ``pytype`` read from serialized file. None
61 if the file could not be opened.
62 """
63 if not os.path.exists(path):
64 return None
66 return pytype.read(path)
68 def _fromBytes(self, serializedDataset, pytype=None):
69 """Read the bytes object as a python object.
71 Parameters
72 ----------
73 serializedDataset : `bytes`
74 Bytes object to unserialize.
75 pytype : `type`
76 The Python type to be instantiated. Required.
78 Returns
79 -------
80 inMemoryDataset : `object`
81 The requested data as an object, or None if the string could
82 not be read.
83 """
84 # The format can not come from the formatter configuration
85 # because the current configuration has no connection to how
86 # the data were stored.
87 format = "yaml" if serializedDataset.startswith(b"!<lsst.base.Packages>") else "pickle"
88 return pytype.fromBytes(serializedDataset, format)
90 def _writeFile(self, inMemoryDataset):
91 """Write the in memory dataset to file on disk.
93 Parameters
94 ----------
95 inMemoryDataset : `object`
96 Object to serialize.
98 Raises
99 ------
100 Exception
101 The file could not be written.
102 """
103 inMemoryDataset.write(self.fileDescriptor.location.path)
105 def _toBytes(self, inMemoryDataset):
106 """Write the in memory dataset to a bytestring.
108 Parameters
109 ----------
110 inMemoryDataset : `object`
111 Object to serialize
113 Returns
114 -------
115 serializedDataset : `bytes`
116 YAML string encoded to bytes.
118 Raises
119 ------
120 Exception
121 The object could not be serialized.
122 """
123 format = "yaml" if self.extension == ".yaml" else "pickle"
124 return inMemoryDataset.toBytes(format)