Coverage for python / lsst / daf / butler / formatters / packages.py: 0%
23 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-30 08:41 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-30 08:41 +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 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",)
30from typing import Any
32from lsst.daf.butler import FormatterV2
33from lsst.resources import ResourcePath
34from lsst.utils.packages import Packages
37class PackagesFormatter(FormatterV2):
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 supported_write_parameters = frozenset({"format"})
47 supported_extensions = frozenset({".yaml", ".pickle", ".pkl", ".json"})
48 can_read_from_uri = True
50 def get_write_extension(self) -> str:
51 # Default to YAML but allow configuration via write parameter
52 format = self.write_parameters.get("format", "yaml")
53 ext = "." + format
54 if ext not in self.supported_extensions:
55 raise RuntimeError(f"Requested file format '{format}' is not supported for Packages")
56 return ext
58 def read_from_uri(self, uri: ResourcePath, component: str | None = None, expected_size: int = -1) -> Any:
59 # Read the full file using the class associated with the
60 # storage class it was originally written with.
61 # Read the bytes directly from resource. These are not going to be
62 # large.
63 pytype = self.file_descriptor.storageClass.pytype
64 assert issubclass(pytype, Packages) # for mypy
65 format = uri.getExtension().lstrip(".") # .yaml -> yaml
66 return pytype.fromBytes(uri.read(), format)
68 def to_bytes(self, in_memory_dataset: Any) -> bytes:
69 """Write the in memory dataset to a bytestring.
71 Parameters
72 ----------
73 in_memory_dataset : `object`
74 Object to serialize.
76 Returns
77 -------
78 serializedDataset : `bytes`
79 YAML string encoded to bytes.
81 Raises
82 ------
83 Exception
84 The object could not be serialized.
85 """
86 format = self.get_write_extension().lstrip(".")
87 return in_memory_dataset.toBytes(format)