Some simplifications

This commit is contained in:
Sybren A. Stüvel 2018-02-22 17:44:38 +01:00
parent 968068d21d
commit 480b82337e

View File

@ -138,17 +138,17 @@ class BlendFile:
def __exit__(self, exctype, excvalue, traceback): def __exit__(self, exctype, excvalue, traceback):
self.close() self.close()
def find_blocks_from_code(self, code): def find_blocks_from_code(self, code: bytes) -> typing.List['BlendFileBlock']:
assert (type(code) == bytes) assert isinstance(code, bytes)
if code not in self.code_index:
return []
return self.code_index[code] return self.code_index[code]
def find_block_from_offset(self, offset): def find_block_from_address(self, address: int) -> typing.Optional['BlendFileBlock']:
# same as looking looping over all blocks, """Return the block at that address, or None if not found.
# then checking ``block.addr_old == offset``
assert (type(offset) is int) :param address: the BlendFileBlock.addr_old value
return self.block_from_addr.get(offset) """
assert type(address) is int
return self.block_from_addr.get(address)
def close(self): def close(self):
"""Close the blend file. """Close the blend file.
@ -160,11 +160,12 @@ class BlendFile:
def ensure_subtype_smaller(self, sdna_index_curr, sdna_index_next): def ensure_subtype_smaller(self, sdna_index_curr, sdna_index_next):
# never refine to a smaller type # never refine to a smaller type
if (self.structs[sdna_index_curr].size > curr_struct = self.structs[sdna_index_curr]
self.structs[sdna_index_next].size): next_struct = self.structs[sdna_index_next]
raise RuntimeError("cant refine to smaller type (%s -> %s)" % if curr_struct.size > next_struct.size:
(self.structs[sdna_index_curr].dna_type_id.decode('ascii'), raise RuntimeError("Can't refine to smaller type (%s -> %s)" %
self.structs[sdna_index_next].dna_type_id.decode('ascii'))) (curr_struct.dna_type_id.decode('utf-8'),
next_struct.dna_type_id.decode('utf-8')))
def decode_structs(self, block: 'BlendFileBlock'): def decode_structs(self, block: 'BlendFileBlock'):
""" """
@ -366,18 +367,25 @@ class BlendFileBlock:
ofs += (self.size // self.count) * base_index ofs += (self.size // self.count) * base_index
self.bfile.fileobj.seek(ofs, os.SEEK_SET) self.bfile.fileobj.seek(ofs, os.SEEK_SET)
if sdna_index_refine is None: dna_struct = self._get_struct(sdna_index_refine)
sdna_index_refine = self.sdna_index
else:
self.bfile.ensure_subtype_smaller(self.sdna_index, sdna_index_refine)
dna_struct = self.bfile.structs[sdna_index_refine]
return dna_struct.field_get( return dna_struct.field_get(
self.bfile.header, self.bfile.fileobj, path, self.bfile.header, self.bfile.fileobj, path,
default=default, default=default,
null_terminated=null_terminated, as_str=as_str, null_terminated=null_terminated, as_str=as_str,
) )
def _get_struct(self, sdna_index_refine) -> dna.Struct:
"""Gets the (possibly refined) struct for this block."""
if sdna_index_refine is None:
index = self.sdna_index
else:
self.bfile.ensure_subtype_smaller(self.sdna_index, sdna_index_refine)
index = sdna_index_refine
dna_struct = self.bfile.structs[index]
return dna_struct
def get_recursive_iter(self, def get_recursive_iter(self,
path: dna.FieldPath, path: dna.FieldPath,
path_root: dna.FieldPath = b'', path_root: dna.FieldPath = b'',
@ -439,12 +447,7 @@ class BlendFileBlock:
sdna_index_refine=None, sdna_index_refine=None,
): ):
if sdna_index_refine is None: dna_struct = self._get_struct(sdna_index_refine)
sdna_index_refine = self.sdna_index
else:
self.bfile.ensure_subtype_smaller(self.sdna_index, sdna_index_refine)
dna_struct = self.bfile.structs[sdna_index_refine]
self.bfile.handle.seek(self.file_offset, os.SEEK_SET) self.bfile.handle.seek(self.file_offset, os.SEEK_SET)
self.bfile.is_modified = True self.bfile.is_modified = True
return dna_struct.field_set( return dna_struct.field_set(