mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 02:22:44 +01:00
Address review: guard region_mode type and scope it to media
Non-string region_mode values (YAML list/mapping) now take the warn-and-fallback path instead of raising TypeError on the frozenset membership check during startup. get_preferred_regions only applies prefer_config reordering when selecting media (for_media=True); name and release-date selection keep the rom-tags-first ordering, matching the documented scope. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -762,7 +762,10 @@ class ConfigManager:
|
||||
log.critical("Invalid config.yml: scan.priority.region must be a list")
|
||||
sys.exit(3)
|
||||
|
||||
if self.config.SCAN_REGION_MODE not in VALID_SCAN_REGION_MODES:
|
||||
if (
|
||||
not isinstance(self.config.SCAN_REGION_MODE, str)
|
||||
or self.config.SCAN_REGION_MODE not in VALID_SCAN_REGION_MODES
|
||||
):
|
||||
log.warning(
|
||||
f"Unknown scan.priority.region_mode value "
|
||||
f"{self.config.SCAN_REGION_MODE!r}; falling back to "
|
||||
|
||||
@@ -75,7 +75,9 @@ def add_ss_auth_to_url(url: str | None) -> str:
|
||||
)
|
||||
|
||||
|
||||
def get_preferred_regions(rom: Rom | None = None) -> list[str]:
|
||||
def get_preferred_regions(
|
||||
rom: Rom | None = None, *, for_media: bool = False
|
||||
) -> list[str]:
|
||||
"""Get preferred regions, prepending the rom's own region tags when available.
|
||||
|
||||
When a rom is tagged with multiple regions (e.g. "(Japan, USA)"), the rom's
|
||||
@@ -84,9 +86,11 @@ def get_preferred_regions(rom: Rom | None = None) -> list[str]:
|
||||
Filename-tagged regions not present in the priority list keep their relative
|
||||
order and follow the prioritized ones.
|
||||
|
||||
With SCAN_REGION_MODE set to "prefer_config", the configured priority is
|
||||
authoritative instead: config regions come first and the rom's own tags
|
||||
become the fallback when the config regions have no media.
|
||||
With SCAN_REGION_MODE set to "prefer_config" and for_media=True, the
|
||||
configured priority is authoritative instead: config regions come first and
|
||||
the rom's own tags become the fallback when the config regions have no
|
||||
media. The mode only applies to media selection; name and release-date
|
||||
selection always keep the rom-tags-first ordering.
|
||||
"""
|
||||
config = cm.get_config()
|
||||
priority = config.SCAN_REGION_PRIORITY
|
||||
@@ -101,7 +105,7 @@ def get_preferred_regions(rom: Rom | None = None) -> list[str]:
|
||||
key=lambda code: priority.index(code) if code in priority else len(priority)
|
||||
)
|
||||
|
||||
if config.SCAN_REGION_MODE == "prefer_config":
|
||||
if for_media and config.SCAN_REGION_MODE == "prefer_config":
|
||||
ordered = priority + rom_codes
|
||||
else:
|
||||
ordered = rom_codes + priority
|
||||
@@ -274,7 +278,7 @@ def extract_media_from_ss_game(rom: Rom, game: SSGame) -> SSMetadataMedia:
|
||||
video_normalized_path=None,
|
||||
)
|
||||
|
||||
for region in get_preferred_regions(rom):
|
||||
for region in get_preferred_regions(rom, for_media=True):
|
||||
for media in game.get("medias", []):
|
||||
if media.get("region", "unk") != region or media.get("parent") != "jeu":
|
||||
continue
|
||||
|
||||
@@ -164,6 +164,18 @@ def test_forward_compat_unknown_values_are_tolerated():
|
||||
assert loader.config.SCAN_REGION_MODE == "prefer_rom_tags"
|
||||
|
||||
|
||||
def test_non_string_region_mode_falls_back_to_default(tmp_path):
|
||||
"""A list/mapping region_mode is unhashable and must not crash the
|
||||
membership check against the valid-modes set."""
|
||||
config_file = tmp_path / "config.yml"
|
||||
config_file.write_text(
|
||||
"scan:\n priority:\n region_mode:\n - prefer_config\n"
|
||||
)
|
||||
loader = ConfigManager(str(config_file))
|
||||
|
||||
assert loader.config.SCAN_REGION_MODE == "prefer_rom_tags"
|
||||
|
||||
|
||||
def test_malformed_yaml_falls_back_to_defaults():
|
||||
"""A YAML parse error should log critically and leave the app on
|
||||
defaults, not crash."""
|
||||
|
||||
@@ -126,7 +126,7 @@ class TestGetPreferredRegions:
|
||||
rom.regions = ["Europe"]
|
||||
config = _make_config(region_priority=["fr", "eu"], region_mode="prefer_config")
|
||||
with patch("handler.metadata.ss_handler.cm.get_config", return_value=config):
|
||||
regions = get_preferred_regions(rom)
|
||||
regions = get_preferred_regions(rom, for_media=True)
|
||||
|
||||
assert regions.index("fr") < regions.index("eu")
|
||||
|
||||
@@ -137,11 +137,22 @@ class TestGetPreferredRegions:
|
||||
rom.regions = ["Japan"]
|
||||
config = _make_config(region_priority=["fr"], region_mode="prefer_config")
|
||||
with patch("handler.metadata.ss_handler.cm.get_config", return_value=config):
|
||||
regions = get_preferred_regions(rom)
|
||||
regions = get_preferred_regions(rom, for_media=True)
|
||||
|
||||
assert regions.index("fr") < regions.index("jp")
|
||||
assert regions.index("jp") < regions.index("us")
|
||||
|
||||
def test_prefer_config_mode_only_applies_to_media(self):
|
||||
"""region_mode only affects media selection; name/date ordering keeps
|
||||
the rom's own tags first."""
|
||||
rom = MagicMock()
|
||||
rom.regions = ["Europe"]
|
||||
config = _make_config(region_priority=["fr", "eu"], region_mode="prefer_config")
|
||||
with patch("handler.metadata.ss_handler.cm.get_config", return_value=config):
|
||||
regions = get_preferred_regions(rom)
|
||||
|
||||
assert regions.index("eu") < regions.index("fr")
|
||||
|
||||
def test_default_mode_rom_tags_still_win(self):
|
||||
"""Default prefer_rom_tags mode keeps the current behavior."""
|
||||
rom = MagicMock()
|
||||
|
||||
Reference in New Issue
Block a user