- Implemented RenameService for handling file renaming with features like name validation, proposed name generation, conflict detection, and atomic rename operations. - Created utility modules for language code extraction, regex pattern matching, and frame class matching to centralize common functionalities. - Added comprehensive logging for error handling and debugging across all new modules.
8.9 KiB
AI Agent Instructions for Media File Renamer Project
Project Description
This is a Python Terminal User Interface (TUI) application for managing media files. It uses the Textual library to provide a curses-like interface in the terminal. The app allows users to scan directories for video files, display them in a hierarchical tree view, view detailed metadata information including video, audio, and subtitle tracks, and rename files based on intelligent metadata extraction.
Current Version: 0.7.0-dev (Phase 1 complete)
Key features:
- Recursive directory scanning with tree navigation
- Dual-mode display: Technical (codec/track details) and Catalog (TMDB metadata with posters)
- Tree-based file navigation with expand/collapse functionality
- Multi-source metadata extraction (MediaInfo, filename parsing, embedded tags, TMDB API)
- Intelligent file renaming with proposed names and confirmation
- Settings management with persistent configuration
- NEW: Unified cache subsystem with flexible strategies and decorators
- NEW: Command palette (Ctrl+P) with cache management commands
- NEW: Thread-safe cache with RLock protection
- NEW: Comprehensive logging (warning/debug levels)
- NEW: Proper exception handling (no bare except clauses)
- Terminal poster display using rich-pixels
- Color-coded information display
- Keyboard and mouse navigation
- Multiple UI screens (main app, directory selection, help, rename confirmation, settings)
- Extensible extractor and formatter architecture
- Loading indicators and comprehensive error handling
Technology Stack
- Python 3.11+
- Textual ≥6.11.0 (TUI framework)
- PyMediaInfo ≥6.0.0 (detailed track information)
- Mutagen ≥1.47.0 (embedded metadata)
- Python-Magic ≥0.4.27 (MIME type detection)
- Langcodes ≥3.5.1 (language code handling)
- Requests ≥2.31.0 (HTTP client for TMDB API)
- Rich-Pixels ≥1.0.0 (terminal image display)
- Pytest ≥7.0.0 (testing framework)
- UV (package manager and build tool)
Code Structure
renamer/main.py: Main application entry point with argument parsingpyproject.toml: Project configuration and dependencies (version 0.5.10)README.md: User documentationDEVELOP.md: Developer guide with debugging infoINSTALL.md: Installation instructionsCLAUDE.md: Comprehensive AI assistant reference guideToDo.md: Development task trackingAI_AGENT.md: This file (AI agent instructions)renamer/: Main packageapp.py: Main Textual application class with tree management, file operations, and command palettesettings.py: Settings management with JSON storagecache/: NEW Unified cache subsystem (v0.7.0)core.py: Thread-safe Cache classstrategies.py: Cache key generation strategiesmanagers.py: CacheManager for operationsdecorators.py: Enhanced cache decoratorstypes.py: Type definitions
secrets.py: API keys and secrets (TMDB)constants.py: Application constants (media types, sources, resolutions, special editions)screens.py: Additional UI screens (OpenScreen, HelpScreen, RenameConfirmScreen, SettingsScreen)bump.py: Version bump utilityrelease.py: Release automation scriptextractors/: Individual extractor classesextractor.py: MediaExtractor class coordinating all extractorsmediainfo_extractor.py: PyMediaInfo-based extractionfilename_extractor.py: Filename parsing with regex patternsmetadata_extractor.py: Mutagen-based embedded metadatafileinfo_extractor.py: Basic file informationtmdb_extractor.py: The Movie Database API integrationdefault_extractor.py: Fallback extractor
formatters/: Data formatting classesformatter.py: Base formatter interfacemedia_formatter.py: Main formatter coordinating displaycatalog_formatter.py: Catalog mode formatting with TMDB dataproposed_name_formatter.py: Generates rename suggestionstrack_formatter.py: Track information formattingsize_formatter.py: File size formattingdate_formatter.py: Timestamp formattingduration_formatter.py: Duration formattingresolution_formatter.py: Resolution formattingtext_formatter.py: Text styling utilitiesextension_formatter.py: File extension formattinghelper_formatter.py: Helper formatting utilitiesspecial_info_formatter.py: Special edition information
decorators/: Utility decoratorscaching.py: Caching decorator for automatic method caching
test/: Unit tests for extractorstest_filename_extractor.py: Filename parsing teststest_mediainfo_extractor.py: MediaInfo extraction teststest_mediainfo_frame_class.py: Frame class detection teststest_fileinfo_extractor.py: File info teststest_metadata_extractor.py: Metadata extraction teststest_filename_detection.py: Filename pattern detection testsfilenames.txt,test_filenames.txt: Sample test datatest_cases.json,test_mediainfo_frame_class.json: Test fixtures
Instructions for AI Agents
Coding Standards
- Use type hints where possible
- Follow PEP 8 style guidelines
- Use descriptive variable and function names
- Add docstrings for functions and classes
- Handle exceptions appropriately
- Use pathlib for file operations
Development Workflow
- Read the current code and understand the architecture
- Check the ToDo.md for pending tasks
- Implement features incrementally
- Test changes by running the app with
uv run python main.py [directory] - Update tests as needed
- Ensure backward compatibility
- Update documentation (README.md, ToDo.md) when adding features
Key Components
RenamerApp: Main application class inheriting from Textual's App- Manages the tree view and file operations
- Handles keyboard navigation and commands
- Coordinates metadata extraction and display
- Implements efficient tree updates for renamed files
MediaTree: Custom Tree widget with file-specific styling (inherited from Textual Tree)MediaExtractor: Coordinates multiple specialized extractorsMediaFormatter: Formats extracted data for TUI display- Various extractor classes for different data sources
- Various formatter classes for different data types
- Screen classes for different UI states
Extractor Architecture
Extractors are responsible for gathering raw data from different sources:
- Each extractor inherits from no base class but follows the pattern of
__init__(file_path)andextract_*()methods - The
MediaExtractorclass coordinates multiple extractors and provides a unifiedget()interface - Extractors return raw data (strings, numbers, dicts) without formatting
Formatter Architecture
Formatters are responsible for converting raw data into display strings:
- Each formatter provides static methods like
format_*() - The
MediaFormattercoordinates formatters and applies them based on data types ProposedNameFormattergenerates intelligent rename suggestions- Formatters handle text styling, color coding, and human-readable representations
Screen Architecture
The app uses multiple screens for different operations:
OpenScreen: Directory selection with input validationHelpScreen: Comprehensive help with key bindingsRenameConfirmScreen: File rename confirmation with error handling
Completed Major Features
- ✅ Settings management with JSON configuration
- ✅ Mode toggle (technical/catalog)
- ✅ Caching system with TTL support
- ✅ TMDB integration for catalog data
- ✅ Poster display in terminal
- ✅ Settings UI screen
Future Enhancements
- Metadata editing capabilities
- Batch rename operations
- Plugin system for custom extractors/formatters
- Advanced search and filtering
- Undo/redo functionality
- Blue highlighting for changed parts in proposed filename
- Exclude dev commands from distributed package
- Full genre name expansion (currently shows codes)
- Optimized poster quality and display
Testing
- Run the app with
uv run python main.py [directory] - Test navigation, selection, and display
- Verify metadata extraction accuracy
- Test file renaming functionality
- Check for any errors or edge cases
- Run unit tests with
uv run pytest
Contribution Guidelines
- Make small, focused changes
- Update documentation as needed
- Ensure the app runs without errors
- Follow the existing code patterns
- Update tests for new functionality
- Update ToDo.md when completing tasks
- Update version numbers appropriately
Important Files for AI Assistants
For comprehensive project information, AI assistants should refer to:
- CLAUDE.md: Complete AI assistant reference guide (most comprehensive)
- AI_AGENT.md: This file (concise instructions)
- DEVELOP.md: Developer setup and debugging
- ToDo.md: Current task list and completed items
- README.md: User-facing documentation
This document should be updated as the project evolves.
Last Updated: 2025-12-31