add seek() to CompressingFileReader

Change-Id: I33d9119684497b53101db3bc380953e86bdf25f0
This commit is contained in:
Greg Lange
2013年09月17日 19:24:24 +00:00
parent 8e48dd6b7f
commit 285a3a88a1

View File

@@ -56,14 +56,23 @@ class CompressingFileReader(object):
def __init__(self, file_obj, compresslevel=9, chunk_size=4096):
self._f = file_obj
self.compresslevel = compresslevel
self.chunk_size = chunk_size
self.set_initial_state()
def set_initial_state(self):
"""
Sets the object to the state needed for the first read.
"""
self._f.seek(0)
self._compressor = compressobj(
compresslevel, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL,
0)
self.compresslevel, zlib.DEFLATED, -zlib.MAX_WBITS,
zlib.DEF_MEM_LEVEL, 0)
self.done = False
self.first = True
self.crc32 = 0
self.total_size = 0
self.chunk_size = chunk_size
def read(self, *a, **kw):
"""
@@ -105,6 +114,11 @@ class CompressingFileReader(object):
return chunk
raise StopIteration
def seek(self, offset, whence=0):
if not (offset == 0 and whence == 0):
raise NotImplementedError('Seek implemented on offset 0 only')
self.set_initial_state()
class InternalClient(object):
"""

View File

@@ -141,7 +141,8 @@ class TestCompressingfileReader(unittest.TestCase):
def test_read(self):
exp_data = 'abcdefghijklmnopqrstuvwxyz'
fobj = internal_client.CompressingFileReader(StringIO(exp_data))
fobj = internal_client.CompressingFileReader(
StringIO(exp_data), chunk_size=5)
data = ''
d = zlib.decompressobj(16 + zlib.MAX_WBITS)
@@ -150,6 +151,29 @@ class TestCompressingfileReader(unittest.TestCase):
self.assertEquals(exp_data, data)
def test_seek(self):
exp_data = 'abcdefghijklmnopqrstuvwxyz'
fobj = internal_client.CompressingFileReader(
StringIO(exp_data), chunk_size=5)
# read a couple of chunks only
for _ in range(2):
fobj.read()
# read whole thing after seek and check data
fobj.seek(0)
data = ''
d = zlib.decompressobj(16 + zlib.MAX_WBITS)
for chunk in fobj.read():
data += d.decompress(chunk)
self.assertEquals(exp_data, data)
def test_seek_not_implemented_exception(self):
fobj = internal_client.CompressingFileReader(
StringIO(''), chunk_size=5)
self.assertRaises(NotImplementedError, fobj.seek, 10)
self.assertRaises(NotImplementedError, fobj.seek, 0, 10)
class TestInternalClient(unittest.TestCase):
def test_init(self):
Reference in New Issue
openstack/swift
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.

The note is not visible to the blocked user.