curds: Add a way to get a node from its path

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2019-04-16 11:16:15 -04:00
parent 79ab9d3a40
commit 0994e8986e
2 changed files with 32 additions and 0 deletions

View File

@ -29,6 +29,15 @@ class PlaylistNode:
self.__link_children__(prev, child)
return self.children.index(child)
def get_node(self, path):
if len(path) == 1:
return self.nth_child(path[0])
elif len(path) > 1:
if path[0] < self.n_children():
node = self.nth_child(path[0])
return node.get_node(path[1:])
return None
def get_path(self):
if self.parent == None:
return [ ]

View File

@ -139,3 +139,26 @@ class TestPlaylistNode(unittest.TestCase):
self.assertEqual(a.get_path(), [ 0 ])
self.assertEqual(b.get_path(), [ 0, 0 ])
self.assertEqual(c.get_path(), [ 0, 0, 0 ])
def test_node_get_node(self):
root = node.PlaylistNode()
a = node.PlaylistNode("a")
b = node.PlaylistNode("b")
c = node.PlaylistNode("c")
root.append_child(a)
a.append_child(b)
b.append_child(c)
self.assertIsNone(root.get_node([ ]))
self.assertEqual( root.get_node([ 0 ]), a)
self.assertIsNone(root.get_node([ 1 ]))
self.assertEqual( root.get_node([ 0, 0 ]), b)
self.assertIsNone(root.get_node([ 0, 1 ]))
self.assertIsNone(root.get_node([ 1, 0 ]))
self.assertEqual( root.get_node([ 0, 0, 0 ]), c)
self.assertIsNone(root.get_node([ 0, 0, 1 ]))
self.assertIsNone(root.get_node([ 0, 1, 0 ]))
self.assertIsNone(root.get_node([ 1, 0, 0 ]))