forked from albertz/music-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.py
56 lines (48 loc) · 1.59 KB
/
Search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
# MusicPlayer, https://github.com/albertz/music-player
# Copyright (c) 2012, Albert Zeyer, www.az2000.de
# All rights reserved.
# This code is under the 2-clause BSD license, see License.txt in the root directory of this project.
import utils, math
from utils import UserAttrib, initBy, Event, formatTime
import Traits
import songdb
import gui
class Search:
Keys = ("artist", "title", "duration", "rating")
def __init__(self):
self._searchText = ""
self._searchResults = []
import threading
self._lock = threading.RLock()
def _startSearch(self, txt):
def search():
with self._lock:
if self._searchText != txt: return
res = songdb.search(txt)
with self._lock:
if self._searchText == txt:
self._searchResults = res
self.searchResults_updateEvent.push()
with self._lock:
self._searchText = txt
utils.daemonThreadCall(search, name="Song DB search")
@UserAttrib(type=Traits.EditableText, searchLook=True)
def searchText(self, updateText=None):
with self._lock:
if updateText is not None and self._searchText != updateText:
self._startSearch(updateText)
return self._searchText
@UserAttrib(type=Traits.Table(keys=Keys,
format_duration=lambda d: formatTime(d) if d > 0 else "",
format_rating=lambda r: "★" * int(round(r * 5))),
variableHeight=True)
@property
def searchResults(self):
with self._lock:
return list(self._searchResults)
@searchResults.setUpdateEvent
@initBy
def searchResults_updateEvent(self): return Event()
search = Search()
gui.registerRootObj(obj=search, name="Search", priority=-1, keyShortcut='2')