Support context manager for opening blend file

This commit is contained in:
2015-06-09 03:08:18 +10:00
parent 88eefcf6bc
commit 7d7fede5b2
3 changed files with 57 additions and 51 deletions

View File

@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys import sys
__version__ = "0.0.4.7" __version__ = "0.0.4.8"
def main(argv=sys.argv): def main(argv=sys.argv):
from .cli import main from .cli import main

View File

@@ -145,6 +145,12 @@ class BlendFile:
# cache (could lazy init, incase we never use?) # cache (could lazy init, incase we never use?)
self.block_from_offset = {block.addr_old: block for block in self.blocks if block.code != b'ENDB'} self.block_from_offset = {block.addr_old: block for block in self.blocks if block.code != b'ENDB'}
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
def find_blocks_from_code(self, code): def find_blocks_from_code(self, code):
assert(type(code) == bytes) assert(type(code) == bytes)
if code not in self.code_index: if code not in self.code_index:

View File

@@ -396,7 +396,7 @@ class FilePath:
extra_info = rootdir, os.path.basename(filepath) extra_info = rootdir, os.path.basename(filepath)
from bam.blend import blendfile from bam.blend import blendfile
blend = blendfile.open_blend(filepath_tmp, "rb" if readonly else "r+b") with blendfile.open_blend(filepath_tmp, "rb" if readonly else "r+b") as blend:
for code in blend.code_index.keys(): for code in blend.code_index.keys():
# handle library blocks as special case # handle library blocks as special case
@@ -450,8 +450,8 @@ class FilePath:
# do this after, incase we mangle names above # do this after, incase we mangle names above
for block in iter_blocks_idlib(): for block in iter_blocks_idlib():
yield from FilePath.from_block(block, basedir, extra_info, level) yield from FilePath.from_block(block, basedir, extra_info, level)
del blend
blend.close()
# ---------------- # ----------------
# Handle Recursive # Handle Recursive