Coverage for python/lsst/daf/butler/formatters/yaml.py: 25%
47 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 10:07 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 10:07 +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/>.
28from __future__ import annotations
30__all__ = ("YamlFormatter",)
32import contextlib
33import dataclasses
34from typing import Any
36import yaml
38from .file import FileFormatter
41class YamlFormatter(FileFormatter):
42 """Formatter implementation for YAML files."""
44 extension = ".yaml"
46 unsupportedParameters = None
47 """This formatter does not support any parameters"""
49 supportedWriteParameters = frozenset({"unsafe_dump"})
50 """Allow the normal yaml.dump to be used to write the YAML. Use this
51 if you know that your class has registered representers."""
53 def _readFile(self, path: str, pytype: type[Any] | None = None) -> Any:
54 """Read a file from the path in YAML format.
56 Parameters
57 ----------
58 path : `str`
59 Path to use to open YAML format file.
60 pytype : `class`, optional
61 Not used by this implementation.
63 Returns
64 -------
65 data : `object`
66 Either data as Python object read from YAML file, or None
67 if the file could not be opened.
69 Notes
70 -----
71 The `~yaml.SafeLoader` is used when parsing the YAML file.
72 """
73 try:
74 with open(path, "rb") as fd:
75 data = self._fromBytes(fd.read(), pytype)
76 except FileNotFoundError:
77 data = None
79 return data
81 def _fromBytes(self, serializedDataset: bytes, pytype: type[Any] | None = None) -> Any:
82 """Read the bytes object as a python object.
84 Parameters
85 ----------
86 serializedDataset : `bytes`
87 Bytes object to unserialize.
88 pytype : `class`, optional
89 Not used by this implementation.
91 Returns
92 -------
93 inMemoryDataset : `object`
94 The requested data as an object, or None if the string could
95 not be read.
97 Notes
98 -----
99 The `~yaml.SafeLoader` is used when parsing the YAML.
100 """
101 data = yaml.safe_load(serializedDataset)
103 with contextlib.suppress(AttributeError):
104 data = data.exportAsDict()
106 return data
108 def _writeFile(self, inMemoryDataset: Any) -> None:
109 """Write the in memory dataset to file on disk.
111 Will look for `_asdict()` method to aid YAML serialization, following
112 the approach of the simplejson module. The `dict` will be passed
113 to the relevant constructor on read.
115 Parameters
116 ----------
117 inMemoryDataset : `object`
118 Object to serialize.
120 Raises
121 ------
122 Exception
123 The file could not be written.
125 Notes
126 -----
127 The `~yaml.SafeDumper` is used when generating the YAML serialization.
128 This will fail for data structures that have complex python classes
129 without a registered YAML representer.
130 """
131 self.fileDescriptor.location.uri.write(self._toBytes(inMemoryDataset))
133 def _toBytes(self, inMemoryDataset: Any) -> bytes:
134 """Write the in memory dataset to a bytestring.
136 Will look for `_asdict()` method to aid YAML serialization, following
137 the approach of the simplejson module. The `dict` will be passed
138 to the relevant constructor on read.
140 Parameters
141 ----------
142 inMemoryDataset : `object`
143 Object to serialize
145 Returns
146 -------
147 serializedDataset : `bytes`
148 YAML string encoded to bytes.
150 Raises
151 ------
152 Exception
153 The object could not be serialized.
155 Notes
156 -----
157 The `~yaml.SafeDumper` is used when generating the YAML serialization.
158 This will fail for data structures that have complex python classes
159 without a registered YAML representer.
160 """
161 converted = False
162 if hasattr(inMemoryDataset, "model_dump") and hasattr(inMemoryDataset, "model_dump_json"):
163 # Pydantic v2-like model if both model_dump() and model_json()
164 # exist.
165 with contextlib.suppress(Exception):
166 inMemoryDataset = inMemoryDataset.model_dump()
167 converted = True
169 if not converted and hasattr(inMemoryDataset, "dict") and hasattr(inMemoryDataset, "json"):
170 # Pydantic v1-like model if both dict() and json() exist.
171 with contextlib.suppress(Exception):
172 inMemoryDataset = inMemoryDataset.dict()
173 converted = True
175 if not converted:
176 if dataclasses.is_dataclass(inMemoryDataset):
177 inMemoryDataset = dataclasses.asdict(inMemoryDataset)
178 elif hasattr(inMemoryDataset, "_asdict"):
179 inMemoryDataset = inMemoryDataset._asdict()
181 unsafe_dump = self.writeParameters.get("unsafe_dump", False)
182 if unsafe_dump:
183 serialized = yaml.dump(inMemoryDataset)
184 else:
185 serialized = yaml.safe_dump(inMemoryDataset)
186 return serialized.encode()