# Phase 3: Organization Engine ## Overview The Organization Engine provides flexible, configurable ROM organization by region, kind (type), and language. It supports three organization modes: 1. **Move** - Physical organization by moving files 2. **Copy** - Physical organization by copying files 3. **Symlink** - Virtual organization using symbolic links ## Architecture ### Base Organizer (`organizers/base.py`) Abstract base class providing: - File operations (move, copy, symlink) - Dry-run mode for previewing changes - Keep-in-place and exclusion filtering - Recursive directory processing - Statistics tracking ### Region Organizer (`organizers/region.py`) Organizes ROMs by region (USA, Europe, Japan, etc.): - Extracts region from filename - Multi-region priority support - Configurable keep-in-place regions - Creates `By Region//` structure **Example:** ```python from romfarmer.organizers import RegionOrganizer, OrganizeMode organizer = RegionOrganizer( mode=OrganizeMode.MOVE, keep_in_place=['USA', 'World'], # Keep these in root region_priority=['USA', 'Europe', 'Japan'] # For multi-region ROMs ) stats = organizer.organize(Path('/roms/nes')) ``` **Result:** ``` /roms/nes/ Super Mario Bros (USA).nes <- Kept in root World Game (World).nes <- Kept in root By Region/ Europe/ Game (Europe).nes <- Organized Japan/ Game (Japan).nes <- Organized ``` ### Kind Organizer (`organizers/kind.py`) Organizes ROMs by kind (Demo, Beta, Homebrew, etc.): - Detects kind markers in filename - Supports custom kind markers - Creates `By Kind//` structure **Detected Kinds:** - Demo (Demo, Kiosk) - Beta (Beta, Proto, Prototype) - Homebrew - Translation (T+, T-) - Unlicensed, Pirate, Hack, etc. **Example:** ```python from romfarmer.organizers import KindOrganizer, OrganizeMode organizer = KindOrganizer( mode=OrganizeMode.MOVE, keep_in_place=['Demo'], # Keep demos in place custom_markers={ 'Special': ['Special Edition', 'SE'] } ) stats = organizer.organize(Path('/roms/nes')) ``` **Result:** ``` /roms/nes/ Demo (USA) (Demo).nes <- Kept in place By Kind/ Beta/ Game (Beta).nes <- Organized Homebrew/ Game (Homebrew).nes <- Organized ``` ### Language Organizer (`organizers/language.py`) Organizes ROMs by language (typically with symlinks): - Detects language codes in filename - Multi-language priority support - Full language names or codes - Creates `By Language//` structure **Example:** ```python from romfarmer.organizers import LanguageOrganizer, OrganizeMode organizer = LanguageOrganizer( mode=OrganizeMode.SYMLINK, # Virtual organization exclude_languages=['En'], # English is default use_full_names=True # "English" instead of "En" ) stats = organizer.organize(Path('/roms/nes')) ``` **Result:** ``` /roms/nes/ Game (USA) (En).nes <- Original file Game (France) (Fr).nes <- Original file Game (Japan) (Ja).nes <- Original file By Language/ French/ Game (France) (Fr).nes <- Symlink Japanese/ Game (Japan) (Ja).nes <- Symlink # English excluded ``` ## CLI Commands ### Organize by Region ```bash # Keep USA and World in root, organize others romfarmer organize region /roms/nes --keep-in-place USA --keep-in-place World # Create symlinks instead of moving romfarmer organize region /roms/nes --mode symlink # Dry run to preview romfarmer organize region /roms/nes --dry-run # Process only specific extensions romfarmer organize region /roms/nes --extensions .nes --extensions .sfc ``` ### Organize by Kind ```bash # Keep demos in place, organize others romfarmer organize kind /roms/nes --keep-in-place Demo # Exclude pirate ROMs romfarmer organize kind /roms/nes --exclude Pirate # Copy instead of move romfarmer organize kind /roms/nes --mode copy ``` ### Organize by Language ```bash # Create language symlinks, exclude English romfarmer organize language /roms/nes --exclude En # Use language codes instead of full names romfarmer organize language /roms/nes --use-codes --exclude En # Move files instead of symlinking romfarmer organize language /roms/nes --mode move ``` ### Full Organization ```bash # Organize by region, kind, AND language in one command romfarmer organize all /roms/nes \ --region-keep USA \ --kind-keep Demo \ --lang-exclude En # This runs: # 1. Region organization (move files) # 2. Kind organization (move files recursively) # 3. Language organization (create symlinks) ``` ## Configuration Organization can be configured via `romfarmer.toml`: ```toml [organize] # Default mode (move, copy, symlink) default_mode = "move" # Default regions to keep in root keep_regions = ["USA", "World"] # Default kinds to keep in place keep_kinds = ["Demo"] # Languages to exclude from organization exclude_languages = ["En"] # Region priority for multi-region ROMs region_priority = ["USA", "Europe", "Japan", "World"] # Language priority for multi-language ROMs language_priority = ["En", "Es", "Fr", "De", "It", "Ja"] ``` ## Statistics All organizers return `OrganizeStats` with: - `files_processed` - Total files scanned - `files_moved` - Files moved - `files_copied` - Files copied - `symlinks_created` - Symlinks created - `directories_created` - Directories created - `skipped` - Files skipped (already organized) - `errors` - Errors encountered **Example:** ```python stats = organizer.organize(Path('/roms/nes')) print(stats) # Output: # Files processed: 150 # Files moved: 75 # Files copied: 0 # Symlinks created: 0 # Directories created: 5 # Files skipped: 0 # Errors: 0 ``` ## Testing Comprehensive test coverage (122 tests, 73% coverage): ```bash # Run all organizer tests pytest tests/test_organizers_*.py -v # Test specific organizer pytest tests/test_organizers_region.py -v pytest tests/test_organizers_kind.py -v pytest tests/test_organizers_language.py -v ``` ## Real-World Examples ### USA Build (Keep USA in root) ```bash romfarmer organize region /roms/nes --keep-in-place USA --keep-in-place World romfarmer organize kind /roms/nes --recursive ``` Result: ``` /roms/nes/ Super Mario Bros (USA).nes <- USA games in root By Region/ Europe/ Game (Europe).nes Japan/ Game (Japan).nes By Kind/ Demo/ Demo (USA) (Demo).nes ``` ### English Build (Language symlinks) ```bash romfarmer organize region /roms/nes --keep-in-place USA romfarmer organize kind /roms/nes --recursive romfarmer organize language /roms/nes --exclude En --mode symlink ``` Result: ``` /roms/nes/ Games (USA).nes By Region/ Europe/... Japan/... By Language/ French/ Game (Fr).nes -> ../../By Region/Europe/Game (Fr).nes Japanese/ Game (Ja).nes -> ../../By Region/Japan/Game (Ja).nes ``` ### All Regions Build (Everything organized) ```bash romfarmer organize all /roms/nes --lang-exclude En ``` Result: ``` /roms/nes/ By Region/ USA/... Europe/... Japan/... By Kind/ Demo/... Beta/... By Language/ French/... Japanese/... ``` ## Performance - **Fast**: Processes 1000+ ROMs in seconds - **Safe**: Dry-run mode for previewing changes - **Efficient**: Symlinks avoid file duplication - **Robust**: Handles errors gracefully with statistics ## Next Steps With organization complete, next phases: 1. **DAT Processing** - Parse DAT files for validation 2. **ROM Scanning** - Hash and match ROMs against DATs 3. **Validation** - Verify collection completeness 4. **Scraping** - Fetch metadata and artwork