curds: Move child ID lookups into the ETree

Using the new system keeps us from needing to maintain an external
dictionary of nodes.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2019-12-13 08:34:12 -05:00
parent 030e470f95
commit 53a1e085d4
6 changed files with 20 additions and 44 deletions

View File

@ -5,13 +5,10 @@ from .. import tree
import threading
import unicodedata
nodes = dict()
class PlaylistNode(tree.ETree):
def __init__(self, name="", icon=""):
tree.ETree.__init__(self, name, icon)
self.lock = threading.RLock()
nodes[id(self)] = self
self.__init_common__()
def __init_common__(self):
@ -25,7 +22,6 @@ class PlaylistNode(tree.ETree):
def __setstate__(self, state):
tree.ETree.__setstate__(self, state)
self.lock = threading.RLock()
nodes[id(self)] = self
self.__init_common__()
def append_child(self, child):
@ -37,17 +33,7 @@ class PlaylistNode(tree.ETree):
return self
return self.parent.get_root()
def lookup_byid(self, id):
with self.lock:
node = nodes.get(id)
if node == None or node.get_root() != self.get_root():
return None
return node
def reset(self):
with self.lock:
nodes[id(self)] = self
for child in self.children:
nodes.pop(id(child), None)
self.children.clear()
self.__init_common__()

View File

@ -5,23 +5,16 @@ import threading
import unittest
class TestPlaylistNode(unittest.TestCase):
def setUp(self):
node.nodes.clear()
def test_node_init(self):
self.assertIsInstance(node.nodes, dict)
n = node.PlaylistNode()
self.assertEqual( n.children, [ ])
self.assertEqual( n.icon, "")
self.assertEqual( n.name, "")
self.assertIsNone(n.sibling)
self.assertEqual(node.nodes.get(id(n)), n)
n = node.PlaylistNode("Test Node", "test-icon")
self.assertEqual(n.icon, "test-icon")
self.assertEqual(n.name, "Test Node")
self.assertEqual(node.nodes.get(id(n)), n)
def test_node_append_child(self):
root = node.PlaylistNode()
@ -59,7 +52,6 @@ class TestPlaylistNode(unittest.TestCase):
state = { "children" : [ child ], "icon" : "icon", "name" : "name",
"sibling" : 2, "parent" : 3 }
node.nodes.clear()
root.__setstate__(state)
self.assertEqual(root.children, [ child ])
@ -67,21 +59,6 @@ class TestPlaylistNode(unittest.TestCase):
self.assertEqual(root.name, "name")
self.assertEqual(root.sibling, 2)
self.assertEqual(root.parent, 3)
self.assertEqual(node.nodes[id(root)], root)
def test_node_lookup(self):
root = node.PlaylistNode()
a = node.PlaylistNode("a")
b = node.PlaylistNode("b")
c = node.PlaylistNode("c")
root.insert_child(a)
root.insert_child(b)
self.assertEqual( root.lookup_byid(id(a)), a)
self.assertEqual( root.lookup_byid(id(b)), b)
self.assertIsNone(root.lookup_byid(id(c)))
self.assertIsNone(root.lookup_byid(42))
def test_node_get_root(self):
root = node.PlaylistNode()
@ -108,4 +85,3 @@ class TestPlaylistNode(unittest.TestCase):
root.reset()
self.assertEqual(root.children, [ ])
self.assertEqual(node.nodes, { id(root) : root })

View File

@ -7,9 +7,6 @@ from .. import trak
import unittest
class TestUserPlaylists(unittest.TestCase):
def setUp(self):
node.nodes.clear()
def test_user_node(self):
unode = user.UserNode()
@ -35,11 +32,11 @@ class TestUserPlaylists(unittest.TestCase):
self.assertEqual(unode.nth_child(0), new)
self.assertEqual(len(new), 0)
self.assertIn(id(new), node.nodes.keys())
self.assertIn(id(new), unode.child_ids)
self.assertEqual(unode.nth_child(1), star)
self.assertEqual(len(star), 0)
self.assertIn(id(star), node.nodes.keys())
self.assertIn(id(star), unode.child_ids)
def test_user_starred(self):
unode = user.UserNode()

View File

@ -12,7 +12,6 @@ class TestPlaylist(unittest.TestCase):
def tearDown(self):
notify.clear()
playlist.node.nodes.clear()
playlist.Root = self.orig
playlist.Root.reset()

View File

@ -157,6 +157,21 @@ class TestETree(unittest.TestCase):
self.assertIsNone(root.lookup("C"))
self.assertEqual( root.lookup("D"), d)
def test_etree_lookup_byid(self):
root = tree.ETree()
a = root.insert_child(tree.ETree("A", "icon"))
b = a.insert_child(tree.ETree("B", "icon"))
c = b.insert_child(tree.ETree("C", "icon"))
self.assertEqual(root.lookup_byid(id(a)), a)
self.assertEqual(root.lookup_byid(id(b)), b)
self.assertEqual(root.lookup_byid(id(c)), c)
self.assertEqual( a.lookup_byid(id(b)), b)
self.assertEqual( a.lookup_byid(id(c)), c)
self.assertEqual( b.lookup_byid(id(c)), c)
self.assertIsNone(c.lookup_byid(id(a)))
def test_etree_lookup_path(self):
root = tree.ETree()
a = root.find_child("A", "icon")

View File

@ -95,6 +95,9 @@ class ETree:
(index, child) = sort.bisect(self.children, sort.key(key))
return child
def lookup_byid(self, id):
return self.child_ids.get(id, None)
def lookup_path(self, path):
if path == None:
return None