88 lines
2.9 KiB
Python
88 lines
2.9 KiB
Python
|
#!/usr/bin/python3
|
||
|
|
||
|
from . import WBZM3UData
|
||
|
|
||
|
|
||
|
class WobuzzM3U:
|
||
|
sort_orders = {
|
||
|
"Title": WBZM3UData.SortOrder.track_title,
|
||
|
"Artist": WBZM3UData.SortOrder.track_artist,
|
||
|
"Album": WBZM3UData.SortOrder.track_album,
|
||
|
"Genre": WBZM3UData.SortOrder.track_genre,
|
||
|
"Custom": WBZM3UData.SortOrder.custom_sorting
|
||
|
}
|
||
|
|
||
|
sort_order_names = {
|
||
|
WBZM3UData.SortOrder.track_title: "Title",
|
||
|
WBZM3UData.SortOrder.track_artist: "Artist",
|
||
|
WBZM3UData.SortOrder.track_album: "Album",
|
||
|
WBZM3UData.SortOrder.track_genre: "Genre",
|
||
|
WBZM3UData.SortOrder.custom_sorting: "Custom"
|
||
|
}
|
||
|
|
||
|
def __init__(self, filename: str):
|
||
|
self.filename = filename
|
||
|
|
||
|
def parse_line(self, line: str) -> WBZM3UData | None:
|
||
|
if line.startswith("#"): # comments and EXTM3U/WOBUZZM3U
|
||
|
if line.startswith("#WOBUZZM3U"):
|
||
|
return WBZM3UData.Header()
|
||
|
|
||
|
elif line.startswith("#SORT: "): # sort
|
||
|
sorting_params = line[6:] # delete "#SORT: " from the line
|
||
|
|
||
|
sorting = sorting_params.split(", ") # split into the sort column specifier and the sort order
|
||
|
# e.g. ["Title", "Ascending"]
|
||
|
|
||
|
if not sorting[0] in self.sort_orders:
|
||
|
return None
|
||
|
|
||
|
sort_by = self.sort_orders[sorting[0]]
|
||
|
order = sorting[1] == "Ascending"
|
||
|
|
||
|
return WBZM3UData.SortOrder(sort_by, order)
|
||
|
|
||
|
elif line.startswith("#TRACK_TITLE: "):
|
||
|
return WBZM3UData.TrackMetadata.TrackTitle(line[14:])
|
||
|
|
||
|
elif line.startswith("#TRACK_ARTIST: "):
|
||
|
return WBZM3UData.TrackMetadata.TrackArtist(line[15:])
|
||
|
|
||
|
elif line.startswith("#TRACK_ALBUM: "):
|
||
|
return WBZM3UData.TrackMetadata.TrackAlbum(line[14:])
|
||
|
|
||
|
return None
|
||
|
|
||
|
elif line.startswith("http"):
|
||
|
return WBZM3UData.URL("URLs currently aren't supported.")
|
||
|
|
||
|
# line contains a path
|
||
|
return WBZM3UData.Path(line)
|
||
|
|
||
|
def assemble_line(self, data: WBZM3UData) -> str | None:
|
||
|
if isinstance(data, WBZM3UData.Header):
|
||
|
return "#WOBUZZM3U\n"
|
||
|
|
||
|
if isinstance(data, WBZM3UData.Path):
|
||
|
return f"{data}\n"
|
||
|
|
||
|
if isinstance(data, WBZM3UData.URL):
|
||
|
return None
|
||
|
|
||
|
if isinstance(data, WBZM3UData.SortOrder):
|
||
|
direction = "Ascending" if data.ascending else "Descending"
|
||
|
|
||
|
return f"#SORT: {self.sort_order_names[data.sort_by]}, {direction}\n"
|
||
|
|
||
|
if isinstance(data, WBZM3UData.TrackMetadata.TrackTitle):
|
||
|
return f"#TRACK_TITLE: {data}\n"
|
||
|
|
||
|
if isinstance(data, WBZM3UData.TrackMetadata.TrackArtist):
|
||
|
return f"#TRACK_ARTIST: {data}\n"
|
||
|
|
||
|
if isinstance(data, WBZM3UData.TrackMetadata.TrackAlbum):
|
||
|
return f"#TRACK_ALBUM: {data}\n"
|
||
|
|
||
|
if isinstance(data, WBZM3UData.TrackMetadata.TrackGenre):
|
||
|
return f"#TRACK_GENRE: {data}\n"
|