Python snappy.decompress() Examples

The following are 14 code examples of snappy.decompress(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module snappy , or try the search function .
Example #1
Source File: aff4_image.py    From pyaff4 with Apache License 2.0 5 votes vote down vote up
def doDecompress(self, cbuffer, chunk_id):

        if self.compression == lexicon.AFF4_IMAGE_COMPRESSION_ZLIB :
            if len(cbuffer) == self.chunk_size:
                return cbuffer
            return zlib.decompress(cbuffer)

        elif self.compression == lexicon.AFF4_IMAGE_COMPRESSION_SNAPPY_SCUDETTE:
            # Backwards compatibility with Scudette's AFF4 implementation.
            # Chunks are always compressed.
            return snappy.decompress(cbuffer)

        elif self.compression == lexicon.AFF4_IMAGE_COMPRESSION_SNAPPY:

            if len(cbuffer) == self.chunk_size:
                # Buffer is not compressed.
                return cbuffer
            try:
                return snappy.decompress(cbuffer)
            except Exception as e:
                raise e

        elif self.compression == lexicon.AFF4_IMAGE_COMPRESSION_STORED:
            return cbuffer

        else:
            raise RuntimeError(
                "Unable to process compression %s" % self.compression)


# This class implements Evimetry's AFF4 pre standardisation effort 
Example #2
Source File: buffers.py    From purerpc with Apache License 2.0 5 votes vote down vote up
def decompress(self, data):
        if self._message_encoding == "gzip" or self._message_encoding == "deflate":
            import zlib
            return zlib.decompress(data)
        elif self._message_encoding == "snappy":
            import snappy
            return snappy.decompress(data)
        else:
            raise UnsupportedMessageEncodingError(
                "Unsupported compression: {}".format(self._message_encoding)) 
Example #3
Source File: buffers.py    From purerpc with Apache License 2.0 5 votes vote down vote up
def _parse_one_message(self):
        # either compressed_flag = message_length = flow_controlled_length = None and
        # compressed_flag, message_length are the next elements in self._buffer, or they are all
        # not None, and the next element in self._buffer is data
        if self._message_length is None:
            if len(self._buffer) < 5:
                return None, 0
            message_header, self._flow_controlled_length = self._buffer.popleft_flowcontrol(5)
            self._compressed_flag, self._message_length = struct.unpack('>?I', message_header)
            if self._message_length > self._max_message_length:
                # Even after the error is raised, the state is not corrupted, and parsing
                # can be safely resumed
                raise MessageTooLargeError(
                    "Received message larger than max: {message_length} > {max_message_length}".format(
                        message_length=self._message_length,
                        max_message_length=self._max_message_length,
                    )
                )
        if len(self._buffer) < self._message_length:
            return None, 0
        data, flow_controlled_length = self._buffer.popleft_flowcontrol(self._message_length)
        flow_controlled_length += self._flow_controlled_length
        if self._compressed_flag:
            data = self.decompress(data)
        self._compressed_flag = self._message_length = self._flow_controlled_length = None
        return data, flow_controlled_length 
Example #4
Source File: codec.py    From afkak with Apache License 2.0 5 votes vote down vote up
def snappy_decode(payload):
    if not has_snappy():
        raise NotImplementedError("Snappy codec is not available")

    if payload.startswith(_XERIAL_HEADER):
        # TODO ? Should become a fileobj ?
        view = memoryview(payload)
        out = []
        length = len(payload)

        cursor = 16
        while cursor < length:
            block_size = struct.unpack_from('!i', view, cursor)[0]
            # Skip the block size
            cursor += 4
            end = cursor + block_size
            # XXX snappy requires a bytes-like object but doesn't accept
            # a memoryview, so we must copy.
            out.append(snappy.decompress(view[cursor:end].tobytes()))
            cursor = end

        # See https://atleastfornow.net/blog/not-all-bytes/
        return b''.join(out)
    else:
        return snappy.decompress(payload) 
Example #5
Source File: serializer.py    From intake with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def decompress(self, data):
        return data 
Example #6
Source File: serializer.py    From intake with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def decompress(self, data):
        with gzip.GzipFile(fileobj=io.BytesIO(data)) as f:
            return f.read() 
Example #7
Source File: serializer.py    From intake with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def decode(self, bytestr, container):
        return self._format_encoder.decode(
            self._compressor.decompress(bytestr), container) 
Example #8
Source File: serializer.py    From intake with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def decompress(self, data):
            return snappy.decompress(data) 
Example #9
Source File: datafile.py    From spavro with Apache License 2.0 5 votes vote down vote up
def _read_block_header(self):
        self.block_count = self.raw_decoder.read_long()
        if self.codec == "null":
            # Skip a long; we don't need to use the length.
            self.raw_decoder.skip_long()
            self._datum_decoder = self._raw_decoder
        elif self.codec == 'deflate':
            # Compressed data is stored as (length, data), which
            # corresponds to how the "bytes" type is encoded.
            data = self.raw_decoder.read_bytes()
            # -15 is the log of the window size; negative indicates
            # "raw" (no zlib headers) decompression.    See zlib.h.
            uncompressed = zlib.decompress(data, -15)
            self._datum_decoder = io.BinaryDecoder(StringIO(uncompressed))
        elif self.codec == 'snappy':
            # Compressed data includes a 4-byte CRC32 checksum
            length = self.raw_decoder.read_long()
            data = self.raw_decoder.read(length - 4)
            uncompressed = snappy.decompress(data)
            self._datum_decoder = io.BinaryDecoder(StringIO(uncompressed))
            self.raw_decoder.check_crc32(uncompressed);
        elif self.codec == 'xz':
            # Compressed data is stored as (length, data), which
            # corresponds to how the "bytes" type is encoded.
            data = self.raw_decoder.read_bytes()
            uncompressed = lzma.decompress(data)
            self._datum_decoder = io.BinaryDecoder(StringIO(uncompressed))
        else:
            raise DataFileException("Unknown codec: %r" % self.codec) 
Example #10
Source File: base.py    From lahja with MIT License 5 votes vote down vote up
def _decompress_event(self, data: Union[BaseEvent, bytes]) -> BaseEvent:
        if isinstance(data, BaseEvent):
            return data
        else:
            import snappy

            return cast(BaseEvent, pickle.loads(snappy.decompress(data)))

    #
    # Remote Endpoint Management
    # 
Example #11
Source File: gossiper.py    From trinity with MIT License 5 votes vote down vote up
def _deserialize_gossip(
    compressed_data: bytes, sedes: ssz.BaseSedes
) -> ssz.Serializable:
    data = snappy.decompress(compressed_data)
    return ssz.decode(data, sedes) 
Example #12
Source File: commands.py    From trinity with MIT License 5 votes vote down vote up
def decompress(self, data: bytes) -> bytes:
        return snappy.decompress(data) 
Example #13
Source File: commands.py    From trinity with MIT License 5 votes vote down vote up
def decompress(self, data: bytes) -> bytes:
        return data 
Example #14
Source File: commands.py    From trinity with MIT License 5 votes vote down vote up
def decode(cls: Type[TCommand], message: MessageAPI, snappy_support: bool) -> TCommand:
        if snappy_support:
            payload_data = cls.compression_codec.decompress(message.encoded_payload)
        else:
            payload_data = message.encoded_payload

        try:
            payload = cls.serialization_codec.decode(payload_data)
        except rlp.exceptions.DeserializationError as err:
            raise rlp.exceptions.DeserializationError(
                f"DeserializationError for {cls}",
                err.serial,
            ) from err
        return cls(payload)