22 from __future__
import annotations
24 __all__ = (
"FilterFormatter",)
27 from lsst.afw.image
import Filter
35 from lsst.daf.butler.formatters.file
import FileFormatter
39 """Read and write `~lsst.afw.image.Filter` filter information."""
43 unsupportedParameters =
None
44 """This formatter does not support any parameters."""
46 def _readFile(self, path: str, pytype: Type[Any] =
None) -> Any:
47 """Read a file from the path in YAML format.
52 Path to use to open the file.
53 pytype : `class`, optional
54 The type expected to be returned.
59 Either data as Python object read from YAML file, or None
60 if the file could not be opened.
63 with open(path,
"rb")
as fd:
65 except FileNotFoundError:
70 def _fromBytes(self, serializedDataset: bytes, pytype: Optional[Type[Any]] =
None) -> Any:
71 """Read the bytes object as a python object.
75 serializedDataset : `bytes`
76 Bytes object to unserialize.
77 pytype : `type`, optional
78 Expected python type to be returned.
82 inMemoryDataset : `lsst.afw.image.Filter`
83 The requested data as an object.
85 data = yaml.load(serializedDataset, Loader=yaml.SafeLoader)
92 filter = pytype(data[
"canonicalName"], force=
True)
96 def _writeFile(self, inMemoryDataset: Any) ->
None:
97 """Write the in memory dataset to file on disk.
101 inMemoryDataset : `lsst.afw.image.Filter`
107 Raised if the file could not be written or the dataset could not be
110 with open(self.fileDescriptor.location.path,
"wb")
as fd:
111 fd.write(self.
_toBytes(inMemoryDataset))
113 def _toBytes(self, inMemoryDataset: Any) -> bytes:
114 """Write the in memory dataset to a bytestring.
118 inMemoryDataset : `lsst.afw.image.Filter`
123 serializedDataset : `bytes`
124 YAML string encoded to bytes.
129 Raised if the object could not be serialized.
137 filter[
"canonicalName"] = inMemoryDataset.getCanonicalName()
138 filter[
"name"] = inMemoryDataset.getName()
139 filter[
"aliases"] = inMemoryDataset.getAliases()
141 return yaml.dump(filter).encode()