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) return cls(file_, name, size=size, isvirtual=False, ismem=False)
@classmethod @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. Construct a new BunkeredFile that uses BytesIO in the background.
This is used either when loading data from a remote ressource or for This is used either when loading data from a remote ressource or for
databases. databases.
""" """
file_ = io.BytesIO() if(length_hint is not None
return cls(file_, name, size=None, isvirtual=True, ismem=True) 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 @classmethod
def from_BytesIO(cls, bytes_, name): def from_BytesIO(cls, bytes_, name):
@ -130,7 +139,7 @@ class BunkeredFile(io.RawIOBase):
ismem = True ismem = True
with tarfile.extractfile(tarinfo) as fin: with tarfile.extractfile(tarinfo) as fin:
print(file_.write(fin.read())) file_.write(fin.read())
file_.seek(0, 0) file_.seek(0, 0)
return cls(file_, tarinfo.name, size=tarinfo.size, ismem=ismem, isvirtual=True, bunker=rewriteable_tar_file) return cls(file_, tarinfo.name, size=tarinfo.size, ismem=ismem, isvirtual=True, bunker=rewriteable_tar_file)