curds: Keep a mapping of all descendant node IDs

We're going to use this to be able to look up by ID from any anscestor
of the node in question.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2019-12-12 16:02:01 -05:00
parent d020f4163b
commit 030e470f95
3 changed files with 29 additions and 6 deletions

View File

@ -55,12 +55,14 @@ class TestPlaylistNode(unittest.TestCase):
def test_node_setstate(self):
root = node.PlaylistNode()
state = { "children" : [ 1 ], "icon" : "icon", "name" : "name",
child = node.PlaylistNode("1")
state = { "children" : [ child ], "icon" : "icon", "name" : "name",
"sibling" : 2, "parent" : 3 }
node.nodes.clear()
root.__setstate__(state)
self.assertEqual(root.children, [ 1 ])
self.assertEqual(root.children, [ child ])
self.assertEqual(root.icon, "icon")
self.assertEqual(root.name, "name")
self.assertEqual(root.sibling, 2)

View File

@ -26,7 +26,8 @@ class TestETree(unittest.TestCase):
self.assertEqual(etree.name, "Test Tree")
self.assertEqual(etree.icon, "test-icon")
self.assertEqual( etree.children, [ ])
self.assertEqual( etree.children, [ ])
self.assertEqual( etree.child_ids, { })
self.assertFalse( etree.tree_lock.locked())
self.assertIsNone(etree.parent)
self.assertIsNone(etree.sibling)
@ -44,20 +45,24 @@ class TestETree(unittest.TestCase):
self.assertEqual(state["children"], [ ])
self.assertEqual(state["parent"], None)
self.assertEqual(state["sibling"], None)
self.assertNotIn("child_ids", state)
self.assertNotIn("tree_lock", state)
def test_etree_setstate(self):
etree = tree.ETree()
ctree = tree.ETree()
state = { "icon" : "test-icon", "name" : "Test Tree",
"children" : [ 1 ], "parent" : 2, "sibling" : 3 }
"children" : [ ctree ], "parent" : 2, "sibling" : 3 }
etree.tree_lock = None
ctree.child_ids = { id(1) : 1 }
etree.__setstate__(state)
self.assertEqual(etree.icon, "test-icon")
self.assertEqual(etree.name, "Test Tree")
self.assertEqual(etree.children, [ 1 ])
self.assertEqual(etree.children, [ ctree ])
self.assertEqual(etree.parent, 2)
self.assertEqual(etree.sibling, 3)
self.assertEqual(etree.child_ids, { id(1) : 1, id(ctree) : ctree })
self.assertFalse(etree.tree_lock.locked())
def test_etree_child_index(self):
@ -95,6 +100,7 @@ class TestETree(unittest.TestCase):
self.assertEqual(root.find_child("C", "icon"), c)
self.assertEqual(root.find_child("D", "icon"), d)
self.assertEqual(root.children, [ a, b, c, d ])
self.assertEqual(root.child_ids, { id(a) : a, id(b) : b, id(c) : c, id(d) : d })
def test_etree_get_markup(self):
etree = tree.ETree("Test Tree", "test-icon")

View File

@ -9,6 +9,7 @@ class ETree:
self.name = name
self.children = [ ]
self.child_ids = dict()
self.tree_lock = threading.Lock()
self.parent = None
self.sibling = None
@ -16,6 +17,7 @@ class ETree:
def __getstate__(self):
with self.tree_lock:
state = self.__dict__.copy()
del state["child_ids"]
del state["tree_lock"]
return state
@ -28,6 +30,7 @@ class ETree:
with prev.tree_lock:
prev.sibling = child
self.__map_child__(child)
if len(self.children) == 1:
notify.notify("first-child", self, self.get_path())
notify.notify("child-inserted", child, self.get_path() + [ index ])
@ -36,13 +39,25 @@ class ETree:
def __nth_child__(self, n):
return self.children[n] if -1 < n < len(self.children) else None
def __map_child__(self, child):
self.child_ids[id(child)] = child
self.child_ids.update(child.child_ids)
if self.parent != None:
with self.parent.tree_lock:
self.parent.__map_child__(child)
def __setstate__(self, state):
self.parent = None
self.child_ids = dict()
self.tree_lock = threading.Lock()
self.icon = state["icon"]
self.name = state["name"]
self.children = state["children"]
for child in self.children:
self.__map_child__(child)
self.parent = state["parent"]
self.sibling = state["sibling"]
self.tree_lock = threading.Lock()
def alloc_child(self, name, icon):
return ETree(name, icon)