#!/usr/bin/env python3 """ Preferences Manager for Karaoke Song Library Cleanup Tool Handles loading and applying user priority preferences for file selection. """ import json import os from typing import Dict, List, Optional, Tuple from pathlib import Path class PreferencesManager: """Manages user priority preferences for file selection.""" def __init__(self, data_dir: str = "../data"): self.data_dir = Path(data_dir) self.preferences_dir = self.data_dir / "preferences" self.preferences_file = self.preferences_dir / "priority_preferences.json" self._preferences: Dict[str, List[str]] = {} self._load_preferences() def _load_preferences(self) -> None: """Load priority preferences from file.""" try: if self.preferences_file.exists(): with open(self.preferences_file, 'r', encoding='utf-8') as f: self._preferences = json.load(f) print(f"Loaded {len(self._preferences)} priority preferences") else: self._preferences = {} print("No priority preferences found") except Exception as e: print(f"Warning: Could not load priority preferences: {e}") self._preferences = {} def get_priority_order(self, artist: str, title: str) -> Optional[List[str]]: """Get priority order for a specific song.""" song_key = f"{artist} - {title}" return self._preferences.get(song_key) def has_preferences(self) -> bool: """Check if any preferences exist.""" return len(self._preferences) > 0 def get_preference_count(self) -> int: """Get the number of songs with preferences.""" return len(self._preferences) def apply_priority_order(self, files: List[Dict], artist: str, title: str) -> List[Dict]: """ Apply user priority preferences to reorder files. Args: files: List of file dictionaries with 'path' key artist: Song artist title: Song title Returns: Reordered list of files based on user preferences """ priority_order = self.get_priority_order(artist, title) if not priority_order: return files # Create a mapping of path to file file_map = {file['path']: file for file in files} # Reorder files based on priority reordered_files = [] used_paths = set() # Add files in priority order for path in priority_order: if path in file_map: reordered_files.append(file_map[path]) used_paths.add(path) # Add any remaining files that weren't in the priority list for file in files: if file['path'] not in used_paths: reordered_files.append(file) return reordered_files def get_preferences_summary(self) -> Dict: """Get a summary of current preferences.""" return { 'total_preferences': len(self._preferences), 'songs_with_preferences': list(self._preferences.keys()), 'preferences_file': str(self.preferences_file) if self.preferences_file.exists() else None } def create_preferences_manager(data_dir: str = "../data") -> PreferencesManager: """Factory function to create a preferences manager.""" return PreferencesManager(data_dir)