playlist: Create a custom filter for matching tracks

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-07-08 11:49:15 -04:00
parent 0952eeccde
commit e8fc15dc6c
2 changed files with 43 additions and 0 deletions

28
playlist/filter.py Normal file
View File

@ -0,0 +1,28 @@
# Copyright 2021 (c) Anna Schumaker.
from gi.repository import Gtk
import re
class Filter(Gtk.CustomFilter):
def __init__(self):
Gtk.CustomFilter.__init__(self)
self.text = None
self.pattern = re.compile("")
self.set_filter_func(self.filter_func)
def filter_func(self, track):
for field in [ "artist", "album", "title" ]:
if self.pattern.search(track[field]) != None:
return True
return False
def set_search_text(self, text):
change = Gtk.FilterChange.DIFFERENT
if self.text == None or self.text in text:
Gtk.FilterChange.MORE_STRICT
elif text in self.text:
Gtk.FilterChange.LESS_STRICT
self.pattern = re.compile(text, re.I)
self.text = text
self.changed(change)

15
playlist/test_filter.py Normal file
View File

@ -0,0 +1,15 @@
# Copyright 2021 (c) Anna Schumaker.
from . import filter
from gi.repository import Gtk
import unittest
class TestPlaylistFilter(unittest.TestCase):
def test_playlist_filter_init(self):
f = filter.Filter()
self.assertIsInstance(f, Gtk.CustomFilter)
self.assertIsNone(f.text)
self.assertEqual(f.pattern.pattern, "")
f.set_search_text("text")
self.assertEqual(f.text, "text")
self.assertEqual(f.pattern.pattern, "text")