refactor(clew): use predicate-based directory walking

This commit is contained in:
2026-09-07 18:15:39 -04:00
parent 3c68375a66
commit 246d7d5d0e

View File

@@ -92,22 +92,21 @@ def is_game_root(path: Path) -> bool:
) )
def find_game_roots(extracted_root: Path) -> list[Path]: def walk_directory(root: Path, predicate) -> list[Path]:
game_roots = [] matches = []
for root, directories, _files in os.walk(extracted_root): for current, _directories, files in os.walk(root):
path = Path(root) current_path = Path(current)
if is_game_root(path): if predicate(current_path):
game_roots.append(path) matches.append(current_path)
directories[:] = [] for file_name in files:
return sorted(game_roots) file_path = current_path / file_name
if predicate(file_path):
matches.append(file_path)
return sorted(matches)
def find_compiled_scripts(game_root: Path) -> list[Path]: def is_compiled_script(path: Path) -> bool:
scripts = [] return path.is_file() and path.suffix.lower() in {".rpyc", ".rpymc"}
game_directory = game_root / "game"
for suffix in ("*.rpyc", "*.rpymc"):
scripts.extend(game_directory.rglob(suffix))
return sorted(scripts)
def object_dump_output_path(output_root: Path, extracted_root: Path, game_root: Path, script: Path) -> Path: def object_dump_output_path(output_root: Path, extracted_root: Path, game_root: Path, script: Path) -> Path:
@@ -135,13 +134,13 @@ def run_renpy_object_dump(game_root: Path, script: Path, output: Path) -> None:
def dump_renpy_objects(output_root: Path, extracted_root: Path, overwrite: bool) -> None: def dump_renpy_objects(output_root: Path, extracted_root: Path, overwrite: bool) -> None:
game_roots = find_game_roots(extracted_root) game_roots = walk_directory(extracted_root, is_game_root)
if not game_roots: if not game_roots:
LOG.warning("no Ren'Py game roots found under %s", extracted_root) LOG.warning("no Ren'Py game roots found under %s", extracted_root)
return return
for game_root in game_roots: for game_root in game_roots:
scripts = find_compiled_scripts(game_root) scripts = walk_directory(game_root / "game", is_compiled_script)
LOG.info("found %d compiled Ren'Py scripts under %s", len(scripts), game_root) LOG.info("found %d compiled Ren'Py scripts under %s", len(scripts), game_root)
total = len(scripts) total = len(scripts)
for index, script in enumerate(scripts, start=1): for index, script in enumerate(scripts, start=1):