From 98bc9a9eeabb026b80e00a0602cbde7c7fdc2b1c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 21 May 2026 18:51:56 +0000 Subject: [PATCH] Optimize multi-ROM exclusion matching pass Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com> --- backend/handler/filesystem/roms_handler.py | 34 ++++++++++++++----- .../handler/filesystem/test_roms_handler.py | 13 +++++++ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/backend/handler/filesystem/roms_handler.py b/backend/handler/filesystem/roms_handler.py index f33e22840..b0bd46b36 100644 --- a/backend/handler/filesystem/roms_handler.py +++ b/backend/handler/filesystem/roms_handler.py @@ -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 diff --git a/backend/tests/handler/filesystem/test_roms_handler.py b/backend/tests/handler/filesystem/test_roms_handler.py index 9454be309..1b11c16fe 100644 --- a/backend/tests/handler/filesystem/test_roms_handler.py +++ b/backend/tests/handler/filesystem/test_roms_handler.py @@ -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)