"""Compressor is an interface to implement extensions to the compressionmodule. Extensions will typically subclass the Compressor ABC andprovide that subclass as a setuptools entry point.Note that this interface has similar patterns to Converter. Thisinterface is designed for compression and decompression of ASDFbinary array blocks, while Converter is designed for serializationof custom Python types into the YAML tree."""importabc
[docs]classCompressor(abc.ABC):""" Abstract base class for plugins that compress binary data. Implementing classes must provide the ``labels`` property, and at least one of the `compress()` and `decompress()` methods. May also provide a constructor. """@classmethoddef__subclasshook__(cls,class_):ifclsisCompressor:returnhasattr(class_,"label")and(hasattr(class_,"compress")orhasattr(class_,"decompress"))returnNotImplemented# pragma: no cover@property@abc.abstractmethoddeflabel(self):""" Get the 4-byte label identifying this compression Returns ------- label : bytes The compression label """
[docs]defcompress(self,data,**kwargs):""" Compress ``data``, yielding the results. The yield may be block-by-block, or all at once. Parameters ---------- data : memoryview The data to compress. Must be contiguous and 1D, with the underlying ``itemsize`` preserved. **kwargs Keyword arguments to be passed to the underlying compression function Yields ------ compressed : bytes-like A block of compressed data """raiseNotImplementedError
[docs]defdecompress(self,data,out,**kwargs):""" Decompress ``data``, writing the result into ``out``. Parameters ---------- data : Iterable of bytes-like An Iterable of bytes-like objects containing chunks of compressed data. out : read-write bytes-like A contiguous, 1D output array, of equal or greater length than the decompressed data. **kwargs Keyword arguments to be passed to the underlying decompression function Returns ------- nbytes : int The number of bytes written to ``out`` """raiseNotImplementedError