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
|
return _chunk
|
||||||
|
|
||||||
def write(self, compress=True):
|
def write(self, fp=None, compress=True):
|
||||||
bio = BytesIO()
|
if not fp:
|
||||||
|
bio = BytesIO()
|
||||||
|
else:
|
||||||
|
bio = fp
|
||||||
|
|
||||||
self.uncompressed_size = self.compressed_size = len(self.data)
|
self.uncompressed_size = self.compressed_size = len(self.data)
|
||||||
if compress or self.compressed:
|
if compress or self.compressed:
|
||||||
|
@ -139,4 +142,5 @@ class Chunk:
|
||||||
# finally, add the data
|
# finally, add the data
|
||||||
bio.write(self._data)
|
bio.write(self._data)
|
||||||
|
|
||||||
return bio.getvalue()
|
if not fp:
|
||||||
|
return bio.getvalue()
|
||||||
|
|
|
@ -135,8 +135,9 @@ class Manifest:
|
||||||
|
|
||||||
return _manifest
|
return _manifest
|
||||||
|
|
||||||
def write(self, compress=True):
|
def write(self, fp=None, compress=True):
|
||||||
body_bio = BytesIO()
|
body_bio = BytesIO()
|
||||||
|
|
||||||
self.meta.write(body_bio)
|
self.meta.write(body_bio)
|
||||||
self.chunk_data_list.write(body_bio)
|
self.chunk_data_list.write(body_bio)
|
||||||
self.file_manifest_list.write(body_bio)
|
self.file_manifest_list.write(body_bio)
|
||||||
|
@ -151,7 +152,11 @@ class Manifest:
|
||||||
self.data = zlib.compress(self.data)
|
self.data = zlib.compress(self.data)
|
||||||
self.size_compressed = len(self.data)
|
self.size_compressed = len(self.data)
|
||||||
|
|
||||||
bio = BytesIO()
|
if not fp:
|
||||||
|
bio = BytesIO()
|
||||||
|
else:
|
||||||
|
bio = fp
|
||||||
|
|
||||||
bio.write(struct.pack('<I', self.header_magic))
|
bio.write(struct.pack('<I', self.header_magic))
|
||||||
bio.write(struct.pack('<I', self.header_size))
|
bio.write(struct.pack('<I', self.header_size))
|
||||||
bio.write(struct.pack('<I', self.size_uncompressed))
|
bio.write(struct.pack('<I', self.size_uncompressed))
|
||||||
|
@ -161,7 +166,8 @@ class Manifest:
|
||||||
bio.write(struct.pack('<I', self.version))
|
bio.write(struct.pack('<I', self.version))
|
||||||
bio.write(self.data)
|
bio.write(self.data)
|
||||||
|
|
||||||
return bio.getvalue()
|
if not fp:
|
||||||
|
return bio.getvalue()
|
||||||
|
|
||||||
|
|
||||||
class ManifestMeta:
|
class ManifestMeta:
|
||||||
|
|
Loading…
Reference in a new issue