mirror of
https://github.com/derrod/legendary.git
synced 2025-01-03 04:45:28 +00:00
[models] Support file objects in manifest/chunk serialization
This commit is contained in:
parent
60845fce48
commit
d94d07a26b
|
@ -111,8 +111,11 @@ class Chunk:
|
|||
|
||||
return _chunk
|
||||
|
||||
def write(self, compress=True):
|
||||
def write(self, fp=None, compress=True):
|
||||
if not fp:
|
||||
bio = BytesIO()
|
||||
else:
|
||||
bio = fp
|
||||
|
||||
self.uncompressed_size = self.compressed_size = len(self.data)
|
||||
if compress or self.compressed:
|
||||
|
@ -139,4 +142,5 @@ class Chunk:
|
|||
# finally, add the data
|
||||
bio.write(self._data)
|
||||
|
||||
if not fp:
|
||||
return bio.getvalue()
|
||||
|
|
|
@ -135,8 +135,9 @@ class Manifest:
|
|||
|
||||
return _manifest
|
||||
|
||||
def write(self, compress=True):
|
||||
def write(self, fp=None, compress=True):
|
||||
body_bio = BytesIO()
|
||||
|
||||
self.meta.write(body_bio)
|
||||
self.chunk_data_list.write(body_bio)
|
||||
self.file_manifest_list.write(body_bio)
|
||||
|
@ -151,7 +152,11 @@ class Manifest:
|
|||
self.data = zlib.compress(self.data)
|
||||
self.size_compressed = len(self.data)
|
||||
|
||||
if not fp:
|
||||
bio = BytesIO()
|
||||
else:
|
||||
bio = fp
|
||||
|
||||
bio.write(struct.pack('<I', self.header_magic))
|
||||
bio.write(struct.pack('<I', self.header_size))
|
||||
bio.write(struct.pack('<I', self.size_uncompressed))
|
||||
|
@ -161,6 +166,7 @@ class Manifest:
|
|||
bio.write(struct.pack('<I', self.version))
|
||||
bio.write(self.data)
|
||||
|
||||
if not fp:
|
||||
return bio.getvalue()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue