2022-12-28 10:14:33 -06:00
|
|
|
from os import path, listdir
|
|
|
|
from menu import Menu, MenuItem, MenuType
|
|
|
|
from cinput import ControlInput
|
|
|
|
from graphics import Graphics
|
|
|
|
from .player import MusicPlayer
|
|
|
|
|
2022-12-28 11:17:14 -06:00
|
|
|
SUPPORTED_FORMATS = (
|
|
|
|
".mp3", ".ogg", ".m4a", ".wav", ".flac", ".ape",
|
|
|
|
".aiff", ".aif", ".wma", ".mid", ".midi")
|
|
|
|
|
2022-12-28 10:14:33 -06:00
|
|
|
def execute(cinput: ControlInput, graphics: Graphics, directory: str):
|
2022-12-28 10:51:41 -06:00
|
|
|
is_resume = directory != "music"
|
2022-12-28 10:14:33 -06:00
|
|
|
current_dir = path.join("/home/rudism", directory)
|
2022-12-28 10:51:41 -06:00
|
|
|
top_menu = create_directory_menu(current_dir, "music", is_resume)
|
2022-12-28 10:32:07 -06:00
|
|
|
menu = Menu(top_menu.data["sub_menu"], cinput, graphics)
|
2022-12-28 10:14:33 -06:00
|
|
|
MusicPlayer(cinput, graphics, menu).run()
|
|
|
|
|
2022-12-28 10:51:41 -06:00
|
|
|
def create_directory_menu(directory: str, dirname: str, is_resume: bool):
|
2022-12-28 11:12:15 -06:00
|
|
|
dir_menu = list()
|
|
|
|
track_menu = list()
|
2022-12-28 10:51:41 -06:00
|
|
|
for fname in sorted(listdir(directory), key=str.casefold):
|
2022-12-28 10:14:33 -06:00
|
|
|
new_path = path.join(directory, fname)
|
|
|
|
if path.isdir(new_path):
|
2022-12-28 11:12:15 -06:00
|
|
|
dir_menu.append(create_directory_menu(new_path, fname, is_resume))
|
2022-12-28 11:17:14 -06:00
|
|
|
elif fname.lower().endswith(tuple(SUPPORTED_FORMATS)):
|
2022-12-28 11:12:15 -06:00
|
|
|
track_menu.append(MenuItem(fname, MenuType.CMD, {
|
2022-12-28 10:51:41 -06:00
|
|
|
"dir": directory,
|
2022-12-28 11:12:15 -06:00
|
|
|
"mode": "single",
|
2022-12-28 12:26:06 -06:00
|
|
|
"track": fname}))
|
2022-12-28 11:12:15 -06:00
|
|
|
if len(dir_menu) > 0:
|
|
|
|
return MenuItem(dirname, MenuType.SUB_MENU, {"sub_menu": dir_menu})
|
|
|
|
else:
|
|
|
|
if is_resume:
|
2022-12-28 11:26:30 -06:00
|
|
|
return MenuItem(dirname.lstrip("_"), MenuType.CMD, {
|
2022-12-28 10:51:41 -06:00
|
|
|
"dir": directory,
|
2022-12-28 11:12:15 -06:00
|
|
|
"mode": "resume"})
|
|
|
|
else:
|
2022-12-28 11:26:30 -06:00
|
|
|
return MenuItem(dirname.lstrip("_"), MenuType.SUB_MENU, {
|
2022-12-28 11:12:15 -06:00
|
|
|
"sub_menu": [
|
|
|
|
MenuItem("Play", MenuType.CMD, {
|
|
|
|
"dir": directory,
|
|
|
|
"mode": "album"}),
|
|
|
|
MenuItem("Randomize", MenuType.CMD, {
|
|
|
|
"dir": directory,
|
|
|
|
"mode": "random"}),
|
|
|
|
MenuItem("View Tracks", MenuType.SUB_MENU, {
|
|
|
|
"sub_menu": track_menu})
|
|
|
|
]})
|