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 `~lsst.utils.packages.Packages`.
32 This formatter supports write parameters:
34 * ``format``: The file format to use to write the package data. Allowed
35 options are ``yaml``, ``json``, and ``pickle``.
36 """
38 supportedWriteParameters = frozenset({"format"})
39 supportedExtensions = frozenset({".yaml", ".pickle", ".pkl", ".json"})
41 @property
42 def extension(self) -> str:
43 # Default to YAML but allow configuration via write parameter
44 format = self.writeParameters.get("format", "yaml")
45 ext = "." + format
46 if ext not in self.supportedExtensions: 46 ↛ 47line 46 didn't jump to line 47, because the condition on line 46 was never true
47 raise RuntimeError(f"Requested file format '{format}' is not supported for Packages")
48 return ext
50 def _readFile(self, path, pytype):
51 """Read a file from the path in FITS format.
53 Parameters
54 ----------
55 path : `str`
56 Path to use to open the file.
57 pytype : `type`
58 Class to use to read the serialized file.
60 Returns
61 -------
62 data : `object`
63 Instance of class ``pytype`` read from serialized file. None
64 if the file could not be opened.
65 """
66 if not os.path.exists(path):
67 return None
69 return pytype.read(path)
71 def _fromBytes(self, serializedDataset, pytype=None):
72 """Read the bytes object as a python object.
74 Parameters
75 ----------
76 serializedDataset : `bytes`
77 Bytes object to unserialize.
78 pytype : `type`
79 The Python type to be instantiated. Required.
81 Returns
82 -------
83 inMemoryDataset : `object`
84 The requested data as an object, or None if the string could
85 not be read.
86 """
87 # The format can not come from the formatter configuration
88 # because the current configuration has no connection to how
89 # the data were stored.
90 if serializedDataset.startswith(b"!<lsst."): 90 ↛ 92line 90 didn't jump to line 92, because the condition on line 90 was never false
91 format = "yaml"
92 elif serializedDataset.startswith(b'{"'):
93 format = "json"
94 else:
95 format = "pickle"
96 return pytype.fromBytes(serializedDataset, format)
98 def _writeFile(self, inMemoryDataset):
99 """Write the in memory dataset to file on disk.
101 Parameters
102 ----------
103 inMemoryDataset : `object`
104 Object to serialize.
106 Raises
107 ------
108 Exception
109 The file could not be written.
110 """
111 inMemoryDataset.write(self.fileDescriptor.location.path)
113 def _toBytes(self, inMemoryDataset):
114 """Write the in memory dataset to a bytestring.
116 Parameters
117 ----------
118 inMemoryDataset : `object`
119 Object to serialize
121 Returns
122 -------
123 serializedDataset : `bytes`
124 YAML string encoded to bytes.
126 Raises
127 ------
128 Exception
129 The object could not be serialized.
130 """
131 format = self.extension.lstrip(".")
132 return inMemoryDataset.toBytes(format)