give better exceptions when the file type isnt a GZIP or a Blend

This commit is contained in:
2014-11-19 16:31:26 +01:00
parent 49e3c1e181
commit a5646eeeb9

View File

@@ -42,8 +42,9 @@ def open_blend(filename, access="rb"):
Known issue: does not support packaged blend files Known issue: does not support packaged blend files
""" """
handle = open(filename, access) handle = open(filename, access)
magic = handle.read(7) magic_test = b"BLENDER"
if magic == b"BLENDER": magic = handle.read(len(magic_test))
if magic == magic_test:
log.debug("normal blendfile detected") log.debug("normal blendfile detected")
handle.seek(0, os.SEEK_SET) handle.seek(0, os.SEEK_SET)
bfile = BlendFile(handle) bfile = BlendFile(handle)
@@ -55,19 +56,23 @@ def open_blend(filename, access="rb"):
handle.close() handle.close()
log.debug("decompressing started") log.debug("decompressing started")
fs = gzip.open(filename, "rb") fs = gzip.open(filename, "rb")
handle = tempfile.TemporaryFile()
data = fs.read(FILE_BUFFER_SIZE) data = fs.read(FILE_BUFFER_SIZE)
while data: magic = data[:len(magic_test)]
handle.write(data) if magic == magic_test:
data = fs.read(FILE_BUFFER_SIZE) handle = tempfile.TemporaryFile()
log.debug("decompressing finished") while data:
fs.close() handle.write(data)
log.debug("resetting decompressed file") data = fs.read(FILE_BUFFER_SIZE)
handle.seek(os.SEEK_SET, 0) log.debug("decompressing finished")
bfile = BlendFile(handle) fs.close()
bfile.is_compressed = True log.debug("resetting decompressed file")
bfile.filepath_orig = filename handle.seek(os.SEEK_SET, 0)
return bfile bfile = BlendFile(handle)
bfile.is_compressed = True
bfile.filepath_orig = filename
return bfile
else:
raise Exception("filetype inside gzip not a blend")
else: else:
raise Exception("filetype not a blend or a gzip blend") raise Exception("filetype not a blend or a gzip blend")