util: put compression code next to each other

ctxmanager was injecting itself between the compression and
decompression code. Let's restore some order.
This commit is contained in:
Gregory Szorc 2016-10-15 17:24:01 -07:00
parent 2d4abff2d9
commit f301293b9d

View File

@ -2794,32 +2794,6 @@ def finddirs(path):
yield path[:pos] yield path[:pos]
pos = path.rfind('/', 0, pos) pos = path.rfind('/', 0, pos)
# compression utility
class nocompress(object):
def compress(self, x):
return x
def flush(self):
return ""
compressors = {
None: nocompress,
# lambda to prevent early import
'BZ': lambda: bz2.BZ2Compressor(),
'GZ': lambda: zlib.compressobj(),
}
# also support the old form by courtesies
compressors['UN'] = compressors[None]
def _makedecompressor(decompcls):
def generator(f):
d = decompcls()
for chunk in filechunkiter(f):
yield d.decompress(chunk)
def func(fh):
return chunkbuffer(generator(fh))
return func
class ctxmanager(object): class ctxmanager(object):
'''A context manager for use in 'with' blocks to allow multiple '''A context manager for use in 'with' blocks to allow multiple
contexts to be entered at once. This is both safer and more contexts to be entered at once. This is both safer and more
@ -2880,6 +2854,32 @@ class ctxmanager(object):
raise exc_val raise exc_val
return received and suppressed return received and suppressed
# compression utility
class nocompress(object):
def compress(self, x):
return x
def flush(self):
return ""
compressors = {
None: nocompress,
# lambda to prevent early import
'BZ': lambda: bz2.BZ2Compressor(),
'GZ': lambda: zlib.compressobj(),
}
# also support the old form by courtesies
compressors['UN'] = compressors[None]
def _makedecompressor(decompcls):
def generator(f):
d = decompcls()
for chunk in filechunkiter(f):
yield d.decompress(chunk)
def func(fh):
return chunkbuffer(generator(fh))
return func
def _bz2(): def _bz2():
d = bz2.BZ2Decompressor() d = bz2.BZ2Decompressor()
# Bzip2 stream start with BZ, but we stripped it. # Bzip2 stream start with BZ, but we stripped it.