This commit is contained in:
2026-02-20 13:54:25 +01:00
parent 448c50b846
commit 8d83d27bd3
5 changed files with 120 additions and 8 deletions
+31 -2
View File
@@ -108,9 +108,12 @@ def check_foreign(names: list[str]) -> StepResult:
"""Detecta ficheros que no son imágenes ni metadata permitida."""
found = []
for name in names:
basename = name.replace("\\", "/").rstrip("/").rsplit("/", 1)[-1]
normalized = name.replace("\\", "/")
if normalized.endswith("/"):
continue # entrada de directorio — ignorar siempre
basename = normalized.rsplit("/", 1)[-1]
if not basename:
continue # entrada de directorio
continue
ext = os.path.splitext(basename)[1].lower()
if ext not in IMAGE_EXTENSIONS and basename.lower() not in FOREIGN_ALLOWED:
found.append(name)
@@ -118,6 +121,32 @@ def check_foreign(names: list[str]) -> StepResult:
return StepResult(step="check_foreign", changed=False, warnings=warnings)
def check_nested(names: list[str]) -> StepResult:
"""Detecta imágenes en subdirectorios en lugar de en la raíz del archivo."""
subdirs_with_images: set[str] = set()
for name in names:
normalized = name.replace("\\", "/")
if normalized.endswith("/"):
continue
parts = normalized.split("/")
if len(parts) < 2:
continue # fichero en raíz
ext = os.path.splitext(parts[-1])[1].lower()
if ext in IMAGE_EXTENSIONS:
subdirs_with_images.add(parts[0])
if not subdirs_with_images:
return StepResult(step="check_nested", changed=False)
if len(subdirs_with_images) == 1:
subdir = next(iter(subdirs_with_images))
warnings = [f"Imágenes en subdirectorio: {subdir}/"]
else:
listing = ", ".join(sorted(subdirs_with_images))
warnings = [f"Múltiples subdirectorios con imágenes: {listing}"]
return StepResult(step="check_nested", changed=False, warnings=warnings)
def check_comicinfo(names: list[str]) -> StepResult:
"""Detecta ausencia de ComicInfo.xml."""
found = any(
+36
View File
@@ -30,9 +30,45 @@ def clean_directory(work_dir: str) -> StepResult:
shutil.rmtree(full, ignore_errors=True)
removed.append(os.path.relpath(full, work_dir) + "/")
# Eliminar subdirectorios que hayan quedado vacíos
for root, dirs, files in os.walk(work_dir, topdown=False):
if root == work_dir:
continue
if not os.listdir(root):
os.rmdir(root)
removed.append(os.path.relpath(root, work_dir) + "/")
details = [f"Eliminado: {r}" for r in removed]
return StepResult(
step="clean",
changed=bool(removed),
details=details,
)
def flatten_directory(work_dir: str) -> StepResult:
"""
Mueve imágenes de un único subdirectorio a la raíz de work_dir.
Precondición: solo existe 1 subdir con imágenes (validado antes de llamar).
"""
moved = []
for entry in os.listdir(work_dir):
subdir = os.path.join(work_dir, entry)
if not os.path.isdir(subdir):
continue
for root, _dirs, files in os.walk(subdir):
for f in files:
src = os.path.join(root, f)
dst = os.path.join(work_dir, f)
if os.path.exists(dst):
base, ext = os.path.splitext(f)
counter = 1
while os.path.exists(dst):
dst = os.path.join(work_dir, f"{base}_{counter}{ext}")
counter += 1
shutil.move(src, dst)
moved.append(f)
shutil.rmtree(subdir, ignore_errors=True)
details = [f"Aplanado: {f}" for f in moved]
return StepResult(step="flatten", changed=bool(moved), details=details)