-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_func.py
1486 lines (1326 loc) · 49.5 KB
/
my_func.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
# Copyright 2016 by Dai Trying
#
# This file is part of daixmms2client.
#
# daixmms2client is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# daixmms2client is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with daixmms2client. If not, see <http://www.gnu.org/licenses/>.
import xmmsfun
import math
from PyQt5.Qt import QColor
from PyQt5.QtWidgets import QInputDialog, QFileDialog
from xmmsclient import xmmsvalue
import os
from configparser import SafeConfigParser
from mutagen.id3 import ID3, TIT2, TALB, TPOS, TPE2, TPE1, TRCK, TCON, ID3NoHeaderError
import mutagen
if not os.path.isdir(os.path.join(os.path.expanduser('~'), ".config/daixmmsdata")):
try:
os.makedirs(os.path.join(os.path.expanduser('~'), ".config/daixmmsdata"))
except OSError:
pass
conf_file = os.path.join(os.path.expanduser('~'), ".config/daixmmsdata/config.conf")
def set_new_config_file():
conf_sections1 = ['table_ml_columns_show', 'table_pl_columns_show', 'table_np_columns_show']
conf_sections2 = ['table_ml_columns_size', 'table_pl_columns_size', 'table_np_columns_size']
section1_keys = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 'rows']
section2_keys = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
config = SafeConfigParser()
for section1 in conf_sections1:
config.add_section(section1)
for the_key in section1_keys:
config.set(section1, str(the_key), (str(True)))
for section2 in conf_sections2:
config.add_section(section2)
for the_key in section2_keys:
config.set(section2, str(the_key), (str(100)))
with open(conf_file, 'w') as f:
config.write(f)
if os.path.isfile(conf_file):
pass
else:
c_file = open(conf_file, 'a')
c_file.close()
set_new_config_file()
def load_media_to_library(self):
"""
Load all media entries from media lib to memory [list(My_Library)]
:param self: calling Object
:return: None
"""
media_list = xmmsfun.xmms_get_all_library_list()
self.tableMediaLibrary.setSortingEnabled(False)
for item in media_list.value():
track = clean_track(item)
self.My_Library.append(track)
add_row_to_table(self.tableMediaLibrary, track)
self.tableMediaLibrary.setSortingEnabled(True)
def load_play_lists(self):
"""
Load all play lists to memory [list(Play_Lists)]
:param self:
:return: None
"""
all_pls = []
pls = xmmsfun.xmms_get_list_of_play_lists()
for pl in pls.value():
if pl.startswith("_") or pl == "Default":
pass
else:
all_pls.append(pl)
for item in sorted(all_pls, key=lambda s: s.lower()):
self.combo_pl_names.addItem(item)
pl_ids = xmmsfun.xmms_get_playlist_entries(item)
if pl_ids is not False:
self.Play_Lists.append({"Name": item, "Ids": pl_ids.value()})
load_pl_entries_table(self)
def load_pl_entries_table(self):
"""
Load the currently selected playlist to playlist table
:param self:
:return: None
"""
pl = self.combo_pl_names.currentText()
self.table_pl_entries.setRowCount(0)
if pl is None or pl is False or pl == "":
return False
ml_ids = get_playlist_entries_from_mem(self, pl)
if ml_ids is not False:
for this_id in ml_ids:
track = get_info_by_ml_id(self, this_id)
add_row_to_table(self.table_pl_entries, track)
def load_now_playing(self):
"""
Load current playlist to Now Playing table
:param self:
:return: None
"""
npl = xmmsfun.xmms_get_now_playing_entries()
for np in npl.value():
track = get_info_by_ml_id(self, np)
add_row_to_table(self.tableNowPlaying, track)
current_row = xmmsfun.xmms_get_now_playing_pl_id()
if current_row is not False and current_row.value()['position'] != -1:
highlight_row(self, current_row.value()['position'])
def insert_row_to_table(row, table, data):
"""
Insert given row to given table with given data
:param row: int
:param table: Object
:param data: item from [list(My_Library]
:return: None
"""
table.insertRow(row)
set_row_to_table(row, table, data)
def set_row_to_table(row, table, data):
"""
Set data at given row in given table using given data
:param row: int
:param table: Object
:param data: item from [list(My_Library)]
:return: None
"""
table.set_row_data(row, data['id'], data['tracknr'], data['album'], data['partofset'], data['title'],
data['artist'], data['genre'], data['performer'],
data['duration'], data['timesplayed'], data['bitrate'], data['filesize'])
def add_row_to_table(table, data):
"""
Add row to end of given table with given data
:param table: Object
:param data: item from [list(My_Library)]
:return: None
"""
row = table.rowCount()
insert_row_to_table(row, table, data)
def get_playlist_entries_from_mem(self, plist):
"""
Get details from memory [list(Play_Lists)] for given playlist
:param self:
:param plist: str
:return: list of media lib id's of False on error
"""
if plist is None or plist is False:
return False
my_item = next((item for item in self.Play_Lists if item['Name'] == plist), None)
if my_item is None:
return False
else:
return my_item['Ids']
def get_info_by_ml_id(self, ml_id):
"""
Get track information for given media lib id from memory [list(My_Library)]
:param self:
:param ml_id: int
:return: item from [list(My_Library)] or False on error
"""
my_item = next((item for item in self.My_Library if item['id'] == ml_id), None)
if my_item is None:
return False
return my_item
def clean_track(track):
"""
Clean all track variables for insertion to memory/table from given dict
:param track: dict
:return: dict
"""
try:
ml_id = track['id']
except AttributeError:
ml_id = "Unknown"
try:
if track['partofset'] is None:
pos = 0
else:
pos = track['partofset']
except AttributeError:
pos = 0
except KeyError:
pos = 0
try:
if track['tracknr'] is None:
tracknr = 0
else:
tracknr = track['tracknr']
except AttributeError:
tracknr = 0
except KeyError:
tracknr = 0
try:
if track['title'] is None:
title = "Unknown"
else:
title = track['title']
except AttributeError:
title = "Unknown"
except KeyError:
title = "Unknown"
try:
if track['artist'] is None:
artist = "Unknown"
else:
artist = track['artist']
except AttributeError:
artist = "Unknown"
except KeyError:
artist = "Unknown"
try:
if track['album'] is None:
album = "Unknown"
else:
album = track['album']
except AttributeError:
album = "Unknown"
except KeyError:
album = "Unknown"
try:
if track['genre'] is None:
genre = "Unknown"
else:
genre = track['genre']
except AttributeError:
genre = "Unknown"
except KeyError:
genre = "Unknown"
try:
if track['bitrate'] is None:
bitrate = "Unknown"
else:
bitrate = track['bitrate']
except AttributeError:
bitrate = "Unknown"
except KeyError:
bitrate = "Unknown"
try:
if track['performer'] is None:
album_artist = "Unknown"
else:
album_artist = track['performer']
except AttributeError:
album_artist = "Unknown"
except KeyError:
album_artist = "Unknown"
try:
if track['duration'] is None:
duration = 0
else:
duration = track['duration']
except AttributeError:
duration = 0
except KeyError:
duration = 0
try:
if track['timesplayed'] is None:
times_played = "Unknown"
else:
times_played = track['timesplayed']
except AttributeError:
times_played = "Unknown"
except KeyError:
times_played = "Unknown"
try:
if track['size'] is None:
file_size = "Unknown"
else:
file_size = track['size']
except AttributeError:
file_size = "Unknown"
except KeyError:
file_size = "Unknown"
new_track = {'id': ml_id, 'title': title, 'artist': artist, 'album': album, 'genre': genre, 'bitrate': bitrate,
'performer': album_artist, 'duration': duration, 'timesplayed': times_played, 'filesize': file_size,
"tracknr": tracknr, "partofset": pos}
return new_track
# noinspection SpellCheckingInspection
def clean_track_result(track):
"""
Clean all track variables for insertion to memory/table from given XmmsResult
:param track: XmmsResult
:return: dict
"""
try:
ml_id = [track.value()[key] for key in track.value().keys() if key[1] == 'id'][0]
except AttributeError:
ml_id = "Unknown"
except IndexError:
ml_id = "Unknown"
try:
if [track.value()[key] for key in track.value().keys() if key[1] == 'partofset'][0] is None:
pos = 0
else:
pos = [track.value()[key] for key in track.value().keys() if key[1] == 'partofset'][0]
except IndexError:
pos = 0
except AttributeError:
pos = 0
except KeyError:
pos = 0
try:
if [track.value()[key] for key in track.value().keys() if key[1] == 'tracknr'][0] is None:
tracknr = 0
else:
tracknr = [track.value()[key] for key in track.value().keys() if key[1] == 'tracknr'][0]
except IndexError:
tracknr = 0
except AttributeError:
tracknr = 0
except KeyError:
tracknr = 0
try:
if [track.value()[key] for key in track.value().keys() if key[1] == 'title'][0] is None:
title = "Unknown"
else:
title = [track.value()[key] for key in track.value().keys() if key[1] == 'title'][0]
except IndexError:
title = "Unknown"
except AttributeError:
title = "Unknown"
except KeyError:
title = "Unknown"
try:
if [track.value()[key] for key in track.value().keys() if key[1] == 'artist'][0] is None:
artist = "Unknown"
else:
artist = [track.value()[key] for key in track.value().keys() if key[1] == 'artist'][0]
except IndexError:
artist = "Unknown"
except AttributeError:
artist = "Unknown"
except KeyError:
artist = "Unknown"
try:
if [track.value()[key] for key in track.value().keys() if key[1] == 'album'][0] is None:
album = "Unknown"
else:
album = [track.value()[key] for key in track.value().keys() if key[1] == 'album'][0]
except IndexError:
album = "Unknown"
except AttributeError:
album = "Unknown"
except KeyError:
album = "Unknown"
try:
if [track.value()[key] for key in track.value().keys() if key[1] == 'genre'][0] is None:
genre = "Unknown"
else:
genre = [track.value()[key] for key in track.value().keys() if key[1] == 'genre'][0]
except IndexError:
genre = "Unknown"
except AttributeError:
genre = "Unknown"
except KeyError:
genre = "Unknown"
try:
if [track.value()[key] for key in track.value().keys() if key[1] == 'bitrate'][0] is None:
bitrate = "Unknown"
else:
bitrate = [track.value()[key] for key in track.value().keys() if key[1] == 'bitrate'][0]
except IndexError:
bitrate = "Unknown"
except AttributeError:
bitrate = "Unknown"
except KeyError:
bitrate = "Unknown"
try:
if [track.value()[key] for key in track.value().keys() if key[1] == 'performer'][0] is None:
album_artist = "Unknown"
else:
album_artist = [track.value()[key] for key in track.value().keys() if key[1] == 'performer'][0]
except IndexError:
album_artist = "Unknown"
except AttributeError:
album_artist = "Unknown"
except KeyError:
album_artist = "Unknown"
try:
if [track.value()[key] for key in track.value().keys() if key[1] == 'duration'][0] is None:
duration = 0
else:
duration = [track.value()[key] for key in track.value().keys() if key[1] == 'duration'][0]
except IndexError:
duration = 0
except AttributeError:
duration = 0
except KeyError:
duration = 0
try:
if [track.value()[key] for key in track.value().keys() if key[1] == 'timesplayed'][0] is None:
times_played = "Unknown"
else:
times_played = [track.value()[key] for key in track.value().keys() if key[1] == 'timesplayed'][0]
except IndexError:
times_played = "Unknown"
except AttributeError:
times_played = "Unknown"
except KeyError:
times_played = "Unknown"
try:
if [track.value()[key] for key in track.value().keys() if key[1] == 'size'][0] is None:
file_size = "Unknown"
else:
file_size = [track.value()[key] for key in track.value().keys() if key[1] == 'size'][0]
except IndexError:
file_size = "Unknown"
except AttributeError:
file_size = "Unknown"
except KeyError:
file_size = "Unknown"
new_track = {'id': ml_id, 'title': title, 'artist': artist, 'album': album, 'genre': genre, 'bitrate': bitrate,
'performer': album_artist, 'duration': duration, 'timesplayed': times_played, 'filesize': file_size,
"tracknr": tracknr, "partofset": pos}
return new_track
def ms_int_to_time(ms):
"""
Convert time in milliseconds to tuple (minutes, seconds)
:param ms: int
:return: tuple(minutes, seconds)
"""
minutes = ms / 60000
seconds = (ms % 60000)/1000
return "%02i:%02i" % (minutes, seconds)
def convert_to_size(size):
"""
Convert bytes number to Kb/Mb/Gb values
:param size: int
:return: formatted string (??.??kb/Mb/Gb)
"""
if size == 0:
return '0b'
size_name = ("b", "Kb", "Mb", "Gb")
i = int(math.floor(math.log(size, 1024)))
p = math.pow(1024, i)
s = round(size/p, 2)
return '%s %s' % (s, size_name[i])
def playlist_position_change(self, result):
"""
react to broadcast playlist position changes
highlight current row in now playing table
:param self:
:param result: XmmsResult Object
:return: bool
"""
if result.value()['name'] == "Default":
if result.value()['position'] < 0:
if xmmsfun.xmms_next() is False:
xmmsfun.xmms_stop()
return False
else:
highlight_row(self, result.value()['position']+1)
return True
else:
highlight_row(self, result.value()['position'])
return True
else:
return False
def highlight_current(self):
"""
set row to be highlighted from xmms
:param self:
:return: None
"""
pl_row = xmmsfun.xmms_get_now_playing_pl_id()
highlight_row(self, pl_row.value()['position'])
def highlight_row(self, this_row):
"""
highlight given row after removing any previous highlights
:param self:
:param this_row: int
:return: bool
"""
all_rows = self.tableNowPlaying.rowCount()
for row in range(0, all_rows):
self.tableNowPlaying.item(row, 0).setBackground(QColor(255, 255, 255))
self.tableNowPlaying.item(row, 1).setBackground(QColor(255, 255, 255))
self.tableNowPlaying.item(row, 2).setBackground(QColor(255, 255, 255))
self.tableNowPlaying.item(row, 3).setBackground(QColor(255, 255, 255))
self.tableNowPlaying.item(row, 4).setBackground(QColor(255, 255, 255))
self.tableNowPlaying.item(row, 5).setBackground(QColor(255, 255, 255))
self.tableNowPlaying.item(row, 6).setBackground(QColor(255, 255, 255))
self.tableNowPlaying.item(row, 7).setBackground(QColor(255, 255, 255))
self.tableNowPlaying.item(row, 8).setBackground(QColor(255, 255, 255))
self.tableNowPlaying.item(row, 9).setBackground(QColor(255, 255, 255))
self.tableNowPlaying.item(row, 10).setBackground(QColor(255, 255, 255))
self.tableNowPlaying.item(row, 11).setBackground(QColor(255, 255, 255))
if this_row == -1:
return False
try:
self.tableNowPlaying.item(this_row, 0).setBackground(QColor(47, 127, 255))
self.tableNowPlaying.item(this_row, 1).setBackground(QColor(47, 127, 255))
self.tableNowPlaying.item(this_row, 2).setBackground(QColor(47, 127, 255))
self.tableNowPlaying.item(this_row, 3).setBackground(QColor(47, 127, 255))
self.tableNowPlaying.item(this_row, 4).setBackground(QColor(47, 127, 255))
self.tableNowPlaying.item(this_row, 5).setBackground(QColor(47, 127, 255))
self.tableNowPlaying.item(this_row, 6).setBackground(QColor(47, 127, 255))
self.tableNowPlaying.item(this_row, 7).setBackground(QColor(47, 127, 255))
self.tableNowPlaying.item(this_row, 8).setBackground(QColor(47, 127, 255))
self.tableNowPlaying.item(this_row, 9).setBackground(QColor(47, 127, 255))
self.tableNowPlaying.item(this_row, 10).setBackground(QColor(47, 127, 255))
self.tableNowPlaying.item(this_row, 11).setBackground(QColor(47, 127, 255))
return True
except AttributeError:
return False
def playlist_changed(self, result):
"""
React to playlist changes, updating library, play lists and now playing tables
:param self:
:param result: XmmsResult Object
:return: None
"""
if result.value()["type"] == 0:
if result.value()['name'] == "Default":
insert_row_to_table(result.value()['position'], self.tableNowPlaying,
get_info_by_ml_id(self, result.value()['id']))
elif result.value()['name'] == self.combo_pl_names.currentText():
insert_row_to_table(result.value()['position'], self.table_pl_entries,
get_info_by_ml_id(self, result.value()['id']))
add_ml_id_to_playlists(self, result.value()['id'], result.value()['name'])
else:
add_ml_id_to_playlists(self, result.value()['id'], result.value()['name'])
elif result.value()["type"] == 1:
if result.value()['name'] == "Default":
insert_row_to_table(result.value()['position'], self.tableNowPlaying,
get_info_by_ml_id(self, result.value()['id']))
elif result.value()['name'] == self.combo_pl_names.currentText():
insert_row_to_table(result.value()['position'], self.table_pl_entries,
get_info_by_ml_id(self, result.value()['id']))
add_ml_id_to_playlists(self, result.value()['id'], result.value()['name'])
else:
add_ml_id_to_playlists(self, result.value()['id'], result.value()['name'])
elif result.value()["type"] == 2:
if result.value()['name'] == "Default":
self.tableNowPlaying.setRowCount(0)
load_now_playing(self)
elif result.value()['name'] == self.combo_pl_names.currentText():
self.table_pl_entries.setRowCount(0)
refresh_playlists(self, result.value()['name'])
load_pl_entries_table(self)
else:
this_list = self.combo_pl_names.currentText()
refresh_playlists(self, result.value()['name'])
self.combo_pl_names.setCurrentIndex(self.combo_pl_names.findText(this_list))
elif result.value()["type"] == 3:
if result.value()['name'] == "Default":
self.tableNowPlaying.removeRow(result.value()['position'])
elif result.value()['name'] == self.combo_pl_names.currentText():
self.table_pl_entries.removeRow(result.value()['position'])
remove_position_from_playlists(self, result.value()['position'], result.value()['name'])
else:
remove_position_from_playlists(self, result.value()['position'], result.value()['name'])
elif result.value()["type"] == 4:
if result.value()['name'] == "Default":
self.tableNowPlaying.setRowCount(0)
if xmmsfun.xmms_get_play_status().value() == 1:
xmmsfun.xmms_stop()
elif result.value()['name'] == self.combo_pl_names.currentText():
self.table_pl_entries.setRowCount(0)
clear_playlist(self, result.value()['name'])
else:
clear_playlist(self, result.value()['name'])
elif result.value()["type"] == 5:
if result.value()['name'] == "Default":
self.tableNowPlaying.removeRow(result.value()['position'])
insert_row_to_table(result.value()['newposition'], self.tableNowPlaying,
get_info_by_ml_id(self, result.value()['id']))
elif result.value()['name'] == self.combo_pl_names.currentText():
self.table_pl_entries.removeRow(result.value()['position'])
insert_row_to_table(result.value()['newposition'], self.table_pl_entries,
get_info_by_ml_id(self, result.value()['id']))
move_position_in_playlists(self, result.value()['name'], result.value()['position'],
result.value()['newposition'])
else:
move_position_in_playlists(self, result.value()['name'], result.value()['position'],
result.value()['newposition'])
elif result.value()["type"] == 6:
if result.value()['name'] == "Default":
self.tableNowPlaying.setRowCount(0)
load_now_playing(self)
elif result.value()['name'] == self.combo_pl_names.currentText():
self.table_pl_entries.setRowCount(0)
refresh_playlists(self, result.value()['name'])
load_pl_entries_table(self)
else:
this_list = self.combo_pl_names.currentText()
refresh_playlists(self, result.value()['name'])
self.combo_pl_names.setCurrentIndex(self.combo_pl_names.findText(this_list))
elif result.value()["type"] == 7:
pass
def collection_changed(self, result):
"""
React to broadcast of collection changes
:param self:
:param result: XmmsResult
:return: None
"""
if result.value()['namespace'] == "Playlists":
if result.value()['type'] == 0:
add_new_play_list_to_playlists(self, result.value()['name'])
this_pl = self.combo_pl_names.currentText()
refresh_playlist_combo(self)
self.combo_pl_names.setCurrentIndex(self.combo_pl_names.findText(this_pl))
elif result.value()['type'] == 1:
pass
elif result.value()['type'] == 2:
rename_playlist(self, result.value()['name'], result.value()['newname'])
refresh_playlist_combo(self)
elif result.value()['type'] == 3:
self.Play_Lists[:] = [d for d in self.Play_Lists if d.get("Name") != result.value()['name']]
refresh_playlist_combo(self)
else:
print("Collection changed but result is out of bounds")
def set_play_status(self):
"""
Get play status and pass it to play_status_control
:param self:
:return: None
"""
status = xmmsfun.xmms_get_play_status()
if status is not False:
play_status_control(self, status)
pass
def play_status_control(self, status):
"""
Set play buttons according to play status
:param self:
:param status: int
:return: None
"""
if status.value() == 0:
self.toolStop.setDisabled(True)
self.toolPause.hide()
self.toolPlay.show()
self.setWindowTitle("Dai's Xmms2 Client Version " + self.VERSION)
try:
set_pb(self, 0)
except AttributeError:
pass
elif status.value() == 1:
self.toolStop.setDisabled(False)
self.toolPause.show()
self.toolPlay.hide()
elif status.value() == 2:
self.toolStop.setDisabled(False)
self.toolPause.hide()
self.toolPlay.show()
else:
print(" Play Status Out Of Range")
def first_set_now_stuff(self):
"""
get current track from xmms and assign track information to Now_Playing variable and set window title to track
information
:param self:
:return: None
"""
ml_id = xmmsfun.xmms_get_now_playing_ml_id()
if ml_id is None or ml_id == 0 or ml_id is False:
return
else:
track = get_info_by_ml_id(self, ml_id)
if track is not False:
self.Now_Playing = track
self.setWindowTitle(self.Now_Playing['title'] + " By : " + self.Now_Playing['artist'] +
" From the Album : " + self.Now_Playing['album'])
def set_now_stuff_from_broadcast(self, data):
"""
Update Now_Playing variable and window title from broadcast information
:param self:
:param data: XmmsResult Object
:return: None
"""
track = get_info_by_ml_id(self, data.value())
if track is None or track is False:
return
self.Now_Playing = track
self.setWindowTitle(self.Now_Playing['title'] + " By : " + self.Now_Playing['artist'] +
" From the Album : " + self.Now_Playing['album'])
def set_pb(self, ms):
"""
Set progress bar
:param self:
:param ms: XmmsValue or int
:return: None
"""
# noinspection PyUnresolvedReferences
if isinstance(ms, xmmsvalue.XmmsValue):
milli_secs = ms.value()
else:
milli_secs = ms
if self.Now_Playing == "":
return
tot_time = ms_int_to_time(self.Now_Playing["duration"])
self.progressBar.setFormat(str("%02d:%02d" % (milli_secs/60000, (milli_secs/1000) % 60)) + " / " + str(tot_time))
try:
pc = self.Now_Playing["duration"]/100
this_pc = milli_secs / float(pc)
self.progressBar.setValue(this_pc)
except ZeroDivisionError:
return
# Playlist methods
def remove_position_from_playlists(self, pos, play_list):
"""
Remove media lib id from play list [Play_Lists] by position in list
:param self:
:param pos: int
:param play_list: string
:return: bool
"""
if is_in_playlists(self, play_list):
plist = next((item for item in self.Play_Lists if item['Name'] == play_list), None)
del plist['Ids'][pos]
return True
else:
return False
def add_ml_id_to_playlists(self, ml_id, play_list):
"""
Add media lib id to end of play list [Play_Lists]
:param self:
:param ml_id: int
:param play_list: string
:return: bool
"""
if is_in_playlists(self, play_list):
plist = next((item for item in self.Play_Lists if item['Name'] == play_list), None)
if ml_id not in plist['Ids']:
plist['Ids'].append(ml_id)
return True
else:
return False
def is_in_playlists(self, pl_name):
"""
check if play list is in the play list [Play_Lists]
:param self:
:param pl_name: string
:return: bool
"""
for track in self.Play_Lists:
if track['Name'] == pl_name:
return True
return False
def move_position_in_playlists(self, play_list, old, new):
"""
move media lib id in play list [Play_Lists]
:param self:
:param play_list: string
:param old: int
:param new: int
:return: bool
"""
if is_in_playlists(self, play_list):
plist = next((item for item in self.Play_Lists if item['Name'] == play_list), None)
plist['Ids'].insert(new, plist['Ids'].pop(old))
return True
else:
return False
def add_new_play_list_to_playlists(self, pl_name):
"""
Add play list name with empty list to play lists [Play_Lists]
:param self:
:param pl_name: string
:return: None
"""
self.Play_Lists.append({"Name": pl_name, "Ids": []})
def refresh_playlists(self, play_list):
"""
refresh play list table contents from xmms
:param self:
:param play_list: string
:return: None
"""
self.Play_Lists[:] = [d for d in self.Play_Lists if d.get("Name") != play_list]
pl_ids = xmmsfun.xmms_get_playlist_entries(play_list)
if pl_ids is not False:
self.Play_Lists.append({"Name": play_list, "Ids": pl_ids.value()})
index = self.combo_pl_names.findText(play_list)
self.combo_pl_names.setCurrentIndex(index)
def clear_playlist(self, play_list):
"""
clear given playlist in play lists [Play_Lists]
:param self:
:param play_list: string
:return: None
"""
self.Play_Lists[:] = [d for d in self.Play_Lists if d.get("Name") != play_list]
self.Play_Lists.append({"Name": play_list, "Ids": []})
def refresh_playlist_combo(self):
"""
Reload playlist combo box from play lists [Play_Lists]
:param self:
:return: None
"""
all_pls = []
self.combo_pl_names.clear()
for pl in self.Play_Lists:
if pl['Name'].startswith("_") or pl['Name'] == "Default":
pass
else:
all_pls.append(pl['Name'])
for item in sorted(all_pls, key=lambda s: s.lower()):
self.combo_pl_names.addItem(item)
def rename_playlist(self, old_name, new_name):
"""
rename given playlist in play lists [Play_Lists]
:param self:
:param old_name: string
:param new_name: string
:return: None
"""
for plist in self.Play_Lists:
if plist['Name'] == old_name:
plist['Name'] = new_name
def play_list_now(self):
"""
Clear Default play list and Add currently selected play list (combo_pl_names) to now playing and start playback
:param self:
:return: None
"""
xmmsfun.xmms_clear_playlist_tracks("Default")
this_pl = self.combo_pl_names.currentText()
load_this_playlist(self, this_pl)
self.tableNowPlaying.resizeColumnsToContents()
xmmsfun.xmms_play()
def add_play_list_to_now(self):
"""
Add currently selected play list (combo_pl_names) to now playing
:param self:
:return:
"""
this_pl = self.combo_pl_names.currentText()
load_this_playlist(self, this_pl)
self.tableNowPlaying.resizeColumnsToContents()
def load_this_playlist(self, plist):
"""
Add playlist to end of now playing (Default)
:param self:
:param plist: string
:return: None
"""
this_list = get_playlist_entries_from_mem(self, plist)
for track in this_list:
xmmsfun.xmms_add_ml_id_to_playlist(track, 'Default')
def add_id_list_to_now_playing(selection):
"""
Add list of media lib ids to now playing (Default)
:param selection: list of ints
:return: None
"""
if selection:
for ml_id in selection:
xmmsfun.xmms_add_ml_id_to_playlist(ml_id, "Default")
# Media Library methods
def set_table_with_new_values(row, table, track):
"""
Set given row in given table with given track info
:param row: int
:param table: table Object
:param track: dict
:return: None
"""
table.item(row, 0).setText(str(track['id']))
table.item(row, 1).setText(str(track['tracknr']))
table.item(row, 2).setText(str(track['album']))
table.item(row, 3).setText(str(track['partofset']))
table.item(row, 4).setText(str(track['title']))
table.item(row, 5).setText(str(track['artist']))
table.item(row, 6).setText(str(track['genre']))
table.item(row, 7).setText(str(track['performer']))
table.item(row, 8).setText(str(ms_int_to_time(track['duration'])))
table.item(row, 9).setText(str(track['timesplayed']))
table.item(row, 10).setText(str(track['bitrate']))
table.item(row, 11).setText(str(convert_to_size(track['filesize'])))
def update_ml_id_with_track(self, track):
"""
Update all tables with given track information
:param self:
:param track: dict
:return: None
"""
self.My_Library[:] = [d for d in self.My_Library if d.get('id') != track['id']]
self.My_Library.append(track)
np_rows = find_rows_in_table(self.tableNowPlaying, track['id'])
if len(np_rows) > 0:
for this_row in np_rows:
set_table_with_new_values(this_row, self.tableNowPlaying, track)
pl_rows = find_rows_in_table(self.table_pl_entries, track['id'])
if len(pl_rows) > 0:
for this_row in pl_rows:
set_table_with_new_values(this_row, self.table_pl_entries, track)
ml_row = find_row_in_table(self.tableMediaLibrary, track['id'])
if ml_row is not False:
set_table_with_new_values(ml_row, self.tableMediaLibrary, track)
def update_ml_id(self, ml_id):
"""
Get new information from xmms and update Library table
:param self:
:param ml_id: int
:return: bool
"""
cl_track = clean_track_result(xmmsfun.xmms_get_media_lib_info_by_ml_id(ml_id))