curds: Keep a map of PlaylistNode ids
Making it easy to look up a PlaylistNode by the python object id means we can use the object id in the GtkTreeIter rather than storing the entire path Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
cfee39ce17
commit
c834359cdb
|
@ -1,6 +1,8 @@
|
|||
# Copyright 2019 (c) Anna Schumaker.
|
||||
from .. import notify
|
||||
|
||||
nodes = dict()
|
||||
|
||||
class PlaylistNode:
|
||||
def __init__(self, name="", icon=""):
|
||||
self.children = [ ]
|
||||
|
@ -9,6 +11,7 @@ class PlaylistNode:
|
|||
self.next_child = None
|
||||
self.parent = None
|
||||
self.prev_child = None
|
||||
nodes[id(self)] = self
|
||||
|
||||
def __link_children__(self, a, b, c):
|
||||
if a != None:
|
||||
|
@ -73,6 +76,12 @@ class PlaylistNode:
|
|||
return node
|
||||
return None
|
||||
|
||||
def lookup_byid(self, id):
|
||||
node = nodes.get(id)
|
||||
if node == None or node.get_root() != self.get_root():
|
||||
return None
|
||||
return node
|
||||
|
||||
def n_children(self):
|
||||
return len(self.children)
|
||||
|
||||
|
@ -87,4 +96,6 @@ class PlaylistNode:
|
|||
return None
|
||||
|
||||
def reset(self):
|
||||
for child in self.children:
|
||||
del nodes[id(child)]
|
||||
self.children.clear()
|
||||
|
|
|
@ -4,21 +4,28 @@ from .. import notify
|
|||
import unittest
|
||||
|
||||
class TestPlaylistNode(unittest.TestCase):
|
||||
def setUp(self):
|
||||
node.nodes.clear()
|
||||
|
||||
def on_insert_node(self, node, index):
|
||||
self.cb_insert = (node, index)
|
||||
|
||||
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.next_child)
|
||||
self.assertIsNone(n.prev_child)
|
||||
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(str(n), "<big>Test Node</big>")
|
||||
self.assertEqual(node.nodes.get(id(n)), n)
|
||||
|
||||
def test_node_less_than(self):
|
||||
a = node.PlaylistNode("a")
|
||||
|
@ -119,6 +126,7 @@ class TestPlaylistNode(unittest.TestCase):
|
|||
root = node.PlaylistNode()
|
||||
a = node.PlaylistNode("a")
|
||||
b = node.PlaylistNode("b")
|
||||
c = node.PlaylistNode("c")
|
||||
|
||||
root.append_child(a)
|
||||
root.append_child(b)
|
||||
|
@ -127,6 +135,11 @@ class TestPlaylistNode(unittest.TestCase):
|
|||
self.assertEqual( root.lookup("b"), b)
|
||||
self.assertIsNone(root.lookup("c"))
|
||||
|
||||
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_nth_child(self):
|
||||
root = node.PlaylistNode()
|
||||
a = node.PlaylistNode("a")
|
||||
|
@ -236,3 +249,4 @@ class TestPlaylistNode(unittest.TestCase):
|
|||
|
||||
root.reset()
|
||||
self.assertEqual(root.children, [ ])
|
||||
self.assertEqual(node.nodes, { id(root) : root })
|
||||
|
|
Loading…
Reference in New Issue