/usr/lib/python3/dist-packages/asdf/compression.py is in python3-asdf 1.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | # Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals, print_function
import numpy as np
import six
def validate(compression):
"""
Validate the compression string.
Parameters
----------
compression : str or None
Returns
-------
compression : str or None
In canonical form.
Raises
------
ValueError
"""
if not compression:
return None
if compression == b'\0\0\0\0':
return None
if isinstance(compression, bytes):
compression = compression.decode('ascii')
if compression not in ('zlib', 'bzp2'):
raise ValueError(
"Supported compression types are: 'zlib' and 'bzp2'")
return compression
def _get_decoder(compression):
if compression == 'zlib':
try:
import zlib
except ImportError:
raise ImportError(
"Your Python does not have the zlib library, "
"therefore the compressed block in this ASDF file "
"can not be decompressed.")
return zlib.decompressobj()
elif compression == 'bzp2':
try:
import bz2
except ImportError:
raise ImportError(
"Your Python does not have the bz2 library, "
"therefore the compressed block in this ASDF file "
"can not be decompressed.")
return bz2.BZ2Decompressor()
else:
raise ValueError(
"Unknown compression type: '{0}'".format(compression))
def _get_encoder(compression):
if compression == 'zlib':
try:
import zlib
except ImportError:
raise ImportError(
"Your Python does not have the zlib library, "
"therefore the compressed block in this ASDF file "
"can not be decompressed.")
return zlib.compressobj()
elif compression == 'bzp2':
try:
import bz2
except ImportError:
raise ImportError(
"Your Python does not have the bz2 library, "
"therefore the compressed block in this ASDF file "
"can not be decompressed.")
return bz2.BZ2Compressor()
else:
raise ValueError(
"Unknown compression type: '{0}'".format(compression))
def to_compression_header(compression):
"""
Converts a compression string to the four byte field in a block
header.
"""
if not compression:
return b''
if isinstance(compression, six.text_type):
return compression.encode('ascii')
return compression
def decompress(fd, used_size, data_size, compression):
"""
Decompress binary data in a file
Parameters
----------
fd : generic_io.GenericIO object
The file to read the compressed data from.
used_size : int
The size of the compressed data
data_size : int
The size of the uncompressed data
compression : str
The compression type used.
Returns
-------
array : numpy.array
A flat uint8 containing the decompressed data.
"""
buffer = np.empty((data_size,), np.uint8)
compression = validate(compression)
decoder = _get_decoder(compression)
i = 0
for block in fd.read_blocks(used_size):
decoded = decoder.decompress(block)
if i + len(decoded) > data_size:
raise ValueError("Decompressed data too long")
buffer.data[i:i+len(decoded)] = decoded
i += len(decoded)
if hasattr(decoder, 'flush'):
decoded = decoder.flush()
if i + len(decoded) > data_size:
raise ValueError("Decompressed data too long")
elif i + len(decoded) < data_size:
raise ValueError("Decompressed data too short")
buffer[i:i+len(decoded)] = decoded
return buffer
def compress(fd, data, compression, block_size=1 << 16):
"""
Compress array data and write to a file.
Parameters
----------
fd : generic_io.GenericIO object
The file to write to.
data : buffer
The buffer of uncompressed data
compression : str
The type of compression to use.
block_size : int, optional
The size of blocks (in raw data) to process at a time.
"""
compression = validate(compression)
encoder = _get_encoder(compression)
for i in range(0, len(data), block_size):
fd.write(encoder.compress(data[i:i+block_size]))
fd.write(encoder.flush())
def get_compressed_size(data, compression, block_size=1 << 16):
"""
Returns the number of bytes required when the given data is
compressed.
Parameters
----------
data : buffer
compression : str
The type of compression to use.
Returns
-------
bytes : int
"""
compression = validate(compression)
encoder = _get_encoder(compression)
l = 0
for i in range(0, len(data), block_size):
l += len(encoder.compress(data[i:i+block_size]))
l += len(encoder.flush())
return l
|