This repository has been archived on 2022-12-29. You can view files and clone it, but cannot push or open issues or pull requests.
zeropod/plugin/music/__init__.py

49 lines
1.9 KiB
Python
Raw Normal View History

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
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))
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",
"track": fname}))
if len(dir_menu) > 0:
return MenuItem(dirname, MenuType.SUB_MENU, {"sub_menu": dir_menu})
else:
if is_resume:
2022-12-28 10:51:41 -06:00
return MenuItem(dirname, MenuType.CMD, {
"dir": directory,
2022-12-28 11:12:15 -06:00
"mode": "resume"})
else:
return MenuItem(dirname, MenuType.SUB_MENU, {
"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})
]})