Optimize multi-ROM exclusion matching pass

Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-05-21 18:51:56 +00:00
committed by GitHub
parent 5a1e238a5f
commit 98bc9a9eea
2 changed files with 38 additions and 9 deletions

View File

@@ -374,20 +374,36 @@ class FSRomsHandler(FSHandler):
def exclude_multi_roms(self, roms: list[str]) -> list[str]:
excluded_names = cm.get_config().EXCLUDED_MULTI_FILES
normalized_excluded = {
excluded_name.strip().lower() for excluded_name in excluded_names
}
excluded_files: set[str] = set()
normalized_patterns = [
excluded_name.strip().lower()
for excluded_name in excluded_names
if excluded_name.strip()
]
def has_wildcard(pattern: str) -> bool:
return any(char in pattern for char in ("*", "?", "["))
exact_matches = {
pattern for pattern in normalized_patterns if not has_wildcard(pattern)
}
wildcard_patterns = [
pattern for pattern in normalized_patterns if has_wildcard(pattern)
]
kept_roms: list[str] = []
for rom in roms:
normalized_rom_name = rom.strip().lower()
if normalized_rom_name in normalized_excluded or any(
fnmatch.fnmatch(normalized_rom_name, exc_name)
for exc_name in normalized_excluded
if normalized_rom_name in exact_matches:
continue
if any(
fnmatch.fnmatch(normalized_rom_name, pattern)
for pattern in wildcard_patterns
):
excluded_files.add(rom)
continue
return [f for f in roms if f not in excluded_files]
kept_roms.append(rom)
return kept_roms
def _build_rom_file(
self, rom: Rom, rom_path: Path, file_name: str, file_hash: FileHash

View File

@@ -273,6 +273,19 @@ class TestFSRomsHandler:
result = handler.exclude_multi_roms(roms)
assert result == ["Game1", "Game2"]
def test_exclude_multi_roms_wildcard_patterns(
self, handler: FSRomsHandler, config
):
"""Test exclude_multi_roms keeps wildcard matching with normalized config"""
roms = ["Game1", "Manuals", "manuals-fr", "Game2"]
config.EXCLUDED_MULTI_FILES = [" manuals* "]
with pytest.MonkeyPatch.context() as m:
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
result = handler.exclude_multi_roms(roms)
assert result == ["Game1", "Game2"]
def test_build_rom_file_single_file(self, rom_single: Rom, handler: FSRomsHandler):
"""Test _build_rom_file with actual single ROM file"""
rom_path = Path(rom_single.fs_path)