fixed bunker.files.bunkeredfile.BunkeredFile.empty

This commit is contained in:
Daniel Knüttel 2019-03-13 14:01:02 +01:00
parent 9c98e20596
commit 48985cc57d

View File

@ -84,15 +84,24 @@ class BunkeredFile(io.RawIOBase):
return cls(file_, name, size=size, isvirtual=False, ismem=False)
@classmethod
def empty(cls, name):
def empty(cls, name
, length_hint=None
, max_in_memory_bytes=2**20
, mktempfile=tempfile.TemporaryFile):
"""
Construct a new BunkeredFile that uses BytesIO in the background.
This is used either when loading data from a remote ressource or for
databases.
"""
file_ = io.BytesIO()
return cls(file_, name, size=None, isvirtual=True, ismem=True)
if(length_hint is not None
and length_hint > max_in_memory_bytes):
file_ = mktempfile()
ismem = False
else:
file_ = io.BytesIO()
ismem = True
return cls(file_, name, size=None, isvirtual=True, ismem=ismem)
@classmethod
def from_BytesIO(cls, bytes_, name):
@ -130,7 +139,7 @@ class BunkeredFile(io.RawIOBase):
ismem = True
with tarfile.extractfile(tarinfo) as fin:
print(file_.write(fin.read()))
file_.write(fin.read())
file_.seek(0, 0)
return cls(file_, tarinfo.name, size=tarinfo.size, ismem=ismem, isvirtual=True, bunker=rewriteable_tar_file)