Coverage for src/romfarmer/organizers/region.py: 0%
34 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-03 11:31 -0700
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-03 11:31 -0700
1"""
2Region-based ROM organizer.
4Organizes ROMs by region (USA, Europe, Japan, etc.) extracted from filenames.
5Supports No-Intro and Redump naming conventions.
6"""
8import logging
9import re
11from .base import BaseOrganizer, OrganizeMode
13logger = logging.getLogger(__name__)
16# Standard region codes from No-Intro
17REGION_CODES = [
18 "Argentina",
19 "Asia",
20 "Australia",
21 "Austria",
22 "Belgium",
23 "Brazil",
24 "Canada",
25 "China",
26 "Croatia",
27 "Czech Republic",
28 "Denmark",
29 "Europe",
30 "Finland",
31 "France",
32 "Germany",
33 "Greece",
34 "Hong Kong",
35 "Hungary",
36 "Ireland",
37 "Israel",
38 "Italy",
39 "Japan",
40 "Korea",
41 "Mexico",
42 "Netherlands",
43 "New Zealand",
44 "Norway",
45 "Poland",
46 "Portugal",
47 "Russia",
48 "Scandinavia",
49 "Singapore",
50 "Slovakia",
51 "South Africa",
52 "Spain",
53 "Sweden",
54 "Switzerland",
55 "Taiwan",
56 "Turkey",
57 "UK",
58 "Ukraine",
59 "USA",
60 "World",
61]
64class RegionOrganizer(BaseOrganizer):
65 """
66 Organize ROMs by region.
68 Extracts region information from ROM filenames and organizes them into
69 region-specific directories.
71 Examples:
72 >>> organizer = RegionOrganizer(
73 ... mode=OrganizeMode.MOVE,
74 ... keep_in_place=['USA', 'World']
75 ... )
76 >>> stats = organizer.organize(Path('/roms/nes'))
78 # Results in:
79 # /roms/nes/Super Mario Bros (USA).nes <- kept in place
80 # /roms/nes/By Region/Japan/Mario (Japan).nes <- organized
81 # /roms/nes/By Region/Europe/Mario (Europe).nes <- organized
83 Args:
84 mode: How to organize files (move, copy, or symlink)
85 dry_run: Preview changes without actually making them
86 region_priority: Preferred regions in order (for multi-region ROMs)
87 keep_in_place: Regions to keep in root directory
88 exclude_regions: Regions to exclude from organization
89 """
91 def __init__(
92 self,
93 mode: OrganizeMode = OrganizeMode.MOVE,
94 dry_run: bool = False,
95 region_priority: list[str] | None = None,
96 keep_in_place: list[str] | None = None,
97 exclude_regions: list[str] | None = None,
98 ):
99 super().__init__(
100 mode=mode,
101 dry_run=dry_run,
102 keep_in_place=keep_in_place,
103 exclude_values=exclude_regions,
104 )
105 self.region_priority = region_priority or ["USA", "Europe", "Japan", "World"]
107 # Validate region priority
108 for region in self.region_priority:
109 if region not in REGION_CODES:
110 logger.warning(f"Unknown region in priority list: {region}")
112 def get_organization_dir_name(self) -> str:
113 """Get the directory name for region organization."""
114 return "By Region"
116 def get_organization_value(self, filename: str) -> str | None:
117 """
118 Extract region from filename.
120 Supports formats like:
121 - (USA)
122 - (Europe)
123 - (USA, Europe)
124 - (Japan) (En)
126 For multi-region ROMs, returns the highest priority region.
128 Args:
129 filename: ROM filename
131 Returns:
132 Region code, or None if not detected
133 """
134 # Pattern to match regions in parentheses
135 # Matches: (USA), (Europe), (USA, Europe), etc.
136 pattern = r"\(([^)]+)\)"
138 matches = re.findall(pattern, filename)
140 if not matches:
141 return None
143 # Check each match for region codes
144 detected_regions = []
146 for match in matches:
147 # Split by comma for multi-region entries
148 parts = [p.strip() for p in match.split(",")]
150 for part in parts:
151 # Check if this part is a known region
152 if part in REGION_CODES:
153 detected_regions.append(part)
155 if not detected_regions:
156 return None
158 # If multiple regions detected, use priority
159 if len(detected_regions) > 1:
160 logger.debug(f"Multi-region ROM detected: {detected_regions}")
161 for priority_region in self.region_priority:
162 if priority_region in detected_regions:
163 logger.debug(f"Selected region: {priority_region} (priority)")
164 return priority_region
166 # Return first detected region
167 return detected_regions[0]