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
Declare whether
read_from_file
is available to this formatter.Declare whether
read_from_stream
is available to this formatter.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:
- uri
lsst.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.
- component
str
orNone
, optional The component to be read from the dataset.
- expected_size
int
, optional If known, the expected size of the resource to read. This can be
-1
indicates the file size is not known.
- uri
- Returns:
- in_memory_dataset
object
orNotImplemented
The Python object read from the resource or
NotImplemented
.
- in_memory_dataset
- 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 toTrue
.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 callingread_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_dataset
object
The Dataset to serialize.
- uri
lsst.resources.ResourcePath
URI to use when writing the serialized dataset.
- cache_manager
AbstractDatastoreCacheManager
A cache manager to use to allow a formatter to cache the written file.
- in_memory_dataset
- Returns:
- written
bool
Flag to indicate whether the direct write did happen.
- written
- 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 thewrite
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 bywrite_locally_then_move
throughwrite_local_file
) and is preferred over this method being subclassed and the atomic write re-implemented.