curds: Clean up how nodes are inserted and linked
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
91a606e054
commit
8ab2d97cf5
|
@ -10,17 +10,25 @@ class PlaylistNode:
|
|||
self.parent = None
|
||||
self.prev_child = None
|
||||
|
||||
def __link_children__(self, b, c):
|
||||
if c.prev_child:
|
||||
c.prev_child.next_child = b
|
||||
if b:
|
||||
b.prev_child = c.prev_child
|
||||
b.next_child = c
|
||||
c.prev_child = b
|
||||
def __link_children__(self, a, b, c):
|
||||
if a:
|
||||
a.next_child = b
|
||||
b.parent = self
|
||||
b.prev_child = a
|
||||
b.next_child = c
|
||||
if c:
|
||||
c.prev_child = b
|
||||
|
||||
def __lt__(self, rhs):
|
||||
return self.name < rhs.name
|
||||
|
||||
def __insert_node__(self, child, index):
|
||||
self.children.insert(index, child)
|
||||
self.__link_children__(self.nth_child(index - 1), child,
|
||||
self.nth_child(index + 1))
|
||||
notify.notify("node-inserted", child, index)
|
||||
return index
|
||||
|
||||
def __normalize__(self, name):
|
||||
return name.title()
|
||||
|
||||
|
@ -28,14 +36,7 @@ class PlaylistNode:
|
|||
return f"<small>\n</small><big>{self.name}</big>"
|
||||
|
||||
def append_child(self, child):
|
||||
prev = None
|
||||
if len(self.children) > 0:
|
||||
prev = self.children[-1]
|
||||
self.children.append(child)
|
||||
child.parent = self
|
||||
self.__link_children__(prev, child)
|
||||
notify.notify("node-inserted", child, child.node_index())
|
||||
return self.children.index(child)
|
||||
return self.__insert_node__(child, len(self.children))
|
||||
|
||||
def get_node(self, path):
|
||||
if len(path) == 1:
|
||||
|
@ -54,6 +55,12 @@ class PlaylistNode:
|
|||
def has_sibling(self):
|
||||
return (self.next_child != None) or (self.prev_child != None)
|
||||
|
||||
def insert_child(self, child):
|
||||
for i, n in enumerate(self.children):
|
||||
if child < n:
|
||||
return self.__insert_node__(child, i)
|
||||
return self.append_child(child)
|
||||
|
||||
def lookup(self, name):
|
||||
name = self.__normalize__(name)
|
||||
for node in self.children:
|
||||
|
@ -61,26 +68,16 @@ class PlaylistNode:
|
|||
return node
|
||||
return None
|
||||
|
||||
def n_children(self):
|
||||
return len(self.children)
|
||||
|
||||
def node_index(self):
|
||||
if self.parent == None:
|
||||
return None
|
||||
return self.parent.children.index(self)
|
||||
|
||||
def insert_child(self, child):
|
||||
for i, n in enumerate(self.children):
|
||||
if child < n:
|
||||
self.children.insert(i, child)
|
||||
child.parent = self
|
||||
self.__link_children__(child, n)
|
||||
notify.notify("node-inserted", child, child.node_index())
|
||||
return self.children.index(child)
|
||||
return self.append_child(child)
|
||||
|
||||
def n_children(self):
|
||||
return len(self.children)
|
||||
|
||||
def nth_child(self, n):
|
||||
if n < len(self.children):
|
||||
if -1 < n < len(self.children):
|
||||
return self.children[n]
|
||||
return None
|
||||
|
||||
|
|
|
@ -137,10 +137,12 @@ class TestPlaylistNode(unittest.TestCase):
|
|||
root.append_child(b)
|
||||
a.append_child(c)
|
||||
|
||||
self.assertIsNone(root.nth_child(-1))
|
||||
self.assertEqual( root.nth_child(0), a)
|
||||
self.assertEqual( root.nth_child(1), b)
|
||||
self.assertIsNone(root.nth_child(2))
|
||||
|
||||
self.assertIsNone(a.nth_child(-1))
|
||||
self.assertEqual( a.nth_child(0), c)
|
||||
self.assertIsNone(a.nth_child(1))
|
||||
|
||||
|
|
Loading…
Reference in New Issue