Permalink
Browse files

Allow searching descendants.

  • Loading branch information...
1 parent 9876c3f commit 049b5a912f0eed24d8fa5af9fd612c9aa896791b @SteveMarshall committed Jan 21, 2009
Showing with 35 additions and 0 deletions.
  1. +18 −0 atom.py
  2. +17 −0 atomtest.py
View
18 atom.py
@@ -233,6 +233,24 @@ def get_all_descendants(self):
return descendants
+ def get_children_of_type(self, type):
+ children = []
+ if self.is_container():
+ [children.append(child) for child in self if child.type == type]
+
+ return children
+
+ def get_descendants_of_type(self, type):
+ descendants = []
+ if self.is_container():
+ for child in self:
+ if child.type == type:
+ descendants.append(child)
+ descendants += child.get_descendants_of_type(type)
+
+ return descendants
+
+
# File-like behaviours
def next(self):
View
@@ -386,6 +386,23 @@ def testGetAllDescendants(self):
self.assertEqual(loaded_atom[0][1], descendants[2])
self.assertEqual(loaded_atom[1], descendants[3])
+ def testGetChildrenOfType(self):
+ loaded_atom = atom.Atom(self.atom_stream)
+ children_of_type = loaded_atom.get_children_of_type(self.child_1_type)
+
+ self.assertEqual(1, len(children_of_type))
+ self.assertEqual(loaded_atom[0], children_of_type[0])
+
+ def testGetDescendantsOfType(self):
+ loaded_atom = atom.Atom(self.atom_stream)
+ descendants_of_type = loaded_atom.get_descendants_of_type(self.child_1_1_type)
+
+ self.assertEqual(3, len(descendants_of_type))
+ self.assertEqual(loaded_atom[0][0], descendants_of_type[0])
+ self.assertEqual(loaded_atom[0][1], descendants_of_type[1])
+ self.assertEqual(loaded_atom[1], descendants_of_type[2])
+
+
class LoadedContainerAtomChildManipulation(unittest.TestCase):
type = 'moov'

0 comments on commit 049b5a9

Please sign in to comment.