From b462c9fb1e090386b59bc9c766a5bf4755c5fbd3 Mon Sep 17 00:00:00 2001 From: Sergio Date: Fri, 20 Feb 2026 21:25:47 +0100 Subject: [PATCH] comprova la rura duplica els resultats de fitxers brossa mes info en --listar --aplanar --- core/pipeline.py | 52 +++++++++++++++++++++++--------------------- core/summary.py | 5 ++++- main.py | 42 ++++++++++++++++++++++------------- processors/checks.py | 2 +- 4 files changed, 59 insertions(+), 42 deletions(-) diff --git a/core/pipeline.py b/core/pipeline.py index 32c20e9..27ef763 100644 --- a/core/pipeline.py +++ b/core/pipeline.py @@ -53,26 +53,25 @@ class Pipeline: if step == "clean": trash_r = next((r for r in step_results if r.step == "check_trash"), None) foreign_r = next((r for r in step_results if r.step == "check_foreign"), None) - nested_r = next((r for r in step_results if r.step == "check_nested"), None) prefix = "Basura detectada: " items = [w.removeprefix(prefix) for w in (trash_r.warnings if trash_r else []) if w.startswith(prefix)] items += [w.removeprefix("Fichero extraño: ") for w in (foreign_r.warnings if foreign_r else [])] - flatten = False + return {"items": items} + + elif step == "flatten": + nested_r = next((r for r in step_results if r.step == "check_nested"), None) flatten_files: list[tuple[str, str]] = [] - if nested_r and nested_r.warnings: - w = nested_r.warnings[0] - if w.startswith("Imágenes en subdirectorio: "): - flatten = True - for entry in sorted(os.listdir(temp_dir)): - subpath = os.path.join(temp_dir, entry) - if not os.path.isdir(subpath): - continue - for root, _dirs, files in os.walk(subpath): - for f in sorted(files): - src_abs = os.path.join(root, f) - src_rel = os.path.relpath(src_abs, temp_dir) - flatten_files.append((src_rel, f)) - return {"items": items, "flatten": flatten, "flatten_files": flatten_files} + if nested_r and nested_r.warnings and nested_r.warnings[0].startswith("Imágenes en subdirectorio: "): + for entry in sorted(os.listdir(temp_dir)): + subpath = os.path.join(temp_dir, entry) + if not os.path.isdir(subpath): + continue + for root, _dirs, files in os.walk(subpath): + for f in sorted(files): + src_abs = os.path.join(root, f) + src_rel = os.path.relpath(src_abs, temp_dir) + flatten_files.append((src_rel, f)) + return {"flatten_files": flatten_files} elif step == "normalize_pages": renames = preview_normalize_pages(temp_dir) @@ -112,10 +111,10 @@ class Pipeline: if step == "clean": trash = next((r for r in step_results if r.step == "check_trash"), None) foreign = next((r for r in step_results if r.step == "check_foreign"), None) - nested = next((r for r in step_results if r.step == "check_nested"), None) if (trash and trash.warnings) or (foreign and foreign.warnings): return True - # Solo necesita extracción si es el caso aplanable (1 subdir) + if step == "flatten": + nested = next((r for r in step_results if r.step == "check_nested"), None) if nested and nested.warnings and nested.warnings[0].startswith("Imágenes en subdirectorio: "): return True return False @@ -188,18 +187,21 @@ class Pipeline: if "clean" in self.steps: preview = self._compute_preview("clean", temp_dir, step_results) - if preview.get("items") or preview.get("flatten"): + if preview.get("items"): if confirm_fn is None or confirm_fn("clean", preview): clean_result = clean_directory(temp_dir) step_results.append(clean_result) if clean_result.changed: any_changed = True - # Aplanar si caso simple - if preview.get("flatten"): - flat_result = flatten_directory(temp_dir) - step_results.append(flat_result) - if flat_result.changed: - any_changed = True + + if "flatten" in self.steps: + preview = self._compute_preview("flatten", temp_dir, step_results) + if preview.get("flatten_files"): + if confirm_fn is None or confirm_fn("flatten", preview): + flat_result = flatten_directory(temp_dir) + step_results.append(flat_result) + if flat_result.changed: + any_changed = True if "normalize_pages" in self.steps: preview = self._compute_preview("normalize_pages", temp_dir, step_results) diff --git a/core/summary.py b/core/summary.py index 74533c1..1cdbc03 100644 --- a/core/summary.py +++ b/core/summary.py @@ -55,6 +55,7 @@ class SummaryCollector: ) cleaned = count_step(["clean"]) + flattened = count_step(["flatten"]) pages_normalized = count_step(["normalize_pages"]) images_converted = count_step(["normalize_images", "convert_images"]) format_converted = count_step(["convert"]) @@ -75,6 +76,8 @@ class SummaryCollector: if modified: if cleaned: lines.append(f" · {'Limpiados':<{_SL}} : {cleaned:>{w}}") + if flattened: + lines.append(f" · {'Aplanados':<{_SL}} : {flattened:>{w}}") if pages_normalized: lines.append(f" · {'Páginas normalizadas':<{_SL}} : {pages_normalized:>{w}}") if images_converted: @@ -139,7 +142,7 @@ class SummaryCollector: ("Extensión incorrecta", "validate", lambda w: "Extensión incorrecta" in w, ["convert"], "convertido"), ("Basura detectada", "check_trash", lambda w: True, ["clean"], "limpiado"), ("Ficheros extraños", "check_foreign", lambda w: True, ["clean"], "limpiado"), - ("Estructura anidada", "check_nested", lambda w: w.startswith("Imágenes en subdirectorio: "), ["clean"], "aplanado"), + ("Estructura anidada", "check_nested", lambda w: w.startswith("Imágenes en subdirectorio: "), ["flatten"], "aplanado"), ("Estructura compleja", "check_nested", lambda w: w.startswith("Múltiples subdirectorios"), [], None), ("Numeración de páginas", "check_page_numbering", lambda w: True, ["normalize_pages"], "renumerado"), ("Imágenes mezcladas", "check_image_extensions", lambda w: True, ["normalize_images", "convert_images"], "normalizado"), diff --git a/main.py b/main.py index 8a9ec92..ce2138c 100644 --- a/main.py +++ b/main.py @@ -37,24 +37,22 @@ def _print_preview(step: str, preview: dict, formato: str) -> None: fmt = formato.upper() if step == "clean": - trash_items = [i for i in preview["items"] if not i.startswith("[Aplanar]")] - flatten_files = preview.get("flatten_files", []) - - if trash_items: + if preview["items"]: print("Ficheros a eliminar:") - for item in trash_items: + for item in preview["items"]: print(f" - {item}") + print(f"Formato final del archivo: {fmt}") - if flatten_files: - n = len(flatten_files) - display = flatten_files[:10] if n > 10 else flatten_files - col_w = max((len(src) for src, _ in display), default=0) + 2 - print(f"Aplanar estructura ({n} ficheros):") - for src, dst in display: - print(f" {src:<{col_w}} → {dst}") - if n > 10: - print(f" ... y {n - 10} más") - + elif step == "flatten": + flatten_files = preview["flatten_files"] + n = len(flatten_files) + display = flatten_files[:10] if n > 10 else flatten_files + col_w = max((len(src) for src, _ in display), default=0) + 2 + print(f"Aplanar estructura ({n} ficheros):") + for src, dst in display: + print(f" {src:<{col_w}} → {dst}") + if n > 10: + print(f" ... y {n - 10} más") print(f"Formato final del archivo: {fmt}") elif step == "normalize_pages": @@ -124,6 +122,7 @@ def parse_args(): parser.add_argument("--listar", action="store_true") parser.add_argument("--validar", action="store_true") parser.add_argument("--limpiar", action="store_true") + parser.add_argument("--aplanar", action="store_true") parser.add_argument("--convertir", action="store_true") parser.add_argument("--estandarizar", action="store_true") parser.add_argument("--formato", choices=["cbz", "cbr"], default="cbz") @@ -153,11 +152,22 @@ def parse_args(): def main(): args = parse_args() + if not os.path.isdir(args.ruta): + print(f"Error: la ruta '{args.ruta}' no existe o no es un directorio.", flush=True) + return + comic_files = find_comic_files(args.ruta) if args.listar: for f in comic_files: print(f) + ndirs = nfiles = 0 + for _, _, files in os.walk(args.ruta): + ndirs += 1 + nfiles += len(files) + print(f"\n Carpetas analizadas : {ndirs}") + print(f" Ficheros analizados : {nfiles}") + print(f" Cómics encontrados : {len(comic_files)}") if args.validar: pipeline = Pipeline(steps=[]) @@ -186,6 +196,8 @@ def main(): steps = [] if args.limpiar or args.estandarizar: steps.append("clean") + if args.aplanar or args.estandarizar: + steps.append("flatten") if args.renumerar or args.estandarizar: steps.append("normalize_pages") if args.uniformizar_imagenes or args.estandarizar: diff --git a/processors/checks.py b/processors/checks.py index d82ae9c..b73d076 100644 --- a/processors/checks.py +++ b/processors/checks.py @@ -115,7 +115,7 @@ def check_foreign(names: list[str]) -> StepResult: if not basename: continue ext = os.path.splitext(basename)[1].lower() - if ext not in IMAGE_EXTENSIONS and basename.lower() not in FOREIGN_ALLOWED: + if ext not in IMAGE_EXTENSIONS and basename.lower() not in FOREIGN_ALLOWED and basename.lower() not in TRASH_FILES: found.append(name) warnings = [f"Fichero extraño: {f}" for f in sorted(found)] return StepResult(step="check_foreign", changed=False, warnings=warnings)