| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import json
- import os
- import logging
- from modules.core.state import state
- # Configure logging
- logger = logging.getLogger(__name__)
- # Global state
- PLAYLISTS_FILE = os.path.join(os.getcwd(), "playlists.json")
- # Ensure the file exists and contains at least an empty JSON object
- if not os.path.isfile(PLAYLISTS_FILE):
- logger.info(f"Creating new playlists file at {PLAYLISTS_FILE}")
- with open(PLAYLISTS_FILE, "w") as f:
- json.dump({}, f, indent=2)
- def load_playlists():
- """Load the entire playlists dictionary from the JSON file."""
- try:
- with open(PLAYLISTS_FILE, "r") as f:
- content = f.read().strip()
- if not content:
- logger.warning("Playlists file is empty, returning empty dict")
- return {}
- playlists = json.loads(content)
- logger.debug(f"Loaded {len(playlists)} playlists")
- return playlists
- except (json.JSONDecodeError, ValueError) as e:
- logger.error(f"Playlists file is corrupted, resetting to empty: {e}")
- save_playlists({})
- return {}
- def save_playlists(playlists_dict):
- """Save the entire playlists dictionary back to the JSON file."""
- logger.debug(f"Saving {len(playlists_dict)} playlists to file")
- with open(PLAYLISTS_FILE, "w") as f:
- json.dump(playlists_dict, f, indent=2)
- def list_all_playlists():
- """Returns a list of all playlist names."""
- playlists_dict = load_playlists()
- playlist_names = list(playlists_dict.keys())
- logger.debug(f"Found {len(playlist_names)} playlists")
- return playlist_names
- def get_playlist(playlist_name):
- """Get a specific playlist by name."""
- playlists_dict = load_playlists()
- if playlist_name not in playlists_dict:
- logger.warning(f"Playlist not found: {playlist_name}")
- return None
- logger.debug(f"Retrieved playlist: {playlist_name}")
- return {
- "name": playlist_name,
- "files": playlists_dict[playlist_name]
- }
- def create_playlist(playlist_name, files):
- """Create or update a playlist."""
- playlists_dict = load_playlists()
- playlists_dict[playlist_name] = files
- save_playlists(playlists_dict)
- logger.info(f"Created/updated playlist '{playlist_name}' with {len(files)} files")
- # Keep the board's /playlists/<name>.txt mirror in sync (autostart runs it).
- from modules.core import board_settings
- board_settings.mirror_playlist_async(playlist_name, files)
- return True
- def modify_playlist(playlist_name, files):
- """Modify an existing playlist."""
- logger.info(f"Modifying playlist '{playlist_name}' with {len(files)} files")
- return create_playlist(playlist_name, files)
- def delete_playlist(playlist_name):
- """Delete a playlist."""
- playlists_dict = load_playlists()
- if playlist_name not in playlists_dict:
- logger.warning(f"Cannot delete non-existent playlist: {playlist_name}")
- return False
- del playlists_dict[playlist_name]
- save_playlists(playlists_dict)
- logger.info(f"Deleted playlist: {playlist_name}")
- from modules.core import board_settings
- board_settings.unmirror_playlist_async(playlist_name)
- return True
- def add_to_playlist(playlist_name, pattern):
- """Add a pattern to an existing playlist."""
- playlists_dict = load_playlists()
- if playlist_name not in playlists_dict:
- logger.warning(f"Cannot add to non-existent playlist: {playlist_name}")
- return False
- playlists_dict[playlist_name].append(pattern)
- save_playlists(playlists_dict)
- logger.info(f"Added pattern '{pattern}' to playlist '{playlist_name}'")
- from modules.core import board_settings
- board_settings.mirror_playlist_async(playlist_name, playlists_dict[playlist_name])
- return True
- def rename_playlist(old_name, new_name):
- """Rename an existing playlist."""
- if not new_name or not new_name.strip():
- logger.warning("Cannot rename playlist: new name is empty")
- return False, "New name cannot be empty"
- new_name = new_name.strip()
- playlists_dict = load_playlists()
- if old_name not in playlists_dict:
- logger.warning(f"Cannot rename non-existent playlist: {old_name}")
- return False, "Playlist not found"
- if old_name == new_name:
- return True, "Name unchanged"
- if new_name in playlists_dict:
- logger.warning(f"Cannot rename playlist: '{new_name}' already exists")
- return False, "A playlist with that name already exists"
- # Copy files to new key and delete old key
- playlists_dict[new_name] = playlists_dict[old_name]
- del playlists_dict[old_name]
- save_playlists(playlists_dict)
- logger.info(f"Renamed playlist '{old_name}' to '{new_name}'")
- from modules.core import board_settings
- board_settings.unmirror_playlist_async(old_name)
- board_settings.mirror_playlist_async(new_name, playlists_dict[new_name])
- return True, f"Playlist renamed to '{new_name}'"
|