22 __all__ = (
"PackagesFormatter", )
26 from lsst.daf.butler.formatters.file
import FileFormatter
30 """Interface for reading and writing objects that support the standard
31 afw I/O readFits/writeFits methods.
33 supportedWriteParameters = frozenset({
"format"})
34 supportedExtensions = frozenset({
".yaml",
".pickle",
".pkl"})
39 format = self.writeParameters.get(
"format",
"yaml")
42 elif format ==
"pickle":
44 raise RuntimeError(f
"Requested file format '{format}' is not supported for Packages")
46 def _readFile(self, path, pytype):
47 """Read a file from the path in FITS format.
52 Path to use to open the file.
54 Class to use to read the serialized file.
59 Instance of class ``pytype`` read from serialized file. None
60 if the file could not be opened.
62 if not os.path.exists(path):
65 return pytype.read(path)
67 def _fromBytes(self, serializedDataset, pytype=None):
68 """Read the bytes object as a python object.
72 serializedDataset : `bytes`
73 Bytes object to unserialize.
75 The Python type to be instantiated. Required.
79 inMemoryDataset : `object`
80 The requested data as an object, or None if the string could
86 format =
"yaml" if serializedDataset.startswith(b
"!<lsst.base.Packages>")
else "pickle"
87 return pytype.fromBytes(serializedDataset, format)
89 def _writeFile(self, inMemoryDataset):
90 """Write the in memory dataset to file on disk.
94 inMemoryDataset : `object`
100 The file could not be written.
102 inMemoryDataset.write(self.fileDescriptor.location.path)
104 def _toBytes(self, inMemoryDataset):
105 """Write the in memory dataset to a bytestring.
109 inMemoryDataset : `object`
114 serializedDataset : `bytes`
115 YAML string encoded to bytes.
120 The object could not be serialized.
123 return inMemoryDataset.toBytes(format)