curds: Add a way to get a node from its path
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
79ab9d3a40
commit
0994e8986e
|
@ -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 [ ]
|
||||
|
|
|
@ -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 ]))
|
||||
|
|
Loading…
Reference in New Issue