curds: Add path detection to sort.key()
This lets us remove some special casing in the library playlist, and simplify node inserting. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
cac252cc17
commit
f72c3596e7
|
@ -13,10 +13,6 @@ class LibraryPlaylist(playlist.Playlist):
|
|||
def scan(self):
|
||||
library_thread.push(self.thread_scan)
|
||||
|
||||
def sort_key(self, path=None):
|
||||
key = self.name if path == None else path
|
||||
return key.strip("/").split("/")
|
||||
|
||||
def thread_add(self, path):
|
||||
self.add(trak.lookup(path))
|
||||
|
||||
|
|
|
@ -55,12 +55,6 @@ class TestLibraryPlaylist(unittest.TestCase):
|
|||
library.join()
|
||||
self.assertEqual(len(plist), 12)
|
||||
|
||||
def test_library_sort_key(self):
|
||||
plist = library.LibraryPlaylist(test_library)
|
||||
key = test_library.strip("/").split("/")
|
||||
self.assertEqual(plist.sort_key(), key)
|
||||
self.assertEqual(plist.sort_key("/a/b/c/"), [ "a", "b", "c" ])
|
||||
|
||||
def test_library_thread_reset(self):
|
||||
self.assertTrue(library.library_thread.is_alive())
|
||||
library.stop()
|
||||
|
|
|
@ -1,25 +1,27 @@
|
|||
# Copyright 2019 (c) Anna Schumaker.
|
||||
import unicodedata
|
||||
import os
|
||||
|
||||
def bisect(lst, key, *args):
|
||||
def bisect(lst, lhs, *args):
|
||||
begin = 0
|
||||
end = len(lst)
|
||||
|
||||
while end - begin > 0:
|
||||
pos = (end + begin) // 2
|
||||
cur = lst[pos].sort_key(*args)
|
||||
if cur == key:
|
||||
if lhs == (rhs := lst[pos].sort_key(*args)):
|
||||
return (pos, lst[pos])
|
||||
elif key < cur:
|
||||
elif lhs < rhs:
|
||||
end = pos
|
||||
else:
|
||||
begin = pos + 1
|
||||
return (begin, None)
|
||||
|
||||
def key(text):
|
||||
if os.path.exists(text):
|
||||
return text.strip("/").split("/")
|
||||
words = normalize(text).lower().split()
|
||||
words = [ ''.join(filter(str.isalnum, w)) for w in words ]
|
||||
words = [ w for w in words if w is not "" ]
|
||||
words = [ w for w in words if w != "" ]
|
||||
if words[0] in [ "a", "the" ]:
|
||||
return words[1:]
|
||||
return words
|
||||
|
|
|
@ -38,6 +38,11 @@ class TestSort(unittest.TestCase):
|
|||
self.assertEqual(sort.key("Test A Key"), [ "test", "a", "key" ])
|
||||
self.assertEqual(sort.key("Toäst Keäy"), [ "toast", "keay" ])
|
||||
|
||||
def test_key_path(self):
|
||||
self.assertEqual(sort.key("/a/b/c/d/"), [ "abcd" ])
|
||||
self.assertEqual(sort.key("trier/Test Album"), [ "trier", "Test Album" ])
|
||||
self.assertEqual(sort.key("trier/Test Album/"), [ "trier", "Test Album" ])
|
||||
|
||||
def test_normalize(self):
|
||||
self.assertEqual(sort.normalize("Test Text"), "Test Text")
|
||||
self.assertEqual(sort.normalize("Toäst Keäy"), "Toast Keay")
|
||||
|
|
|
@ -13,11 +13,6 @@ class ETree:
|
|||
self.parent = None
|
||||
self.sibling = None
|
||||
|
||||
def __bisect__(self, name):
|
||||
if len(self.children) == 0:
|
||||
return (0, None)
|
||||
return sort.bisect(self.children, self.children[0].sort_key(name))
|
||||
|
||||
def __getstate__(self):
|
||||
with self.tree_lock:
|
||||
state = self.__dict__.copy()
|
||||
|
@ -61,7 +56,7 @@ class ETree:
|
|||
|
||||
def find_child(self, name, icon):
|
||||
with self.tree_lock:
|
||||
(index, node) = self.__bisect__(name)
|
||||
(index, node) = sort.bisect(self.children, sort.key(name))
|
||||
if node:
|
||||
return node
|
||||
return self.__insert__(index, self.alloc_child(name, icon))
|
||||
|
@ -76,14 +71,14 @@ class ETree:
|
|||
|
||||
def insert_child(self, child):
|
||||
with self.tree_lock:
|
||||
(index, node) = self.__bisect__(child.name)
|
||||
(index, node) = sort.bisect(self.children, sort.key(child.name))
|
||||
if node == child:
|
||||
return child
|
||||
return self.__insert__(index, child)
|
||||
|
||||
def lookup(self, key):
|
||||
with self.tree_lock:
|
||||
(index, child) = self.__bisect__(key)
|
||||
(index, child) = sort.bisect(self.children, sort.key(key))
|
||||
return child
|
||||
|
||||
def lookup_path(self, path):
|
||||
|
|
Loading…
Reference in New Issue