Release v1.0.8: recursive folder scan and file type filters.
Adds subfolder inclusion and extension presets so the preview list can target nested media without loading everything. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -6,6 +6,7 @@ from __future__ import annotations
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from difflib import SequenceMatcher
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from .tvdb_client import TvdbEpisode
|
||||
@@ -201,7 +202,8 @@ def match_filenames_to_episodes(
|
||||
|
||||
file_entries: list[tuple[str, str, str, dict]] = []
|
||||
for name in filenames:
|
||||
stem = name.rsplit(".", 1)[0] if "." in name and not name.startswith(".") else name
|
||||
base = Path(name).name
|
||||
stem = base.rsplit(".", 1)[0] if "." in base and not base.startswith(".") else base
|
||||
parsed = parse_episode_stem(stem, pattern)
|
||||
if not parsed or not parsed["title"]:
|
||||
continue
|
||||
@@ -247,7 +249,11 @@ def match_filenames_to_episodes(
|
||||
name for name in filenames
|
||||
if name not in mapping
|
||||
and parse_episode_stem(
|
||||
name.rsplit(".", 1)[0] if "." in name and not name.startswith(".") else name,
|
||||
(
|
||||
Path(name).name.rsplit(".", 1)[0]
|
||||
if "." in Path(name).name and not Path(name).name.startswith(".")
|
||||
else Path(name).name
|
||||
),
|
||||
pattern,
|
||||
)
|
||||
]
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
"""Scan folders for rename candidates with optional recursion and extension filters."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from .pipeline import UNDO_FILENAME
|
||||
|
||||
FILE_TYPE_PRESETS: dict[str, str] = {
|
||||
"All files": "",
|
||||
"Video": ".mkv,.mp4,.avi,.webm,.m4v,.mov",
|
||||
"Audio": ".mp3,.flac,.wav,.ogg,.m4a,.aac",
|
||||
"Images": ".jpg,.jpeg,.png,.gif,.webp,.bmp",
|
||||
}
|
||||
|
||||
|
||||
def parse_extension_filter(spec: str) -> frozenset[str] | None:
|
||||
"""
|
||||
Parse a user extension filter into a set of lowercase extensions with a leading dot.
|
||||
Returns None when empty (match all files).
|
||||
"""
|
||||
spec = spec.strip()
|
||||
if not spec:
|
||||
return None
|
||||
exts: set[str] = set()
|
||||
for part in re.split(r"[,;\s]+", spec):
|
||||
part = part.strip().lower()
|
||||
if not part:
|
||||
continue
|
||||
if part.startswith("*."):
|
||||
part = part[1:]
|
||||
if not part.startswith("."):
|
||||
part = "." + part
|
||||
exts.add(part)
|
||||
return frozenset(exts) if exts else None
|
||||
|
||||
|
||||
def file_extension(rel_path: str) -> str:
|
||||
"""Return the lowercase extension (with dot) of the basename, or '' if none."""
|
||||
name = Path(rel_path).name
|
||||
if "." in name and not name.startswith("."):
|
||||
return "." + name.rsplit(".", 1)[-1].lower()
|
||||
return ""
|
||||
|
||||
|
||||
def matches_extension(rel_path: str, extensions: frozenset[str] | None) -> bool:
|
||||
if extensions is None:
|
||||
return True
|
||||
return file_extension(rel_path) in extensions
|
||||
|
||||
|
||||
def list_files(
|
||||
base_dir: str,
|
||||
*,
|
||||
recursive: bool = False,
|
||||
extensions: frozenset[str] | None = None,
|
||||
) -> list[str]:
|
||||
"""Return sorted paths relative to base_dir."""
|
||||
base = Path(base_dir)
|
||||
if not base.is_dir():
|
||||
return []
|
||||
|
||||
found: list[str] = []
|
||||
|
||||
def consider(path: Path) -> None:
|
||||
if not path.is_file():
|
||||
return
|
||||
if path.name.startswith("."):
|
||||
return
|
||||
if path.name == UNDO_FILENAME:
|
||||
return
|
||||
rel = path.relative_to(base).as_posix()
|
||||
if matches_extension(rel, extensions):
|
||||
found.append(rel)
|
||||
|
||||
if recursive:
|
||||
for dirpath, dirnames, filenames in os.walk(base):
|
||||
dirnames[:] = sorted(d for d in dirnames if not d.startswith("."))
|
||||
for fn in sorted(filenames):
|
||||
consider(Path(dirpath) / fn)
|
||||
else:
|
||||
for entry in sorted(base.iterdir(), key=lambda p: p.name.lower()):
|
||||
consider(entry)
|
||||
|
||||
return sorted(found, key=str.lower)
|
||||
+19
-7
@@ -25,13 +25,24 @@ def apply_pipeline(
|
||||
index: int,
|
||||
total: int,
|
||||
) -> str:
|
||||
"""Apply all enabled rules to a single filename (no path). Returns new filename."""
|
||||
stem, ext = _split_name(name)
|
||||
"""Apply all enabled rules to a filename or relative path. Returns new name/path."""
|
||||
rel = Path(name)
|
||||
if len(rel.parts) > 1:
|
||||
parent = rel.parent
|
||||
file_name = rel.name
|
||||
else:
|
||||
parent = None
|
||||
file_name = name
|
||||
|
||||
stem, ext = _split_name(file_name)
|
||||
for r in rules:
|
||||
if not r.enabled:
|
||||
continue
|
||||
stem, ext = r.apply(stem, ext, index, total, original_name=name)
|
||||
return stem + ext
|
||||
new_name = stem + ext
|
||||
if parent is not None:
|
||||
return str(parent / new_name)
|
||||
return new_name
|
||||
|
||||
|
||||
def compute_preview(
|
||||
@@ -77,11 +88,12 @@ def perform_renames(
|
||||
final_names = {n for _, n in step1}
|
||||
temp_map = []
|
||||
for i, (old_path, new_name) in enumerate(step1):
|
||||
temp_name = f"__temp_{i}_{old_path.name}{temp_suffix}"
|
||||
while temp_name in final_names or (base / temp_name).exists():
|
||||
new_path = base / new_name
|
||||
temp_path = old_path.parent / f"__temp_{i}_{old_path.name}{temp_suffix}"
|
||||
while temp_path.name in final_names or temp_path.exists() or temp_path == new_path:
|
||||
i += 1
|
||||
temp_name = f"__temp_{i}_{old_path.name}{temp_suffix}"
|
||||
temp_map.append((old_path, base / temp_name, base / new_name))
|
||||
temp_path = old_path.parent / f"__temp_{i}_{old_path.name}{temp_suffix}"
|
||||
temp_map.append((old_path, temp_path, new_path))
|
||||
|
||||
if dry_run:
|
||||
for old_p, temp_p, new_p in temp_map:
|
||||
|
||||
Reference in New Issue
Block a user