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") def execute(cinput: ControlInput, graphics: Graphics, directory: str): is_resume = directory != "music" current_dir = path.join("/home/rudism", directory) top_menu = create_directory_menu(current_dir, "music", is_resume) menu = Menu(top_menu.data["sub_menu"], cinput, graphics) MusicPlayer(cinput, graphics, menu).run() def create_directory_menu(directory: str, dirname: str, is_resume: bool): dir_menu = list() track_menu = list() for fname in sorted(listdir(directory), key=str.casefold): new_path = path.join(directory, fname) if path.isdir(new_path): dir_menu.append(create_directory_menu(new_path, fname, is_resume)) elif fname.lower().endswith(tuple(SUPPORTED_FORMATS)): track_menu.append(MenuItem(fname, MenuType.CMD, { "dir": directory, "mode": "single", "track": path.join(directory, fname)})) if len(dir_menu) > 0: return MenuItem(dirname, MenuType.SUB_MENU, {"sub_menu": dir_menu}) else: if is_resume: return MenuItem(dirname.lstrip("_"), MenuType.CMD, { "dir": directory, "mode": "resume"}) else: return MenuItem(dirname.lstrip("_"), 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}) ]})