Made mutagen import optional

This commit is contained in:
NicoHood 2016-08-27 21:02:59 +02:00
parent a21b0a2fe1
commit 3ff22a4950

View file

@ -1,11 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Builtin libraries
import sys import sys
import struct import struct
import urllib.request, urllib.parse, urllib.error import urllib.request, urllib.parse, urllib.error
import os import os
import hashlib import hashlib
import mutagen
import binascii
import subprocess import subprocess
import collections import collections
import errno import errno
@ -15,6 +15,12 @@ import re
import tempfile import tempfile
import signal import signal
# External libraries
try:
import mutagen
except ImportError:
mutagen = None
audio_ext = (".mp3", ".m4a", ".m4b", ".m4p", ".aa", ".wav") audio_ext = (".mp3", ".m4a", ".m4b", ".m4p", ".aa", ".wav")
list_ext = (".pls", ".m3u") list_ext = (".pls", ".m3u")
def make_dir_if_absent(path): def make_dir_if_absent(path):
@ -359,6 +365,9 @@ class Track(Record):
self["filetype"] = 2 self["filetype"] = 2
text = os.path.splitext(os.path.basename(filename))[0] text = os.path.splitext(os.path.basename(filename))[0]
# Try to get album and artist information with mutagen
if mutagen:
audio = None audio = None
try: try:
audio = mutagen.File(filename, easy = True) audio = mutagen.File(filename, easy = True)
@ -622,10 +631,15 @@ class Shuffler(object):
self.lists.append(os.path.abspath(dirpath)) self.lists.append(os.path.abspath(dirpath))
if self.auto_id3_playlists != None: if self.auto_id3_playlists != None:
if mutagen:
for grouped_list in group_tracks_by_id3_template(self.tracks, self.auto_id3_playlists): for grouped_list in group_tracks_by_id3_template(self.tracks, self.auto_id3_playlists):
self.lists.append(grouped_list) self.lists.append(grouped_list)
else:
print("Error: No mutagen found. Cannot generate auto-id3-playlists.")
sys.exit(1)
def write_database(self): def write_database(self):
print("Writing database. This may take a while...")
with open(os.path.join(self.path, "iPod_Control", "iTunes", "iTunesSD"), "wb") as f: with open(os.path.join(self.path, "iPod_Control", "iTunes", "iTunesSD"), "wb") as f:
try: try:
f.write(self.tunessd.construct()) f.write(self.tunessd.construct())
@ -633,6 +647,7 @@ class Shuffler(object):
print("I/O error({0}): {1}".format(e.errno, e.strerror)) print("I/O error({0}): {1}".format(e.errno, e.strerror))
print("Error: Writing iPod database failed.") print("Error: Writing iPod database failed.")
sys.exit(1) sys.exit(1)
print("Database written successfully:") print("Database written successfully:")
print("Tracks", len(self.tracks)) print("Tracks", len(self.tracks))
print("Albums", len(self.albums)) print("Albums", len(self.albums))
@ -740,6 +755,9 @@ if __name__ == '__main__':
if result.rename_unicode: if result.rename_unicode:
check_unicode(result.path) check_unicode(result.path)
if not mutagen:
print("Warning: No mutagen found. Database will not contain any album nor artist information.")
verboseprint("Playlist voiceover requested:", result.playlist_voiceover) verboseprint("Playlist voiceover requested:", result.playlist_voiceover)
verboseprint("Track voiceover requested:", result.track_voiceover) verboseprint("Track voiceover requested:", result.track_voiceover)
if (result.track_voiceover or result.playlist_voiceover): if (result.track_voiceover or result.playlist_voiceover):