diff --git a/examples/avb2aaf.py b/examples/avb2aaf.py index cfb2b79..a66a5ce 100644 --- a/examples/avb2aaf.py +++ b/examples/avb2aaf.py @@ -284,7 +284,7 @@ def check_source_clip(f, avb_source_clip): mob_id = MobID(bytes_le=avb_source_clip.mob_id.bytes_le) if zero_mob_id(mob_id): return 0 - avb_mob = avb_source_clip.root.content.mob_dict[avb_source_clip.mob_id] + avb_mob = avb_source_clip.root.content.find_by_mob_id(avb_source_clip.mob_id) if mob_id in f.content.mobs: mob = f.content.mobs[mob_id] else: @@ -797,7 +797,6 @@ def avb2aaf_main(path): with avb.open(path) as avb_file: with aaf2.open(path + ".aaf", 'w') as aaf_file: register_definitions(aaf_file) - avb_file.content.build_mob_dict() avb2aaf(aaf_file, avb_file) # with aaf2.open(path + ".aaf", 'r') as aaf_file: diff --git a/src/avb/bin.py b/src/avb/bin.py index f4876cb..5de8841 100644 --- a/src/avb/bin.py +++ b/src/avb/bin.py @@ -216,7 +216,7 @@ class Bin(core.AVBObject): AVBPropertyDef('attributes', 'BinAttr', 'reference'), AVBPropertyDef('was_iconic', 'WasIconic', 'bool', False), ] - __slots__ = ('mob_dict') + __slots__ = ('_mob_dict') def __init__(self): super(Bin, self).__init__(self) @@ -232,7 +232,7 @@ def __init__(self): self.sifted_settings.append(s) self.attributes = self.root.create.Attributes() - self.mob_dict ={} + self._mob_dict ={} def read(self, f): super(Bin, self).read(f) @@ -256,7 +256,7 @@ def read(self, f): object_count = ctx.read_u32(f) self.items = [] - self.mob_dict ={} + self._mob_dict ={} for i in range(object_count): bin_obj = BinItem.__new__(BinItem, root=self.root) @@ -369,16 +369,25 @@ def write(self, f): if self.class_id[:] == b'ABIN': ctx.write_u8(f, 0x03) - def build_mob_dict(self): - self.mob_dict = {} - for mob in self.mobs: - self.mob_dict[mob.mob_id] = mob + def find_by_mob_id(self, mob_id): + + # build mob_id dictionary + if not self._mob_dict: + for mob in self.mobs: + self._mob_dict[mob.mob_id] = mob + + return self._mob_dict.get(mob_id, None) def add_mob(self, mob): bin_item = self.root.create.BinItem() bin_item.mob = mob self.items.append(bin_item) + if self._mob_dict: + self._mob_dict[mob.mob_id] = mob + + return bin_item + @property def mobs(self): for item in self.items: diff --git a/src/avb/components.py b/src/avb/components.py index 7ede820..a393c1b 100644 --- a/src/avb/components.py +++ b/src/avb/components.py @@ -313,13 +313,10 @@ def write(self, f): @property def mob(self): - if not self.root.content.mob_dict: - self.root.content.build_mob_dict() - if hasattr(self, 'mob_id'): mob_id = self.mob_id if mob_id: - return self.root.content.mob_dict.get(self.mob_id, None) + return self.root.content.find_by_mob_id(self.mob_id) @property def track(self): diff --git a/src/avb/trackgroups.py b/src/avb/trackgroups.py index c951afb..8a8b8d9 100644 --- a/src/avb/trackgroups.py +++ b/src/avb/trackgroups.py @@ -233,9 +233,6 @@ def dependant_mobs(self): visited = set() stack = [self] - if not self.root.content.mob_dict: - self.root.content.build_mob_dict() - while stack: mob = stack[-1] children_processed = True diff --git a/tests/test_copy.py b/tests/test_copy.py index 455f3cf..314f138 100644 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -31,9 +31,9 @@ def test_copy(self): with avb.open(test_file_01) as a: with avb.open(result_file) as b: - b.content.build_mob_dict() + for mob_a in a.content.mobs: - mob_b = b.content.mob_dict.get(mob_a.mob_id, None) + mob_b = b.content.find_by_mob_id(mob_a.mob_id) assert mob_b compare(mob_a, mob_b) @@ -59,11 +59,9 @@ def test_copy_mastermob_depends(self): with avb.open(test_file_01) as a: with avb.open(result_file) as b: - b.content.build_mob_dict() - a.content.build_mob_dict() for mob_id in mob_ids: - mob_a = a.content.mob_dict.get(mob_id, None) - mob_b = b.content.mob_dict.get(mob_id, None) + mob_a = a.content.find_by_mob_id(mob_id) + mob_b = b.content.find_by_mob_id(mob_id) compare(mob_a, mob_b) def test_copy_compositionmobs(self): @@ -89,11 +87,9 @@ def test_copy_compositionmobs(self): with avb.open(test_file_01) as a: with avb.open(result_file) as b: - b.content.build_mob_dict() - a.content.build_mob_dict() for mob_id in mob_ids: - mob_a = a.content.mob_dict.get(mob_id, None) - mob_b = b.content.mob_dict.get(mob_id, None) + mob_a = a.content.find_by_mob_id(mob_id) + mob_b = b.content.find_by_mob_id(mob_id) compare(mob_a, mob_b) if __name__ == "__main__":