UTF-8? ✓
import bz2
import snappyclass UnknownCompressor(Exception):
passCompressor classes adapt compression APIs from python libraries to what the CAS class cas.py needs.
Compression / Decompression wrapper CAS <-> python
class Compressor(object):Each class has an identifier attribute by which CAS selects the compressor
identifier = "no" meta = {
"compression": "no",
"orig_size": len(data)
}
return meta, dataDecompress data
@staticmethod
def decompress(data): return dataSelect compressor class by identifier. Return class object
@staticmethod
def select(identifier): for cls in Compressor.__subclasses__():
if cls.identifier == identifier:
return cls
if Compressor.identifier == identifier:
return Compressor
raise UnknownCompressor("Compressor unknown: " + identifier)Return list of supported compressors
@staticmethod
def supported(): return [cls.identifier for cls in Compressor.__subclasses__()] + [Compressor.identifier]class SnappyCompressor(Compressor):
identifier = "snappy" @staticmethod
def compress(data):
meta = {
"compression": "snappy",
"orig_size": len(data)
}
compressed_data = snappy.compress(data)
return meta, compressed_data
@staticmethod
def decompress(compressed_data):
data = snappy.uncompress(compressed_data)
return dataclass BZ2Compressor(Compressor):
identifier = "bz2" @staticmethod
def compress(data):
meta = {
"compression": "bz2",
"orig_size": len(data),
"compresslevel": 9
}
compressed_data = bz2.compress(data, 9)
return meta, compressed_data
@staticmethod
def decompress(compressed_data):
data = bz2.decompress(compressed_data)
return data