curds: Replace find_child() with lookup()

We use the allocate version everywhere already, so let's just rename it
to lookup and have a less confusing name.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2019-12-16 16:37:12 -05:00
parent d7ad6c5894
commit 803cb6f186
6 changed files with 55 additions and 71 deletions

View File

@ -13,7 +13,7 @@ class ArtistPlaylist(playlist.Playlist):
def add(self, track):
playlist.Playlist.add(self, track)
self.find_child(track["album"], ALBUM_ICON).add(track)
self.lookup(track["album"], ALBUM_ICON).add(track)
class ArtistNode(tree.ETree):
@ -24,4 +24,4 @@ class ArtistNode(tree.ETree):
return ArtistPlaylist(name, icon, [ "album", "discnumber", "tracknumber" ])
def new_track(self, track):
self.find_child(track["albumartist"], ARTIST_ICON).add(track)
self.lookup(track["albumartist"], ARTIST_ICON).add(track)

View File

@ -13,7 +13,7 @@ class DecadePlaylist(playlist.Playlist):
def add(self, track):
playlist.Playlist.add(self, track)
self.find_child(str(track["date"]), DECADE_ICON).add(track)
self.lookup(str(track["date"]), DECADE_ICON).add(track)
class DecadeNode(tree.ETree):
@ -25,4 +25,4 @@ class DecadeNode(tree.ETree):
[ "date", "artist", "album", "discnumber", "tracknumber" ])
def new_track(self, track):
self.find_child(track.decade(), DECADE_ICON).add(track)
self.lookup(track.decade(), DECADE_ICON).add(track)

View File

@ -14,4 +14,4 @@ class GenreNode(tree.ETree):
[ "artist", "date", "album", "discnumber", "tracknumber" ])
def new_track(self, track):
self.find_child(track["genre"], GENRE_ICON).add(track)
self.lookup(track["genre"], GENRE_ICON).add(track)

View File

@ -35,7 +35,7 @@ class LibraryNode(tree.ETree):
[ "artist", "date", "album", "discnumber", "tracknumber" ])
def lookup(self, path):
library = self.find_child(os.path.abspath(path), LIBRARY_ICON)
library = tree.ETree.lookup(self, os.path.abspath(path), LIBRARY_ICON)
library.scan()
return library

View File

@ -67,50 +67,23 @@ class TestETree(unittest.TestCase):
def test_etree_child_index(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = root.find_child("B", "icon")
c = a.find_child("C", "icon")
a = root.lookup("A", "icon")
b = root.lookup("B", "icon")
c = a.lookup("C", "icon")
self.assertEqual( root.child_index(a), 0)
self.assertEqual( root.child_index(b), 1)
self.assertIsNone(root.child_index(c))
def test_etree_find_child(self):
root = tree.ETree()
b = root.find_child("B", "icon")
self.assertEqual(root.children, [ b ])
self.assertEqual(b.parent, root)
d = root.find_child("D", "icon")
self.assertEqual(root.children, [ b, d ])
self.assertEqual(b.sibling, d)
c = root.find_child("C", "icon")
self.assertEqual(root.children, [ b, c, d ])
self.assertEqual(b.sibling, c)
self.assertEqual(c.sibling, d)
a = root.find_child("A", "icon")
self.assertEqual(root.children, [ a, b, c, d ])
self.assertEqual(a.sibling, b)
self.assertEqual(root.find_child("A", "icon"), a)
self.assertEqual(root.find_child("B", "icon"), b)
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")
self.assertEqual(etree.get_markup(), "<big>Test Tree</big>")
def test_etree_get_path(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = a.find_child("B", "icon")
c = b.find_child("C", "icon")
a = root.lookup("A", "icon")
b = a.lookup("B", "icon")
c = b.lookup("C", "icon")
self.assertEqual(root.get_path(), [ ])
self.assertEqual(a.get_path(), [ 0 ])
@ -148,14 +121,30 @@ class TestETree(unittest.TestCase):
def test_etree_lookup(self):
root = tree.ETree()
a = root.insert_child(tree.ETree("A", "icon"))
b = root.insert_child(tree.ETree("B", "icon"))
d = root.insert_child(tree.ETree("D", "icon"))
self.assertEqual( root.lookup("A"), a)
self.assertEqual( root.lookup("B"), b)
self.assertIsNone(root.lookup("C"))
self.assertEqual( root.lookup("D"), d)
b = root.lookup("B", "icon")
self.assertEqual(root.children, [ b ])
self.assertEqual(b.parent, root)
d = root.lookup("D", "icon")
self.assertEqual(root.children, [ b, d ])
self.assertEqual(b.sibling, d)
c = root.lookup("C", "icon")
self.assertEqual(root.children, [ b, c, d ])
self.assertEqual(b.sibling, c)
self.assertEqual(c.sibling, d)
a = root.lookup("A", "icon")
self.assertEqual(root.children, [ a, b, c, d ])
self.assertEqual(a.sibling, b)
self.assertEqual(root.lookup("A", "icon"), a)
self.assertEqual(root.lookup("B", "icon"), b)
self.assertEqual(root.lookup("C", "icon"), c)
self.assertEqual(root.lookup("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_lookup_byid(self):
root = tree.ETree()
@ -174,9 +163,9 @@ class TestETree(unittest.TestCase):
def test_etree_lookup_path(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = a.find_child("B", "icon")
c = b.find_child("C", "icon")
a = root.lookup("A", "icon")
b = a.lookup("B", "icon")
c = b.lookup("C", "icon")
self.assertEqual( root.lookup_path([ ]), root)
self.assertEqual( root.lookup_path([ 0 ]), a)
@ -194,15 +183,15 @@ class TestETree(unittest.TestCase):
def test_etree_n_children(self):
root = tree.ETree()
self.assertEqual(root.n_children(), 0)
a = root.find_child("A", "icon")
a = root.lookup("A", "icon")
self.assertEqual(root.n_children(), 1)
b = root.find_child("B", "icon")
b = root.lookup("B", "icon")
self.assertEqual(root.n_children(), 2)
def test_etree_next_child(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = root.find_child("B", "icon")
a = root.lookup("A", "icon")
b = root.lookup("B", "icon")
self.assertIsNone(root.next_child())
self.assertEqual( a.next_child(), b)
@ -210,8 +199,8 @@ class TestETree(unittest.TestCase):
def test_etree_nth_child(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = root.find_child("B", "icon")
a = root.lookup("A", "icon")
b = root.lookup("B", "icon")
self.assertIsNone(root.nth_child(-1))
self.assertEqual( root.nth_child(0), a)
@ -220,8 +209,8 @@ class TestETree(unittest.TestCase):
def test_etree_reset(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = root.find_child("B", "icon")
a = root.lookup("A", "icon")
b = root.lookup("B", "icon")
root.reset()
self.assertEqual(root.children, [ ])
@ -235,10 +224,10 @@ class TestETree(unittest.TestCase):
def test_etree_walk(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = a.find_child("B", "icon")
c = a.find_child("C", "icon")
d = b.find_child("D", "icon")
a = root.lookup("A", "icon")
b = a.lookup("B", "icon")
c = a.lookup("C", "icon")
d = b.lookup("D", "icon")
vals = [ a, b, d, c ]
for i, n in enumerate(root.walk()):

View File

@ -74,13 +74,6 @@ class ETree:
return self.children.index(child)
return None
def find_child(self, name, icon):
with self.tree_lock:
(index, node) = sort.bisect(self.children, sort.key(name))
if node:
return node
return self.__insert__(index, self.alloc_child(name, icon))
def get_markup(self):
return f"<big>{self.name}</big>"
@ -96,10 +89,12 @@ class ETree:
return child
return self.__insert__(index, child)
def lookup(self, key):
def lookup(self, name, icon=None):
with self.tree_lock:
(index, child) = sort.bisect(self.children, sort.key(key))
return child
(index, child) = sort.bisect(self.children, sort.key(name))
if child != None:
return child
return self.__insert__(index, self.alloc_child(name, icon))
def lookup_byid(self, id):
return self.child_ids.get(id, None)