BadWriteFormatter

class lsst.daf.butler.tests.BadWriteFormatter(file_descriptor: FileDescriptor, *, ref: DatasetRef, write_parameters: Mapping[str, Any] | None = None, write_recipes: Mapping[str, Any] | None = None, **kwargs: Any)

Bases: YamlFormatter

A formatter that never works but does leave a file behind.

Attributes Summary

can_read_from_local_file

Declare whether read_from_file is available to this formatter.

can_read_from_stream

Declare whether read_from_stream is available to this formatter.

can_read_from_uri

Declare whether read_from_uri is available to this formatter.

Methods Summary

read_from_uri(uri[, component, expected_size])

Read a dataset from a URI that can be local or remote.

write_direct(in_memory_dataset, uri[, ...])

Serialize and write directly to final location.

Attributes Documentation

can_read_from_local_file: ClassVar[bool] = False

Declare whether read_from_file is available to this formatter.

can_read_from_stream: ClassVar[bool] = False

Declare whether read_from_stream is available to this formatter.

can_read_from_uri: ClassVar[bool] = False

Declare whether read_from_uri is available to this formatter.

Methods Documentation

read_from_uri(uri: ResourcePath, component: str | None = None, expected_size: int = -1) Any

Read a dataset from a URI that can be local or remote.

Parameters:
urilsst.resources.ResourcePath

URI to use to read the dataset. This URI can be local or remote and can refer to the actual resource or to a locally cached file.

componentstr or None, optional

The component to be read from the dataset.

expected_sizeint, optional

If known, the expected size of the resource to read. This can be -1 indicates the file size is not known.

Returns:
in_memory_datasetobject or NotImplemented

The Python object read from the resource or NotImplemented.

Raises:
FormatterNotImplementedError

Raised if there is no support for direct reads from a, possibly, remote URI.

Notes

This method is only called if the class property can_read_from_uri is set to True.

It is possible that a cached local file will be given to this method even if it was originally a remote URI. This can happen if the original write resulted in the file being added to the local cache.

If the full file is being read this file will not be added to the local cache. Consider returning NotImplemented in this situation, for example if there are no parameters or component specified, and allowing the system to fall back to calling read_from_local_file (which will populate the cache if configured to do so).

write_direct(in_memory_dataset: Any, uri: ResourcePath, cache_manager: AbstractDatastoreCacheManager | None = None) bool

Serialize and write directly to final location.

Parameters:
in_memory_datasetobject

The Dataset to serialize.

urilsst.resources.ResourcePath

URI to use when writing the serialized dataset.

cache_managerAbstractDatastoreCacheManager

A cache manager to use to allow a formatter to cache the written file.

Returns:
writtenbool

Flag to indicate whether the direct write did happen.

Raises:
Exception

Raised if there was a failure from serializing to bytes that was not FormatterNotImplementedError.

Notes

This method will call to_bytes to serialize the in-memory dataset and then will call the write method directly.

If the dataset should be cached or is local the file will not be written and the method will return False. This is because local URIs should be written to a temporary file name and then renamed to allow atomic writes. That path is handled by write_locally_then_move through write_local_file) and is preferred over this method being subclassed and the atomic write re-implemented.