mirror of
https://github.com/rommapp/romm.git
synced 2026-06-28 06:46:00 +00:00
Simplify extension exclusion to use ends-with check instead of sub-extension iteration
Agent-Logs-Url: https://github.com/rommapp/romm/sessions/a81b2023-a243-4721-bc5e-c6fa1a473a79 Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
55cd0cfc4f
commit
101629628e
@@ -205,19 +205,6 @@ class FSHandler:
|
||||
match = EXTENSION_REGEX.search(file_name)
|
||||
return match.group(1) if match else ""
|
||||
|
||||
def iter_file_extensions(self, file_name: str) -> list[str]:
|
||||
"""Return all right-anchored sub-extensions for a filename.
|
||||
|
||||
For "game.nds.enc.hash.txt" this yields:
|
||||
["nds.enc.hash.txt", "enc.hash.txt", "hash.txt", "txt"]
|
||||
This allows exclusion rules like "hash.txt" to match multi-dot filenames.
|
||||
"""
|
||||
ext = self.parse_file_extension(file_name)
|
||||
if not ext:
|
||||
return []
|
||||
parts = ext.split(".")
|
||||
return [".".join(parts[i:]) for i in range(len(parts))]
|
||||
|
||||
def extract_uuid_v4_from_filename(self, file_name: str) -> str:
|
||||
match = UUID_V4_REGEX.search(file_name)
|
||||
return match.group(0) if match else ""
|
||||
@@ -228,11 +215,12 @@ class FSHandler:
|
||||
excluded_files: list[str] = []
|
||||
|
||||
for file_name in files:
|
||||
# Check all right-anchored sub-extensions so that rules like "hash.txt"
|
||||
# match multi-dot filenames such as "game.nds.enc.hash.txt".
|
||||
# Check whether the filename ends with any excluded extension entry.
|
||||
# Using ends-with handles both simple rules ("txt") and compound rules
|
||||
# ("hash.txt") against multi-dot filenames like "game.nds.enc.hash.txt".
|
||||
if any(
|
||||
e.lower() in excluded_extensions
|
||||
for e in self.iter_file_extensions(file_name)
|
||||
file_name.lower().endswith("." + ext.lower())
|
||||
for ext in excluded_extensions
|
||||
):
|
||||
excluded_files.append(file_name)
|
||||
|
||||
|
||||
@@ -456,11 +456,11 @@ class FSRomsHandler(FSHandler):
|
||||
f"{abs_fs_path}/{rom.fs_name}", recursive=True
|
||||
):
|
||||
# Check if file is excluded by extension.
|
||||
# Check all right-anchored sub-extensions so that rules like "hash.txt"
|
||||
# match multi-dot filenames such as "game.nds.enc.hash.txt".
|
||||
# Using ends-with handles both simple rules ("txt") and compound
|
||||
# rules ("hash.txt") for multi-dot filenames like "game.nds.enc.hash.txt".
|
||||
if any(
|
||||
e.lower() in excluded_file_exts
|
||||
for e in self.iter_file_extensions(file_name)
|
||||
file_name.lower().endswith("." + ext.lower())
|
||||
for ext in excluded_file_exts
|
||||
):
|
||||
continue
|
||||
|
||||
|
||||
@@ -145,22 +145,6 @@ class TestFSHandler:
|
||||
assert handler.parse_file_extension("no_extension") == ""
|
||||
assert handler.parse_file_extension("file.with.dots.txt") == "with.dots.txt"
|
||||
|
||||
def test_iter_file_extensions(self, handler: FSHandler):
|
||||
"""Test that all right-anchored sub-extensions are returned"""
|
||||
assert handler.iter_file_extensions("game.nds") == ["nds"]
|
||||
assert handler.iter_file_extensions("game.nds.hash.txt") == [
|
||||
"nds.hash.txt",
|
||||
"hash.txt",
|
||||
"txt",
|
||||
]
|
||||
assert handler.iter_file_extensions("game.nds.enc.hash.txt") == [
|
||||
"nds.enc.hash.txt",
|
||||
"enc.hash.txt",
|
||||
"hash.txt",
|
||||
"txt",
|
||||
]
|
||||
assert handler.iter_file_extensions("no_extension") == []
|
||||
|
||||
def test_exclude_single_files(self, handler: FSHandler):
|
||||
"""Test file exclusion functionality"""
|
||||
files = ["test.txt", "game.rom", "excluded.tmp", "data.json"]
|
||||
|
||||
Reference in New Issue
Block a user