curds: Add a way to find a node's path

A path is a list of indices in the tree, which can easily be translated
into a GtkTreePath

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

View File

@ -29,6 +29,11 @@ class PlaylistNode:
self.__link_children__(prev, child)
return self.children.index(child)
def get_path(self):
if self.parent == None:
return [ ]
return self.parent.get_path() + [ self.node_index() ]
def node_index(self):
if self.parent == None:
return None

View File

@ -124,3 +124,18 @@ class TestPlaylistNode(unittest.TestCase):
self.assertEqual(b.parent, a)
self.assertEqual(a.parent, root)
self.assertIsNone(root.parent)
def test_node_get_path(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.assertEqual(root.get_path(), [ ])
self.assertEqual(a.get_path(), [ 0 ])
self.assertEqual(b.get_path(), [ 0, 0 ])
self.assertEqual(c.get_path(), [ 0, 0, 0 ])