From 48985cc57dc1e17e5c1568a9d711cea5f05a6587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Wed, 13 Mar 2019 14:01:02 +0100 Subject: [PATCH] fixed bunker.files.bunkeredfile.BunkeredFile.empty --- bunker/files/bunkeredfile.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/bunker/files/bunkeredfile.py b/bunker/files/bunkeredfile.py index dfe3217..d57ee36 100644 --- a/bunker/files/bunkeredfile.py +++ b/bunker/files/bunkeredfile.py @@ -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)