This repository has been archived by the owner on Nov 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_from_musicbrainz_gui.py
executable file
·1393 lines (1308 loc) · 48 KB
/
update_from_musicbrainz_gui.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/env python3
# -*- coding: utf-8 -*-
"""
update_from_musicbrainz_gui.py
Update metadata from a MusicBrainz release
(Tkinter-based GUI assistant supporting Nautilus script integration)
"""
import argparse
import logging
import os
import pathlib
# import re
import sys
import tkinter
import webbrowser
from tkinter import filedialog
from tkinter import messagebox
from tkinter import ttk
# local modules
import gui_commons
import mbdata
import safer_mass_rename
#
# Constants
#
# Phases
CHOOSE_LOCAL_RELEASE = "choose_local_release"
LOCAL_RELEASE_DATA = "local_release_data"
SELECT_MB_RELEASE = "select_mb_release"
CONFIRM_TRANSLATIONS = "confirm_translations"
CONFIRM_METADATA = "confirm_metadata"
RENAME_OPTIONS = "rename_options"
CONFIRM_RENAME = "confirm_rename"
RENAME_FILES = "rename_files"
PHASES = (
CHOOSE_LOCAL_RELEASE,
LOCAL_RELEASE_DATA,
SELECT_MB_RELEASE,
CONFIRM_TRANSLATIONS,
CONFIRM_METADATA,
RENAME_OPTIONS,
CONFIRM_RENAME,
RENAME_FILES,
)
PANEL_NAMES = {
LOCAL_RELEASE_DATA: "Review / change local release data",
SELECT_MB_RELEASE: "Select the matching release from MusicBrainz",
CONFIRM_TRANSLATIONS: "Confirm typograpy fixes in data from MusicBrainz",
CONFIRM_METADATA: "Confirm local Metadata changes",
RENAME_OPTIONS: "View local metadata change result /"
" options for renaming files",
CONFIRM_RENAME: "Confirm files renaming",
RENAME_FILES: "View files renaming results",
}
# MusicBrainz metadata replacements
TYPOGRAPHY_FIXES = dict(
single_quotes=mbdata.RegexTranslator(
r"(?!<\w)'(.+?)'(?!\w)",
r"‘\1’",
description="Neutral single quotes around text"
" → typographic quotes ('abc' → ‘abc’;"
" might misinterpret “Rock 'n' Roll”)",
),
double_quotes=mbdata.RegexTranslator(
r'(?!<\w)"(.+?)"(?!\w)',
r"“\1”",
description="Neutral double quotes around text"
' → typographic quotes ("xyz" → “xyz”)',
),
quotes_as_inch=mbdata.RegexTranslator(
r'(?!<\w)(7|10|12)"',
"\\1\u2033",
description="Neutral double quote directly after 7, 10 or 12"
' → inch sign (7" → 7″, 10" → 10″, 12" → 12″)',
),
three_or_more_dots=mbdata.RegexTranslator(
r"\.{3,}",
"\u2026",
description="Three or more consecutive dots"
" → horizontal ellipsis (eg. ..... → …)",
),
exactly_three_dots=mbdata.Translator(
"...",
"\u2026",
description="Exactly three consecutive dots"
" → horizontal ellipsis (... → …)",
),
apostrophe=mbdata.Translator(
"'",
"\u2019",
description="Typewriter apostrophe"
" → typesetter’s apostrophe (' → ’)",
),
)
#
# Helper Functions
#
def change_treeview_item_text(treeview, iid=None, text=None, select=True):
"""Change the text of a ttk.Treeview item
by removing and reattaching it with the new text
"""
parent_iid = treeview.parent(iid)
current_index = treeview.index(iid)
treeview.delete(iid)
treeview.insert(parent_iid, current_index, iid=iid, text=text)
if select:
treeview.focus(iid)
treeview.selection_set(iid)
#
def open_in_musicbrainz(release_id):
"""Open the webbrowser and show a release in MusicBrainz"""
webbrowser.open(mbdata.FS_RELEASE_URL % mbdata.extract_id(release_id))
#
# Classes
#
class Namespace(dict):
# pylint: disable=too-many-instance-attributes
"""A dict subclass that exposes its items as attributes.
Warning: Namespace instances only have direct access to the
attributes defined in the visible_attributes tuple
"""
visible_attributes = ("items",)
def __repr__(self):
"""Object representation"""
return "{0}({1})".format(type(self).__name__, super().__repr__())
def __dir__(self):
"""Members sequence"""
return tuple(self)
def __getattribute__(self, name):
"""Access a visible attribute
or return an existing dict member
"""
if name in type(self).visible_attributes:
return object.__getattribute__(self, name)
#
try:
return self[name]
except KeyError as error:
raise AttributeError(
"{0!r} object has no attribute {1!r}".format(
type(self).__name__, name
)
) from error
#
def __setattr__(self, name, value):
"""Set an attribute"""
self[name] = value
def __delattr__(self, name):
"""Delete an attribute"""
del self[name]
class UserInterface(gui_commons.UserInterface):
"""GUI using tkinter"""
script_name = "Update from MusicBrainz GUI"
window_title = "musicbrain: Update metadata from a MusicBrainz release"
with_border = dict(borderwidth=2, padx=5, pady=5, relief=tkinter.GROOVE)
grid_fullwidth = dict(padx=4, pady=2, sticky=tkinter.E + tkinter.W)
# pylint: disable=attribute-defined-outside-init
def __init__(self, directory_path):
"""Build the GUI"""
super().__init__()
# FIXME: hard-coded HiDPI scaling // does not change ttk widgets?
self.main_window.tk.call("tk", "scaling", 2.0)
mbdata.set_useragent(
self.script_name, self.version, contact=gui_commons.HOMEPAGE
)
self.variables = Namespace(
mbid_entry=tkinter.StringVar(),
album=tkinter.StringVar(),
albumartist=tkinter.StringVar(),
release_id=tkinter.StringVar(),
local_release=None,
current_phase=CHOOSE_LOCAL_RELEASE,
current_panel=None,
directory_path=directory_path,
directory_display=tkinter.StringVar(),
disable_next_button=False,
errors=[],
mb_releases=[],
selected_mb_release=None,
metadata_changes={},
metadata_lookup={},
metadata_translations={},
panel_display=tkinter.StringVar(),
changed_tracks={},
ignore_mb_data=tkinter.IntVar(),
# fix_typography=tkinter.IntVar(),
always_include_artist=tkinter.IntVar(),
include_medium=tkinter.IntVar(),
rename_result=None,
typography_fixes=[
("single_quotes", tkinter.IntVar(value=0)),
("apostrophe", tkinter.IntVar(value=1)),
("quotes_as_inch", tkinter.IntVar(value=1)),
("double_quotes", tkinter.IntVar(value=1)),
("three_or_more_dots", tkinter.IntVar(value=0)),
("exactly_three_dots", tkinter.IntVar(value=1)),
],
)
self.widgets = Namespace(
action_area=None,
buttons_area=None,
metadata_view=None,
releases_view=None,
result_view=None,
scroll_vertical=None,
typography_fixes={},
)
overview_frame = tkinter.Frame(self.main_window)
directory_label = tkinter.Label(overview_frame, text="Directory:")
directory_label.grid(padx=4, pady=2, row=0, column=0, sticky=tkinter.W)
selected_directory = tkinter.Entry(
overview_frame,
width=60,
state=tkinter.DISABLED,
textvariable=self.variables.directory_display,
)
selected_directory.grid(
padx=4, pady=2, row=0, column=1, sticky=tkinter.W
)
choose_button = tkinter.Button(
overview_frame,
text="Choose another …",
command=self.do_choose_local_release,
)
choose_button.grid(padx=4, pady=4, row=0, column=2, sticky=tkinter.W)
panel_display = tkinter.Label(
overview_frame,
textvariable=self.variables.panel_display,
justify=tkinter.LEFT,
)
panel_display.grid(
padx=4, pady=4, row=1, column=0, columnspan=3, sticky=tkinter.W
)
overview_frame.grid(**self.grid_fullwidth)
self.do_choose_local_release(
keep_existing=True, quit_on_empty_choice=True
)
self.main_window.mainloop()
def do_choose_local_release(
self, keep_existing=False, preset_path=None, quit_on_empty_choice=False
):
"""Choose a release via file dialog"""
if preset_path:
if not preset_path.is_dir():
preset_path = preset_path.parent
#
else:
preset_path = self.variables.directory_path
#
while True:
if not keep_existing or self.variables.directory_path is None:
selected_directory = filedialog.askdirectory(
initialdir=str(preset_path) or os.getcwd()
)
if not selected_directory:
if quit_on_empty_choice:
self.quit()
#
return
#
self.variables.directory_path = pathlib.Path(
selected_directory
)
#
try:
self.variables.local_release = mbdata.local_release_from_path(
self.variables.directory_path
)
except ValueError as error:
messagebox.showerror(
"Error while reading release",
str(error),
icon=messagebox.ERROR,
)
keep_existing = False
preset_path = self.variables.directory_path
continue
#
total_number_of_tracks = sum(
medium.counted_tracks
for medium in self.variables.local_release.media_list
)
self.variables.directory_display.set(
"%s (%s tracks)"
% (self.variables.directory_path.name, total_number_of_tracks)
)
self.variables.mbid_entry.set("")
self.variables.current_panel = CHOOSE_LOCAL_RELEASE
self.next_panel()
break
#
def do_local_release_data(self):
"""Set local release data"""
self.variables.album.set(self.variables.local_release.album or "")
self.variables.albumartist.set(
self.variables.local_release.albumartist or ""
)
def do_select_mb_release(self):
"""Lookup releases in MusicBrainz"""
self.variables.mb_releases.clear()
mbid_value = self.variables.mbid_entry.get()
if mbid_value:
try:
release_mbid = mbdata.extract_id(mbid_value)
except ValueError:
self.variables.errors.append(
"%r does not contain a valid"
" MusicBrainz ID." % mbid_value
)
else:
try:
self.variables.selected_mb_release = (
mbdata.release_from_id(
release_mbid,
local_release=self.variables.local_release,
)
)
except ValueError as error:
self.variables.errors.append(str(error))
else:
self.variables.mb_releases.append(
self.variables.selected_mb_release
)
#
#
else:
# Get releases from musicbrainz
try:
self.variables.mb_releases.extend(
sorted(
mbdata.releases_from_search(
album=self.variables.album.get(),
albumartist=self.variables.albumartist.get(),
local_release=self.variables.local_release,
),
reverse=True,
)
)
except ValueError as error:
self.variables.errors.append(str(error))
#
if not self.variables.mb_releases:
self.variables.errors.append("No matching releases found.")
#
#
def do_confirm_translations(self):
"""Prepare metadata (from MusicBrainz) translations"""
try:
release_mbid = mbdata.extract_id(self.widgets.release_view.focus())
except ValueError:
self.variables.errors.append("No release selected.")
self.variables.disable_next_button = True
return
#
# Fetch data from MB only if they are not here yet
if (
not self.variables.selected_mb_release
or self.variables.selected_mb_release.id_ != release_mbid
):
try:
self.variables.selected_mb_release = mbdata.release_from_id(
release_mbid
)
except ValueError as error:
self.variables.errors.append(str(error))
self.variables.disable_next_button = True
return
#
#
self.variables.ignore_mb_data.set(0)
#
# Translate tag values if typography fixes are required
self.variables.selected_mb_release.clear_translations()
replacements = mbdata.TranslatorChain()
for (fix_name, is_selected) in self.variables.typography_fixes:
if is_selected.get():
replacements.append(TYPOGRAPHY_FIXES[fix_name])
#
#
self.variables.selected_mb_release.translate(replacements)
#
# Directly jump to the next panel if no translations are found
if not self.check_translations():
self.variables.current_panel = CONFIRM_TRANSLATIONS
self.next_action()
#
def check_translations(self):
"""Check translations, return True or False"""
if self.variables.selected_mb_release.translated_accessors:
return True
#
self.variables.errors.append("No typograpy fixes required.")
return False
def do_confirm_metadata(self):
"""Prepare Metadata change"""
# Build map of metadata changes per track
self.variables.metadata_changes.clear()
for track in self.variables.local_release.get_all_tracks():
try:
changes = mbdata.LocalTrackChanges(
track, self.variables.selected_mb_release
)
except (mbdata.MediumNotFound, mbdata.TrackNotFound) as error:
logging.warning(
"Error on track %r: %s", track.file_path.name, error
)
continue
#
if changes:
self.variables.metadata_changes[track.file_path.name] = changes
#
#
self.check_metadata_changes()
def check_metadata_changes(self):
"""Check for metadata changes, return True or False"""
logging.debug("Metadata changes: %r", self.variables.metadata_changes)
if self.variables.metadata_changes:
return True
#
self.variables.errors.append(
"No differences found in metadata.\n"
"Hitting “next” will just fix any wrong tag encodings."
)
return False
def do_rename_options(self):
"""Execute the prepared Metadata change"""
self.variables.changed_tracks.clear()
if self.variables.ignore_mb_data.get():
self.variables.metadata_changes.clear()
#
for track in self.variables.local_release.get_all_tracks():
file_name = track.file_path.name
try:
changes = self.variables.metadata_changes[file_name]
except KeyError:
applied_changes = track.get_saved_changes(
remark="fixed encoding"
)
else:
applied_changes = changes.apply()
#
if applied_changes:
logging.debug(
"Applied changes for %r: %r", file_name, applied_changes
)
self.variables.changed_tracks[file_name] = applied_changes
#
#
if not self.variables.changed_tracks:
self.variables.errors.append("No metadata changes done.")
#
self.variables.always_include_artist.set(False)
self.variables.include_medium.set(
self.variables.local_release.medium_prefixes_required
)
def do_confirm_rename(self):
"""Prepare file mass rename"""
self.variables.renaming_plan = safer_mass_rename.RenamingPlan()
for track in self.variables.local_release.get_all_tracks():
self.variables.renaming_plan.add(
track.file_path,
track.suggested_filename(
include_artist_name=bool(
self.variables.always_include_artist.get()
),
include_medium_number=bool(
self.variables.include_medium.get()
),
),
)
#
if not self.variables.renaming_plan:
self.variables.errors.append("No files need to be renamed.")
self.variables.disable_next_button = True
#
def do_rename_files(self):
"""Execute mass file rename"""
self.variables.rename_result = self.variables.renaming_plan.execute()
def panel_local_release_data(self):
"""Show the local release’s title and artist"""
label_grid = dict(column=0, padx=4, sticky=tkinter.E)
value_grid = dict(column=1, columnspan=3, padx=4, sticky=tkinter.W)
search_frame = tkinter.Frame(
self.widgets.action_area, **self.with_border
)
search_label = tkinter.Label(
search_frame,
text="Search the release in MusicBrainz" " by the following data:",
justify=tkinter.LEFT,
)
search_label.grid(row=0, column=0, columnspan=2, sticky=tkinter.W)
release_label = tkinter.Label(
search_frame, text="Release title:", justify=tkinter.LEFT
)
release_label.grid(row=1, **label_grid)
release_value = tkinter.Entry(
search_frame,
textvariable=self.variables.album,
width=60,
justify=tkinter.LEFT,
)
release_value.grid(row=1, **value_grid)
artist_label = tkinter.Label(
search_frame, text="Release Artist:", justify=tkinter.LEFT
)
artist_label.grid(row=2, **label_grid)
artist_value = tkinter.Entry(
search_frame,
textvariable=self.variables.albumartist,
width=60,
justify=tkinter.LEFT,
)
artist_value.grid(row=2, **value_grid)
search_frame.grid(**self.grid_fullwidth)
direct_entry_frame = tkinter.Frame(
self.widgets.action_area, **self.with_border
)
mbid_label = tkinter.Label(
direct_entry_frame,
text="… or specify a MusicBrainz release directly by its ID:",
justify=tkinter.LEFT,
)
mbid_label.grid(sticky=tkinter.W, padx=4, pady=2)
mbid_value = tkinter.Entry(
direct_entry_frame,
textvariable=self.variables.mbid_entry,
width=70,
justify=tkinter.LEFT,
)
mbid_value.grid(sticky=tkinter.W, padx=4, pady=2)
direct_entry_frame.grid(**self.grid_fullwidth)
def panel_select_mb_release(self):
"""Panel with Musicbrainz release selection"""
if not self.variables.mb_releases:
self.variables.disable_next_button = True
return
#
select_frame = tkinter.Frame(
self.widgets.action_area, **self.with_border
)
release_iids = {}
label = tkinter.Label(
select_frame,
text="Please select a release and hit the “next” button"
" to continue.\n"
"Double-click a release to open its MusicBrainz page"
" in your web browser.",
justify=tkinter.LEFT,
)
label.grid(row=0, column=0, columnspan=2, sticky=tkinter.W)
self.widgets.release_view = ttk.Treeview(
master=select_frame,
height=15,
selectmode=tkinter.BROWSE,
show="tree",
)
self.widgets.release_view.column("#0", width=700)
self.widgets.release_view.bind(
"<Double-Button-1>", self.open_selected_release
)
self.widgets.release_view.bind("<Return>", self.open_selected_release)
for single_release in self.variables.mb_releases:
release_full_name = "%s – %s" % (
single_release[mbdata.ALBUMARTIST],
single_release[mbdata.ALBUM],
)
try:
parent_iid = release_iids[release_full_name.lower()]
except KeyError:
parent_iid = self.widgets.release_view.insert(
"", tkinter.END, open=True, text=release_full_name
)
release_iids[release_full_name.lower()] = parent_iid
#
self.widgets.release_view.insert(
parent_iid,
tkinter.END,
iid=single_release.id_,
text="[%s%%] %s, %s"
% (
single_release.score,
single_release.date or "<unknown date>",
single_release.summary,
),
)
#
# Focus and select the first release
if not self.widgets.release_view.focus():
self.widgets.release_view.focus(single_release.id_)
self.widgets.release_view.selection_set(single_release.id_)
#
#
self.widgets.scroll_vertical = tkinter.Scrollbar(
select_frame,
orient=tkinter.VERTICAL,
command=self.widgets.release_view.yview,
)
self.widgets.release_view[
"yscrollcommand"
] = self.widgets.scroll_vertical.set
self.widgets.release_view.grid(row=1, column=0)
self.widgets.scroll_vertical.grid(
row=1, column=1, sticky=tkinter.N + tkinter.S
)
select_frame.grid(**self.grid_fullwidth)
typo_fix_frame = tkinter.Frame(
self.widgets.action_area, **self.with_border
)
current_row = 0
typo_fix_heading = tkinter.Label(
typo_fix_frame,
text="Apply typographic fixes in the following order:",
justify=tkinter.LEFT,
)
typo_fix_heading.grid(
row=current_row, column=0, padx=4, sticky=tkinter.W
)
for (fix_name, is_active) in self.variables.typography_fixes:
current_row += 1
checkbox = tkinter.Checkbutton(
typo_fix_frame,
text=TYPOGRAPHY_FIXES[fix_name].description,
variable=is_active,
)
# for re-ordering:
# self.widgets.typography_fixes[fix_name] = checkbox
checkbox.grid(row=current_row, column=0, padx=4, sticky=tkinter.W)
#
typo_fix_frame.grid(**self.grid_fullwidth)
def panel_confirm_translations(self):
"""Panel with Metadata translations display"""
self.variables.metadata_translations.clear()
select_frame = tkinter.Frame(
self.widgets.action_area, **self.with_border
)
mb_release = self.variables.selected_mb_release
if not mb_release.translated_accessors:
return
#
label = tkinter.Label(
select_frame,
text="Please review typography fixes.\n"
"Double-click a tag to toggle between leaving"
" the value from MusicBrainz unchanged\n"
"and using the typographically fixed value.",
justify=tkinter.LEFT,
)
label.grid(row=0, column=0, columnspan=2, sticky=tkinter.W)
self.widgets.translation_view = ttk.Treeview(
master=select_frame,
height=15,
selectmode=tkinter.BROWSE,
show="tree",
)
self.widgets.translation_view.column("#0", width=700)
self.widgets.translation_view.bind(
"<Double-Button-1>", self.toggle_translation
)
self.widgets.translation_view.bind("<Return>", self.toggle_translation)
release_iid = None
media_iids = {}
track_iids = {}
for accessor in mb_release.translated_accessors:
if not release_iid:
release_iid = self.widgets.translation_view.insert(
"", tkinter.END, open=True, text=str(mb_release)
)
#
description = mb_release.get_description(**accessor)
try:
track_number = accessor[mbdata.TRACK_NUMBER]
except KeyError:
current_iid = self.widgets.translation_view.insert(
release_iid, tkinter.END, text=description
)
else:
try:
medium_number = accessor[mbdata.MEDIUM_NUMBER]
except KeyError:
logging.warning(
"Medium for accessor %r not found", accessor
)
continue
else:
try:
medium_iid = media_iids[medium_number]
except KeyError:
medium_iid = self.widgets.translation_view.insert(
release_iid,
tkinter.END,
open=True,
text="%s #%s"
% (
mb_release.get_object(
medium_number=medium_number
).format,
medium_number,
),
)
media_iids[medium_number] = medium_iid
#
try:
track_iid = track_iids[(medium_number, track_number)]
except KeyError:
mb_track = mb_release.get_object(
medium_number=medium_number,
track_number=track_number,
)
track_iid = self.widgets.translation_view.insert(
medium_iid,
tkinter.END,
open=True,
text="%02d. %s – %s"
% (
mb_track.track_number,
mb_track[mbdata.ARTIST],
mb_track[mbdata.TITLE],
),
)
track_iids[(medium_number, track_number)] = track_iid
#
current_iid = self.widgets.translation_view.insert(
track_iid, tkinter.END, text=description
)
#
#
self.variables.metadata_translations[current_iid] = accessor
#
self.widgets.scroll_vertical = tkinter.Scrollbar(
select_frame,
orient=tkinter.VERTICAL,
command=self.widgets.translation_view.yview,
)
self.widgets.translation_view[
"yscrollcommand"
] = self.widgets.scroll_vertical.set
self.widgets.translation_view.grid(row=1, column=0)
self.widgets.scroll_vertical.grid(
row=1, column=1, sticky=tkinter.N + tkinter.S
)
select_frame.grid(**self.grid_fullwidth)
def panel_confirm_metadata(self):
"""Panel with Metadata changes confirmation"""
if not self.variables.metadata_changes:
return
#
self.variables.metadata_lookup.clear()
select_frame = tkinter.Frame(
self.widgets.action_area, **self.with_border
)
label = tkinter.Label(
select_frame,
text="Please review metadata changes.\n"
"Double-click a tag to toggle between leaving"
" the original value unchanged (\u2205)\n"
"and using the value retrieved from MusicBrainz (\u21d2).",
justify=tkinter.LEFT,
)
label.grid(row=0, column=0, columnspan=2, sticky=tkinter.W)
self.widgets.metadata_view = ttk.Treeview(
master=select_frame,
height=15,
selectmode=tkinter.BROWSE,
show="tree",
)
self.widgets.metadata_view.column("#0", width=700)
self.widgets.metadata_view.bind(
"<Double-Button-1>", self.toggle_tag_value
)
self.widgets.metadata_view.bind("<Return>", self.toggle_tag_value)
for (
file_name,
single_change,
) in self.variables.metadata_changes.items():
track_iid = self.widgets.metadata_view.insert(
"", tkinter.END, open=True, text=file_name
)
#
for tag_key in single_change.keys():
tag_iid = self.widgets.metadata_view.insert(
track_iid, tkinter.END, text=single_change.display(tag_key)
)
self.variables.metadata_lookup[tag_iid] = (file_name, tag_key)
#
#
self.widgets.scroll_vertical = tkinter.Scrollbar(
select_frame,
orient=tkinter.VERTICAL,
command=self.widgets.metadata_view.yview,
)
self.widgets.metadata_view[
"yscrollcommand"
] = self.widgets.scroll_vertical.set
self.widgets.metadata_view.grid(row=1, column=0)
self.widgets.scroll_vertical.grid(
row=1, column=1, sticky=tkinter.N + tkinter.S
)
ignore_mb_metadata = tkinter.Checkbutton(
select_frame,
text="Skip all above metadata changes",
variable=self.variables.ignore_mb_data,
justify=tkinter.LEFT,
)
ignore_mb_metadata.grid(
row=2, column=0, columnspan=2, padx=4, sticky=tkinter.W
)
select_frame.grid(**self.grid_fullwidth)
def panel_rename_options(self):
"""Panel with Metadata changes summary
and renaming options
"""
logging.debug(self.variables.changed_tracks)
if self.variables.changed_tracks:
select_frame = tkinter.Frame(
self.widgets.action_area, **self.with_border
)
label = tkinter.Label(
select_frame,
text="Metadata in the following tracks were updated:",
justify=tkinter.LEFT,
)
label.grid(row=0, column=0, columnspan=2, sticky=tkinter.W)
result_view = ttk.Treeview(
master=select_frame,
height=15,
selectmode=tkinter.BROWSE,
show="tree",
)
result_view.column("#0", width=700)
for (
file_name,
changes_done,
) in self.variables.changed_tracks.items():
track_iid = result_view.insert(
"", tkinter.END, open=True, text=file_name
)
#
for message in changes_done:
result_view.insert(track_iid, tkinter.END, text=message)
#
#
self.widgets.scroll_vertical = tkinter.Scrollbar(
select_frame,
orient=tkinter.VERTICAL,
command=result_view.yview,
)
result_view["yscrollcommand"] = self.widgets.scroll_vertical.set
result_view.grid(row=1, column=0)
self.widgets.scroll_vertical.grid(
row=1, column=1, sticky=tkinter.N + tkinter.S
)
select_frame.grid(**self.grid_fullwidth)
#
options_frame = tkinter.Frame(
self.widgets.action_area, **self.with_border
)
label = tkinter.Label(
options_frame,
text="Please specify renaming options:",
justify=tkinter.LEFT,
)
label.grid(sticky=tkinter.W)
include_artist = tkinter.Checkbutton(
options_frame,
text="Always include artist in file name",
variable=self.variables.always_include_artist,
justify=tkinter.LEFT,
)
include_artist.grid(padx=4, sticky=tkinter.W)
include_medium = tkinter.Checkbutton(
options_frame,
text="Include medium prefix in file name",
variable=self.variables.include_medium,
justify=tkinter.LEFT,
)
include_medium.grid(padx=4, sticky=tkinter.W)
options_frame.grid(**self.grid_fullwidth)
def panel_confirm_rename(self):
"""Confirm files to be renamed"""
if not self.variables.renaming_plan:
return
#
select_frame = tkinter.Frame(
self.widgets.action_area, **self.with_border
)
label = tkinter.Label(
select_frame,
text="The following files will be renamed:",
justify=tkinter.LEFT,
)
label.grid(row=0, column=0, columnspan=2, sticky=tkinter.W)
result_view = ttk.Treeview(
master=select_frame,
height=15,
selectmode=tkinter.BROWSE,
show="tree",
)
result_view.column("#0", width=700)
for rename_item in self.variables.renaming_plan:
track_iid = result_view.insert(
"", tkinter.END, open=True, text=rename_item.source_path.name
)
result_view.insert(
track_iid,
tkinter.END,
text="→ %s" % rename_item.target_path.name,
)
#
#
self.widgets.scroll_vertical = tkinter.Scrollbar(
select_frame, orient=tkinter.VERTICAL, command=result_view.yview
)
result_view["yscrollcommand"] = self.widgets.scroll_vertical.set
result_view.grid(row=1, column=0)
self.widgets.scroll_vertical.grid(
row=1, column=1, sticky=tkinter.N + tkinter.S
)
#
select_frame.grid(**self.grid_fullwidth)
def panel_rename_files(self):
"""Show results"""
select_frame = tkinter.Frame(
self.widgets.action_area, **self.with_border
)
label = tkinter.Label(
select_frame,
text="Results of the mass renaming operation:",
justify=tkinter.LEFT,
)
label.grid(row=0, column=0, columnspan=2, sticky=tkinter.W)
result_view = ttk.Treeview(
master=select_frame,
height=15,
selectmode=tkinter.BROWSE,
show="tree",
)
result_view.column("#0", width=700)
number_success = len(self.variables.rename_result.renamed_files)
conflicts = len(self.variables.rename_result.conflicts)