-
Notifications
You must be signed in to change notification settings - Fork 2
/
quetzalcoatl.py
executable file
·2355 lines (1835 loc) · 70.9 KB
/
quetzalcoatl.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import types
from PyQt4 import QtCore, QtGui
from mpd import MPDClient, MPDError
from PyKDE4 import kdecore, kdeui
import socket
class Parser(object):
@classmethod
def isValid(cls, song):
return "file" in song and len(song["file"].strip()) > 0
@classmethod
def hasKey(cls, song, key):
return cls.isValid(song) and key in song
@classmethod
def valueList(cls, song, key):
# Because tags may contain multiple values.
# Assumes the key is there.
values = set()
if not cls.hasKey(song, key):
return values
if type(song[key]) == types.ListType:
values = set()
for value in song[key]:
if len(value.strip()) > 0:
values.add(value)
else:
if len(song[key].strip()) > 0:
values.add(song[key])
return values
@classmethod
def match(cls, song, key, value):
if not cls.hasKey(song, key):
return False
return value in cls.valueList(song, key)
@classmethod
def title(cls, song):
if cls.hasKey(song, "title"):
return song["title"]
return os.path.splitext(os.path.basename(song["file"]))[0]
@classmethod
def length(cls, song):
return cls.prettyTime(int(song["time"]))
@classmethod
def track(cls, song):
# The "track" key is a freeform string and may or may not exist.
# "1/12" and "1" are both common. We also check for malformed tags.
NO_TRACK = 32768
trackNo = 0
if "track" in song:
track = song["track"].strip()
if len(track) > 0:
found = False
index = 0
for i in xrange(len(track)):
if not track[i].isdigit():
index = i
found = True
break
if found:
if len(track[0: index].strip()) > 0:
trackNo = int(track[0:index])
else:
trackNo = cls.NO_TRACK
else:
trackNo = int(track)
else:
trackNo = NO_TRACK
else:
trackNo = NO_TRACK
return trackNo
@classmethod
def total(cls, status):
return int(status["time"][status["time"].index(":") + 1:])
@classmethod
def elapsed(cls, status):
return int(status["time"][0:status["time"].index(":")])
@classmethod
def prettyTime(cls, time):
seconds = time % 60
minutes = (time % 3600) // 60
hours = time // 3600
pretty = str(seconds)
if seconds < 10:
pretty = "0" + pretty
pretty = str(minutes) + ":" + pretty
if time > 3600:
if minutes < 10:
pretty = "0" + pretty
pretty = str(hours) + ":" + pretty
return pretty
@classmethod
def prettyStatusTime(cls, status):
return cls.prettyTime(cls.elapsed(status)) + "/" \
+ cls.prettyTime(cls.total(status))
@classmethod
def parsedValue(cls, song, key):
# For the tooltips
first = True
valueString = ""
for value in cls.valueList(song, key):
if first:
first = False
else:
valueString = valueString + ", "
valueString = valueString + value.strip().decode("utf-8")
return valueString
class Client(object):
client = None
@classmethod
def create(cls):
cls.client = MPDClient()
@classmethod
def delete(cls):
cls.client = None
@classmethod
def exists(cls):
return not cls.client is None
@classmethod
def cmd(cls, command, a = None, b = None, c = None):
if c is not None:
return getattr(cls.client, command)(str(a), str(b), str(c))
if b is not None:
return getattr(cls.client, command)(str(a), str(b))
if a is not None:
return getattr(cls.client, command)(str(a))
return getattr(cls.client, command)()
class IdleThread(QtCore.QThread):
def __init__(self, parent = None):
super(IdleThread, self).__init__(parent)
self.mpdClient = None
@property
def client(self):
return self.mpdClient
@client.setter
def client(self, value):
self.mpdClient = value
def run(self):
while (Client.exists()):
playlists = None
try:
self.mpdClient.idle("stored_playlist")
self.emit(QtCore.SIGNAL("playlists"),
self.mpdClient.listplaylists())
except:
pass
class Idler(QtCore.QObject):
def __init__(self, parent = None):
super(Idler, self).__init__(parent)
self.options = Options()
self.idleClient = None
self.idleThread = None
def start(self):
self.idleClient = MPDClient()
self.idleClient.connect(str(self.options.host), self.options.port)
self.idleThread = IdleThread()
if self.options.needPassword:
self.idleClient.password(str(self.options.password))
self.idleThread.client = self.idleClient
self.connect(self.idleThread, QtCore.SIGNAL("playlists"),
self.setPlaylistsChanged)
self.idleThread.start()
def stop(self):
try:
self.idleClient.noidle()
self.idleClient.disconnect()
except:
pass
def setPlaylistsChanged(self, playlists):
sortedList = sorted(playlists, key = self.sortingKey)
self.emit(QtCore.SIGNAL("playlists"), sortedList)
def sortingKey(self, element):
return element["playlist"].strip().lower()
class Options(object):
def __init__(self):
self.config = kdecore.KSharedConfig.openConfig("quetzalcoatlrc")
self.connectionGroup = self.config.group("Connection")
@property
def host(self):
return self.connectionGroup.readEntry("host", "localhost").toString()
@host.setter
def host(self, value):
self.connectionGroup.writeEntry("host", value)
@property
def port(self):
return self.connectionGroup.readEntry("port", 6600).toInt()[0]
@port.setter
def port(self, value):
self.connectionGroup.writeEntry("port", value)
@property
def needPassword(self):
return self.connectionGroup.readEntry("needPassword", False).toBool()
@needPassword.setter
def needPassword(self, value):
self.connectionGroup.writeEntry("needPassword", value)
@property
def password(self):
return self.connectionGroup.readEntry("password", "").toString()
@password.setter
def password(self, value):
self.connectionGroup.writeEntry("password", value)
def save(self):
self.config.sync()
class Connector(QtCore.QObject):
SECOND = 1000
NOT_UPDATEABLE = False
UPDATEABLE = True
def __init__(self, parent):
super(Connector, self).__init__(parent)
self.connectables = []
self.timer = QtCore.QTimer()
self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.update)
self.idler = Idler()
self.updateables = []
self.options = Options()
def toggleConnected(self):
if Client.exists():
self.disconnectFromClient()
else:
self.connectToClient()
def connectToClient(self):
Client.create()
connected = False
try:
Client.cmd("connect", self.options.host, self.options.port)
if self.options.needPassword:
Client.cmd("password", self.options.password)
connected = True
except (MPDError, socket.error) as e:
Client.delete()
kdeui.KMessageBox.detailedError(self.parent(),\
"Cannot connect to MPD", str(e), "Cannot Connect")
if connected:
for connectable in self.connectables:
connectable.clientConnect()
self.update()
self.updatePlaylists()
self.timer.start(Connector.SECOND)
self.idler.start()
def update(self):
try:
for updateable in self.updateables:
if Client.exists():
updateable.update(Client.cmd("status"))
except (MPDError, socket.error) as e:
self.setBroken(e)
def setBroken(self, e):
self.disconnectFromClient()
kdeui.KMessageBox.detailedError(self.parent(),\
"Connection Lost", str(e), "Connection Lost")
def disconnectFromClient(self):
self.timer.stop()
for connectable in self.connectables:
connectable.clientDisconnect()
self.playlistModel.clientDisconnect()
try:
Client.cmd("disconnect")
except:
pass
Client.delete()
self.idler.stop()
def addConnectable(self, connectable, updateable = False):
connectable.setConnector(self)
self.connectables.append(connectable)
if updateable:
self.updateables.append(connectable)
def updatePlaylists(self):
try:
if Client.exists():
playlists = Client.cmd("listplaylists")
sortedPlaylists = sorted(playlists, key = self.sortingKey)
self.playlistModel.setPlaylists(sortedPlaylists)
except (MPDError, socket.error) as e:
self.setBroken(e)
def addPlaylistModel(self, model):
model.setConnector(self)
self.playlistModel = model
self.connect(self.idler, QtCore.SIGNAL("playlists"),
self.playlistModel.setPlaylists)
def sortingKey(self, element):
return element["playlist"].strip().lower()
class Configurer(kdeui.KDialog):
PLAYBACK_OPTIONS = 1
def __init__(self, parent):
super(Configurer, self).__init__(parent)
self.options = Options()
self.setWindowIcon(kdeui.KIcon("configure"))
self.setCaption("Configure")
self.tabs = kdeui.KTabWidget(self)
connectionWidget = QtGui.QWidget()
self.setMainWidget(self.tabs)
self.tabs.addTab(connectionWidget, "Connection")
layout = QtGui.QFormLayout(connectionWidget)
self.setButtons(self.ButtonCode(\
self.Cancel | self.Ok | self.Default))
# http://forums.asp.net/p/1178692/1992103.aspx#1992103
hostRx = QtCore.QRegExp("^[a-zA-Z0-9]+([a-zA-Z0-9\-\.]+)?\.(com|org|"
"net|mil|edu|COM|ORG|NET|MIL|EDU)$")
hostValidator = QtGui.QRegExpValidator(hostRx, self)
self.host = kdeui.KLineEdit(self.options.host)
self.host.setValidator(hostValidator)
layout.addRow(self.tr("&Host:"), self.host)
self.port = kdeui.KIntSpinBox(0, 65535, 1, self.options.port, self)
layout.addRow(self.tr("&Port:"), self.port)
self.pwCheck = QtGui.QCheckBox()
layout.addRow(self.tr("&Use Password:"), self.pwCheck)
self.password = kdeui.KLineEdit(self.options.password)
self.password.setPasswordMode(True)
layout.addRow(self.tr("Pass&word:"), self.password)
self.connect(self.pwCheck, QtCore.SIGNAL("stateChanged(int)"),\
self.togglePassword)
self.connect(self, QtCore.SIGNAL("okClicked()"), self,\
QtCore.SLOT("accept()"))
self.connect(self, QtCore.SIGNAL("cancelClicked()"), self,\
QtCore.SLOT("reject()"))
self.connect(self, QtCore.SIGNAL("defaultClicked()"), self.defaults)
playbackWidget = QtGui.QWidget()
self.tabs.addTab(playbackWidget, "Playback")
layout = QtGui.QFormLayout(playbackWidget)
self.fade = kdeui.KIntSpinBox()
self.fade.setMinimum(0)
self.fade.setMaximum(20)
layout.addRow("&Crossfade (in seconds)", self.fade)
self.volume = kdeui.KIntSpinBox()
self.volume.setMinimum(0)
self.volume.setMaximum(100)
layout.addRow("&Volume", self.volume)
self.connect(self.tabs, QtCore.SIGNAL("currentChanged(int)"),\
self.changeTabs)
def changeTabs(self, index):
if index == Configurer.PLAYBACK_OPTIONS:
self.setup()
def exec_(self):
self.host.setText(self.options.host)
self.port.setValue(self.options.port)
self.pwCheck.setChecked(self.options.needPassword)
self.password.setText(self.options.password)
self.togglePassword()
try:
status = Client.cmd("status")
self.fade.setEnabled(True)
self.fade.setValue(int(status["xfade"]))
self.volume.setEnabled(True)
self.volume.setValue(int(status["volume"]))
except:
# If the client is not connected
self.fade.setValue(0)
self.fade.setEnabled(False)
self.volume.setValue(0)
self.volume.setEnabled(False)
kdeui.KDialog.exec_(self)
def togglePassword(self):
self.password.setEnabled(self.pwCheck.isChecked())
if not self.password.isEnabled():
self.password.clear()
def accept(self):
self.options.port = self.port.value()
self.options.host = self.host.text()
self.options.password = self.password.text()
self.options.needPassword = self.pwCheck.isChecked()
self.options.save()
try:
if self.fade.isEnabled():
Client.cmd("crossfade", self.fade.value())
except:
pass
try:
if self.volume.isEnabled():
Client.cmd("volume", self.volume.value())
except Exception as e:
# Setting the volume doesn't work on my development system,
# which uses OSS4.
if "volume" in str(e):
print str(e)
QtGui.QDialog.accept(self)
def defaults(self):
self.host.setText("localhost")
self.port.setText("6600")
self.pwCheck.setChecked(False)
self.password.setText("")
self.password.setEnabled(False)
class Node(object):
def __init__(self, parent = None):
self.nodeParent = parent
self.children = []
self.isALeaf = False
self.fetched = True
def childCount(self):
return len(self.children)
def setFetched(self, isFetched):
self.fetched = isFetched
def isFetched(self):
return self.fetched
def setLeaf(self, isLeaf):
self.isALeaf = isLeaf
def isLeaf(self):
return self.isALeaf
def setParent(self, parent):
self.nodeParent = parent
def parent(self):
return self.nodeParent
def row(self):
return self.nodeParent.children.index(self)
def __getitem__(self, i):
return self.children[i]
def setChildren(self, children):
self.children = children
def clear(self):
del self.children[:]
def data(self, column):
raise NotImplementedError
def preFetch(self):
raise NotImplementedError
def postFetch(self):
raise NotImplementedError
def insertCount(self):
raise NotImplementedError
# These two are to get values from SongNodes
def time(self):
raise NotImplementedError
def track(self):
raise NotImplementedError
# These only work for the immediate parents of song nodes.
def uri(self, row):
return self.children[row].myUri()
def uris(self):
return [child.myUri() for child in self.children]
# And this only work for song nodes
def myUri(self):
raise NotImplementedError
class FetchingNode(Node):
def __init__(self, fetcher, data = None, parent = None):
super(FetchingNode, self).__init__(parent)
fetcher.setNode(self)
self.fetcher = fetcher
self.preFetched = []
self.nodeData = data
def preFetch(self):
self.fetcher.preFetch()
def insertCount(self):
return len(self.preFetched)
def postFetch(self):
self.setChildren(self.preFetched)
def addNode(self, node):
self.preFetched.append(node)
def clientConnect(self):
self.setFetched(False)
def clientDisconnect(self):
self.clear()
self.setFetched(True)
def data(self):
if self.nodeData:
return QtCore.QVariant(self.nodeData.decode("utf-8"))
return QtCore.QVariant()
# The next four only work for playlists.
def modified(self):
return self.nodeData["last-modified"]
def setModified(self, modified):
self.nodeData["last-modified"] = modified
def playlist(self):
return self.nodeData["playlist"]
def setPlaylist(self, name):
self.nodeData["playlist"] = name
class SongNode(Node):
def __init__(self, song, parent = None):
super(SongNode, self).__init__(parent)
self.song = song
self.setLeaf(True)
def data(self):
stripped = Parser.title(self.song).strip()
return QtCore.QVariant(stripped.decode("utf-8"))
def myUri(self):
return self.song["file"]
# For the tooltips
def value(self, key):
# We assume that the song contains the key.
return Parser.parsedValue(self.song, key)
# Again, for the tooltips
def hasKey(self, key):
return Parser.hasKey(self.song, key)
class PlaylistNode(FetchingNode):
def __init__(self, playlist, parent = None):
fetcher = PlaylistSongsFetcher(playlist["playlist"])
fetcher.setNode(self)
super(PlaylistNode, self).__init__(fetcher, playlist, parent)
self.setFetched(False)
def data(self):
return QtCore.QVariant(self.playlist().decode("utf-8"))
class Fetcher(QtCore.QObject):
def __init__(self):
super(Fetcher, self).__init__()
self.myNode = None
def setNode(self, node):
self.myNode = node
def addNode(self, node):
self.myNode.addNode(node)
def preFetch(self):
raise NotImplementedError
def node(self):
return self.myNode
class MenuFetcher(Fetcher):
def __init__(self):
super(MenuFetcher, self).__init__()
def preFetch(self):
for item in sorted(self.list(), key = str.lower):
self.addNode(self.createNode(item))
def list(self):
raise NotImplementedError
def createNode(self, data):
raise NotImplementedError
class ListFetcher(Fetcher):
def __init__(self, type):
super(ListFetcher, self).__init__()
self.type = type
def preFetch(self):
for item in sorted(self.list(self.type), key = str.lower):
self.addNode(self.createNode(item))
def list(self, type):
tags = []
for tag in Client.cmd("list", type):
if len(tag.strip()) > 0:
tags.append(tag)
return tags
def createNode(self, data):
raise NotImplementedError
class GenresFetcher(ListFetcher):
def __init__(self):
super(GenresFetcher, self).__init__("genre")
def preFetch(self):
node = FetchingNode(ArtistsFetcher(), "All Artists", self.node())
node.setFetched(False)
self.addNode(node)
super(GenresFetcher, self).preFetch()
def createNode(self, data):
fetcher = GenreArtistsFetcher(data)
node = FetchingNode(fetcher, data, self.node())
node.setFetched(False)
return node
class GenreArtistsFetcher(MenuFetcher):
def __init__(self, genre):
super(GenreArtistsFetcher, self).__init__()
self.genre = genre
def preFetch(self):
node = FetchingNode(GenreSongsFetcher(self.genre), "All", self.node())
node.setFetched(False)
self.addNode(node)
super(GenreArtistsFetcher, self).preFetch()
def list(self):
artists = set()
for song in Client.cmd("find", "genre", self.genre):
artists = artists | Parser.valueList(song, "artist")
return artists
def createNode(self, data):
fetcher = GenreArtistAlbumsFetcher(self.genre, data)
node = FetchingNode(fetcher, data, self.node())
node.setFetched(False)
return node
class GenreArtistAlbumsFetcher(MenuFetcher):
def __init__(self, genre, artist):
super(GenreArtistAlbumsFetcher, self).__init__()
self.genre = genre
self.artist = artist
def preFetch(self):
songsFetcher = GenreArtistSongsFetcher(self.genre, self.artist)
node = FetchingNode(songsFetcher, "All Songs", self.node())
node.setFetched(False)
self.addNode(node)
super(GenreArtistAlbumsFetcher, self).preFetch()
def list(self):
albums = set()
for song in Client.cmd("find", "genre", self.genre):
if Parser.match(song, "artist", self.artist):
albums = albums | Parser.valueList(song, "album")
return albums
def createNode(self, data):
fetcher = GenreArtistAlbumSongsFetcher(self.genre, self.artist, data)
node = FetchingNode(fetcher, data, self.node())
node.setFetched(False)
return node
class SongsFetcher(Fetcher):
def __init__(self, client = None):
super(SongsFetcher, self).__init__()
def cmp(self, a, b):
return cmp(Parser.title(a).lower(), Parser.title(b).lower())
def createNode(self, song):
return SongNode(song, self.node())
def songs(self):
raise NotImplementedError
class AllSongsFetcher(SongsFetcher):
def __init__(self):
super(AllSongsFetcher, self).__init__()
def preFetch(self):
songList = self.songs()
songList.sort(self.cmp)
for song in songList:
self.addNode(self.createNode(song))
def songs(self):
songList = []
for song in Client.cmd("listallinfo"):
if Parser.isValid(song):
songList.append(song)
return songList
class AlbumFetcher(AllSongsFetcher):
def __init__(self):
super(AlbumFetcher, self).__init__()
def cmp(self, a, b):
trackOfA = Parser.track(a)
trackOfB = Parser.track(b)
if trackOfA <> trackOfB:
return cmp(trackOfA, trackOfB)
return super(AlbumFetcher, self).cmp(a, b)
class GenreArtistAlbumSongsFetcher(AlbumFetcher):
def __init__(self, genre, artist, album):
super(GenreArtistAlbumSongsFetcher, self).__init__()
self.genre = genre
self.artist = artist
self.album = album
def songs(self):
songList = []
for song in Client.cmd("find", "genre", self.genre):
if Parser.match(song, "artist", self.artist):
if Parser.match(song, "album", self.album):
songList.append(song)
return songList
class ArtistsFetcher(ListFetcher):
def __init__(self):
super(ArtistsFetcher, self).__init__("artist")
def preFetch(self):
node = FetchingNode(AlbumsFetcher(), "All Albums", self.node())
node.setFetched(False)
self.addNode(node)
super(ArtistsFetcher, self).preFetch()
def createNode(self, data):
fetcher = ArtistAlbumsFetcher(data)
node = FetchingNode(fetcher, data, self.node())
node.setFetched(False)
return node
class AlbumsFetcher(ListFetcher):
def __init__(self):
super(AlbumsFetcher, self).__init__("album")
def preFetch(self):
node = FetchingNode(AllSongsFetcher(), "All Songs", self.node())
node.setFetched(False)
self.addNode(node)
super(AlbumsFetcher, self).preFetch()
def createNode(self, data):
node = FetchingNode(AlbumSongsFetcher(data), data, self.node())
node.setFetched(False)
return node
class AlbumSongsFetcher(AlbumFetcher):
def __init__(self, album):
super(AlbumSongsFetcher, self).__init__()
self.album = album
def songs(self):
songList = []
for song in Client.cmd("find", "album", self.album):
if Parser.isValid(song):
songList.append(song)
return songList
class ArtistAlbumsFetcher(MenuFetcher):
def __init__(self, artist):
super(ArtistAlbumsFetcher, self).__init__()
self.artist = artist
def preFetch(self):
node = FetchingNode(ArtistSongsFetcher(self.artist), "All Songs",\
self.node())
node.setFetched(False)
self.addNode(node)
super(ArtistAlbumsFetcher, self).preFetch()
def list(self):
albums = []
for album in Client.cmd("list", "album", self.artist):
if len(album.strip()) > 0:
albums.append(album)
return albums
def createNode(self, data):
fetcher = ArtistAlbumSongsFetcher(self.artist, data)
node = FetchingNode(fetcher, data, self.node())
node.setFetched(False)
return node
class ArtistAlbumSongsFetcher(AlbumFetcher):
def __init__(self, artist, album):
super(ArtistAlbumSongsFetcher, self).__init__()
self.artist = artist
self.album = album
def songs(self):
songList = []
for song in Client.cmd("find", "artist", self.artist):
if Parser.match(song, "album", self.album):
songList.append(song)
return songList
class ComposersFetcher(ListFetcher):
def __init__(self):
super(ComposersFetcher, self).__init__("composer")
def preFetch(self):
node = FetchingNode(AlbumsFetcher(), "All Albums", self.node())
node.setFetched(False)
self.addNode(node)
super(ComposersFetcher, self).preFetch()
def createNode(self, data):
node = FetchingNode(ComposerAlbumsFetcher(data), data, self.node())
node.setFetched(False)
return node
class ComposerAlbumsFetcher(MenuFetcher):
def __init__(self, composer):
super(ComposerAlbumsFetcher, self).__init__()
self.composer = composer
def preFetch(self):
node = FetchingNode(ComposerSongsFetcher(self.composer), "All Songs",\
self.node())
node.setFetched(False)
self.addNode(node)
super(ComposerAlbumsFetcher, self).preFetch()
def list(self):
albums = set()
for song in Client.cmd("find", "composer", self.composer):
albums = albums | Parser.valueList(song, "album")
return albums
def createNode(self, data):
fetcher = ComposerAlbumSongsFetcher(self.composer, data)
node = FetchingNode(fetcher, data, self.node())
node.setFetched(False)
return node
class ComposerAlbumSongsFetcher(AlbumFetcher):
def __init__(self, composer, album):
super(ComposerAlbumSongsFetcher, self).__init__()
self.composer = composer
self.album = album
def songs(self):
songList = []
for song in Client.cmd("find", "composer", self.composer):
if Parser.match(song, "album", self.album):
songList.append(song)
return songList
class GenreSongsFetcher(AllSongsFetcher):
def __init__(self, genre):
super(GenreSongsFetcher, self).__init__()
self.genre = genre
def songs(self):
songList = []
for song in Client.cmd("find", "genre", self.genre):
songList.append(song)
return songList
class ArtistSongsFetcher(AllSongsFetcher):
def __init__(self, artist):
super(ArtistSongsFetcher, self).__init__()
self.artist = artist
def songs(self):
songList = []
for song in Client.cmd("find", "artist", self.artist):
songList.append(song)
return songList
class ComposerSongsFetcher(AllSongsFetcher):