Coverage for python/lsst/daf/butler/formatters/yaml.py: 31%
40 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-05 01:26 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-05 01:26 +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 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/>.
22from __future__ import annotations
24__all__ = ("YamlFormatter",)
26import contextlib
27import dataclasses
28from typing import Any
30import yaml
32from .file import FileFormatter
35class YamlFormatter(FileFormatter):
36 """Formatter implementation for YAML files."""
38 extension = ".yaml"
40 unsupportedParameters = None
41 """This formatter does not support any parameters"""
43 supportedWriteParameters = frozenset({"unsafe_dump"})
44 """Allow the normal yaml.dump to be used to write the YAML. Use this
45 if you know that your class has registered representers."""
47 def _readFile(self, path: str, pytype: type[Any] | None = None) -> Any:
48 """Read a file from the path in YAML format.
50 Parameters
51 ----------
52 path : `str`
53 Path to use to open YAML format file.
54 pytype : `class`, optional
55 Not used by this implementation.
57 Returns
58 -------
59 data : `object`
60 Either data as Python object read from YAML file, or None
61 if the file could not be opened.
63 Notes
64 -----
65 The `~yaml.SafeLoader` is used when parsing the YAML file.
66 """
67 try:
68 with open(path, "rb") as fd:
69 data = self._fromBytes(fd.read(), pytype)
70 except FileNotFoundError:
71 data = None
73 return data
75 def _fromBytes(self, serializedDataset: bytes, pytype: type[Any] | None = None) -> Any:
76 """Read the bytes object as a python object.
78 Parameters
79 ----------
80 serializedDataset : `bytes`
81 Bytes object to unserialize.
82 pytype : `class`, optional
83 Not used by this implementation.
85 Returns
86 -------
87 inMemoryDataset : `object`
88 The requested data as an object, or None if the string could
89 not be read.
91 Notes
92 -----
93 The `~yaml.SafeLoader` is used when parsing the YAML.
94 """
95 data = yaml.safe_load(serializedDataset)
97 with contextlib.suppress(AttributeError):
98 data = data.exportAsDict()
100 return data
102 def _writeFile(self, inMemoryDataset: Any) -> None:
103 """Write the in memory dataset to file on disk.
105 Will look for `_asdict()` method to aid YAML serialization, following
106 the approach of the simplejson module. The `dict` will be passed
107 to the relevant constructor on read.
109 Parameters
110 ----------
111 inMemoryDataset : `object`
112 Object to serialize.
114 Raises
115 ------
116 Exception
117 The file could not be written.
119 Notes
120 -----
121 The `~yaml.SafeDumper` is used when generating the YAML serialization.
122 This will fail for data structures that have complex python classes
123 without a registered YAML representer.
124 """
125 self.fileDescriptor.location.uri.write(self._toBytes(inMemoryDataset))
127 def _toBytes(self, inMemoryDataset: Any) -> bytes:
128 """Write the in memory dataset to a bytestring.
130 Will look for `_asdict()` method to aid YAML serialization, following
131 the approach of the simplejson module. The `dict` will be passed
132 to the relevant constructor on read.
134 Parameters
135 ----------
136 inMemoryDataset : `object`
137 Object to serialize
139 Returns
140 -------
141 serializedDataset : `bytes`
142 YAML string encoded to bytes.
144 Raises
145 ------
146 Exception
147 The object could not be serialized.
149 Notes
150 -----
151 The `~yaml.SafeDumper` is used when generating the YAML serialization.
152 This will fail for data structures that have complex python classes
153 without a registered YAML representer.
154 """
155 if hasattr(inMemoryDataset, "dict") and hasattr(inMemoryDataset, "json"):
156 # Pydantic-like model if both dict() and json() exist.
157 with contextlib.suppress(Exception):
158 inMemoryDataset = inMemoryDataset.dict()
160 if dataclasses.is_dataclass(inMemoryDataset):
161 inMemoryDataset = dataclasses.asdict(inMemoryDataset)
162 elif hasattr(inMemoryDataset, "_asdict"):
163 inMemoryDataset = inMemoryDataset._asdict()
164 unsafe_dump = self.writeParameters.get("unsafe_dump", False)
165 if unsafe_dump:
166 serialized = yaml.dump(inMemoryDataset)
167 else:
168 serialized = yaml.safe_dump(inMemoryDataset)
169 return serialized.encode()