use Enums for (Playlist,File)Types

This commit is contained in:
Arno Hautala 2021-09-01 12:30:00 -04:00
parent e0a2f6cf85
commit 3ca83b6273

View file

@ -14,6 +14,8 @@ import shutil
import re import re
import tempfile import tempfile
import signal import signal
import enum
import functools
# External libraries # External libraries
try: try:
@ -21,8 +23,25 @@ try:
except ImportError: except ImportError:
mutagen = None mutagen = None
audio_ext = (".mp3", ".m4a", ".m4b", ".m4p", ".aa", ".wav") class PlaylistType(enum.Enum):
list_ext = (".pls", ".m3u") ALL_SONGS = 1
NORMAL = 2
PODCAST = 3
AUDIOBOOK = 4
class FileType(enum.Enum):
MP3 = (1, {'.mp3'})
AAC = (2, {'.m4a', '.m4b', '.m4p', '.aa'})
WAV = (4, {'.wav'})
def __init__(self, filetype, extensions):
self.filetype = filetype
self.extensions = extensions
audio_ext = functools.reduce(lambda j,k: j.union(k), map(lambda i: i.extensions, FileType))
list_ext = {".pls", ".m3u"}
all_ext = audio_ext.union(list_ext)
def make_dir_if_absent(path): def make_dir_if_absent(path):
try: try:
os.makedirs(path) os.makedirs(path)
@ -368,8 +387,12 @@ class Track(Record):
def populate(self, filename): def populate(self, filename):
self["filename"] = self.path_to_ipod(filename).encode('utf-8') self["filename"] = self.path_to_ipod(filename).encode('utf-8')
if os.path.splitext(filename)[1].lower() in (".m4a", ".m4b", ".m4p", ".aa"): # assign the "filetype" based on the extension
self["filetype"] = 2 ext = os.path.splitext(filename)[1].lower()
for type in FileType:
if ext in type.extensions:
self.filetype = type.filetype
break
if "/iPod_Control/Podcasts/" in filename: if "/iPod_Control/Podcasts/" in filename:
self.is_podcast = True self.is_podcast = True
@ -472,6 +495,7 @@ class PlaylistHeader(Record):
class Playlist(Record): class Playlist(Record):
def __init__(self, parent): def __init__(self, parent):
self.listtracks = [] self.listtracks = []
self.listtype = PlaylistType.NORMAL
Record.__init__(self, parent) Record.__init__(self, parent)
self._struct = collections.OrderedDict([ self._struct = collections.OrderedDict([
("header_id", ("4s", b"lphs")), # shpl ("header_id", ("4s", b"lphs")), # shpl
@ -489,11 +513,14 @@ class Playlist(Record):
if self.playlist_voiceover and (Text2Speech.valid_tts['pico2wave'] or Text2Speech.valid_tts['espeak']): if self.playlist_voiceover and (Text2Speech.valid_tts['pico2wave'] or Text2Speech.valid_tts['espeak']):
self["dbid"] = hashlib.md5(b"masterlist").digest()[:8] self["dbid"] = hashlib.md5(b"masterlist").digest()[:8]
self.text_to_speech("All songs", self["dbid"], True) self.text_to_speech("All songs", self["dbid"], True)
self["listtype"] = 1 self.listtype = PlaylistType.ALL_SONGS
self.listtracks = tracks self.listtracks = tracks
def set_audiobook(self):
self.listtype = PlaylistType.AUDIOBOOK
def set_podcast(self): def set_podcast(self):
self["listtype"] = 3 self.listtype = PlaylistType.PODCAST
def populate_m3u(self, data): def populate_m3u(self, data):
listtracks = [] listtracks = []
@ -584,6 +611,7 @@ class Playlist(Record):
def construct(self, tracks): def construct(self, tracks):
self["total_length"] = 44 + (4 * len(self.listtracks)) self["total_length"] = 44 + (4 * len(self.listtracks))
self["number_of_songs"] = 0 self["number_of_songs"] = 0
self["listtype"] = self.listtype.value
chunks = bytes() chunks = bytes()
for i in self.listtracks: for i in self.listtracks:
@ -696,7 +724,7 @@ def check_unicode(path):
ret_flag = False # True if there is a recognizable file within this level ret_flag = False # True if there is a recognizable file within this level
for item in os.listdir(path): for item in os.listdir(path):
if os.path.isfile(os.path.join(path, item)): if os.path.isfile(os.path.join(path, item)):
if os.path.splitext(item)[1].lower() in audio_ext+list_ext: if os.path.splitext(item)[1].lower() in all_ext:
ret_flag = True ret_flag = True
if raises_unicode_error(item): if raises_unicode_error(item):
src = os.path.join(path, item) src = os.path.join(path, item)