Coverage for python/lsst/daf/butler/formatters/packages.py: 62%
35 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:15 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:15 -0700
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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28__all__ = ("PackagesFormatter",)
30import os.path
31from typing import Any
33from lsst.daf.butler.formatters.file import FileFormatter
34from lsst.utils.packages import Packages
37class PackagesFormatter(FileFormatter):
38 """Interface for reading and writing `~lsst.utils.packages.Packages`.
40 This formatter supports write parameters:
42 * ``format``: The file format to use to write the package data. Allowed
43 options are ``yaml``, ``json``, and ``pickle``.
44 """
46 supportedWriteParameters = frozenset({"format"})
47 supportedExtensions = frozenset({".yaml", ".pickle", ".pkl", ".json"})
49 # MyPy does't like the fact that the base declares this an instance
50 # attribute while this derived class uses a property.
51 @property
52 def extension(self) -> str: # type: ignore
53 # Default to YAML but allow configuration via write parameter
54 format = self.writeParameters.get("format", "yaml")
55 ext = "." + format
56 if ext not in self.supportedExtensions: 56 ↛ 57line 56 didn't jump to line 57, because the condition on line 56 was never true
57 raise RuntimeError(f"Requested file format '{format}' is not supported for Packages")
58 return ext
60 def _readFile(self, path: str, pytype: type | None = None) -> Any:
61 """Read a file from the path.
63 Parameters
64 ----------
65 path : `str`
66 Path to use to open the file.
67 pytype : `type`
68 Class to use to read the serialized file.
70 Returns
71 -------
72 data : `object`
73 Instance of class ``pytype`` read from serialized file. None
74 if the file could not be opened.
75 """
76 if not os.path.exists(path):
77 return None
79 assert pytype is not None
80 assert issubclass(pytype, Packages)
81 return pytype.read(path)
83 def _fromBytes(self, serializedDataset: Any, pytype: type | None = None) -> Any:
84 """Read the bytes object as a python object.
86 Parameters
87 ----------
88 serializedDataset : `bytes`
89 Bytes object to unserialize.
90 pytype : `type`
91 The Python type to be instantiated. Required.
93 Returns
94 -------
95 inMemoryDataset : `object`
96 The requested data as an object, or None if the string could
97 not be read.
98 """
99 # The format can not come from the formatter configuration
100 # because the current configuration has no connection to how
101 # the data were stored.
102 if serializedDataset.startswith(b"!<lsst."): 102 ↛ 104line 102 didn't jump to line 104, because the condition on line 102 was never false
103 format = "yaml"
104 elif serializedDataset.startswith(b'{"'):
105 format = "json"
106 else:
107 format = "pickle"
109 assert pytype is not None
110 assert issubclass(pytype, Packages)
111 return pytype.fromBytes(serializedDataset, format)
113 def _writeFile(self, inMemoryDataset: Any) -> None:
114 """Write the in memory dataset to file on disk.
116 Parameters
117 ----------
118 inMemoryDataset : `object`
119 Object to serialize.
121 Raises
122 ------
123 Exception
124 The file could not be written.
125 """
126 inMemoryDataset.write(self.fileDescriptor.location.path)
128 def _toBytes(self, inMemoryDataset: Any) -> bytes:
129 """Write the in memory dataset to a bytestring.
131 Parameters
132 ----------
133 inMemoryDataset : `object`
134 Object to serialize
136 Returns
137 -------
138 serializedDataset : `bytes`
139 YAML string encoded to bytes.
141 Raises
142 ------
143 Exception
144 The object could not be serialized.
145 """
146 format = self.extension.lstrip(".")
147 return inMemoryDataset.toBytes(format)