comprova la rura
duplica els resultats de fitxers brossa mes info en --listar --aplanar
This commit is contained in:
+27
-25
@@ -53,26 +53,25 @@ class Pipeline:
|
|||||||
if step == "clean":
|
if step == "clean":
|
||||||
trash_r = next((r for r in step_results if r.step == "check_trash"), None)
|
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)
|
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: "
|
prefix = "Basura detectada: "
|
||||||
items = [w.removeprefix(prefix) for w in (trash_r.warnings if trash_r else []) if w.startswith(prefix)]
|
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 [])]
|
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]] = []
|
flatten_files: list[tuple[str, str]] = []
|
||||||
if nested_r and nested_r.warnings:
|
if nested_r and nested_r.warnings and nested_r.warnings[0].startswith("Imágenes en subdirectorio: "):
|
||||||
w = nested_r.warnings[0]
|
for entry in sorted(os.listdir(temp_dir)):
|
||||||
if w.startswith("Imágenes en subdirectorio: "):
|
subpath = os.path.join(temp_dir, entry)
|
||||||
flatten = True
|
if not os.path.isdir(subpath):
|
||||||
for entry in sorted(os.listdir(temp_dir)):
|
continue
|
||||||
subpath = os.path.join(temp_dir, entry)
|
for root, _dirs, files in os.walk(subpath):
|
||||||
if not os.path.isdir(subpath):
|
for f in sorted(files):
|
||||||
continue
|
src_abs = os.path.join(root, f)
|
||||||
for root, _dirs, files in os.walk(subpath):
|
src_rel = os.path.relpath(src_abs, temp_dir)
|
||||||
for f in sorted(files):
|
flatten_files.append((src_rel, f))
|
||||||
src_abs = os.path.join(root, f)
|
return {"flatten_files": flatten_files}
|
||||||
src_rel = os.path.relpath(src_abs, temp_dir)
|
|
||||||
flatten_files.append((src_rel, f))
|
|
||||||
return {"items": items, "flatten": flatten, "flatten_files": flatten_files}
|
|
||||||
|
|
||||||
elif step == "normalize_pages":
|
elif step == "normalize_pages":
|
||||||
renames = preview_normalize_pages(temp_dir)
|
renames = preview_normalize_pages(temp_dir)
|
||||||
@@ -112,10 +111,10 @@ class Pipeline:
|
|||||||
if step == "clean":
|
if step == "clean":
|
||||||
trash = next((r for r in step_results if r.step == "check_trash"), None)
|
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)
|
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):
|
if (trash and trash.warnings) or (foreign and foreign.warnings):
|
||||||
return True
|
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: "):
|
if nested and nested.warnings and nested.warnings[0].startswith("Imágenes en subdirectorio: "):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
@@ -188,18 +187,21 @@ class Pipeline:
|
|||||||
|
|
||||||
if "clean" in self.steps:
|
if "clean" in self.steps:
|
||||||
preview = self._compute_preview("clean", temp_dir, step_results)
|
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):
|
if confirm_fn is None or confirm_fn("clean", preview):
|
||||||
clean_result = clean_directory(temp_dir)
|
clean_result = clean_directory(temp_dir)
|
||||||
step_results.append(clean_result)
|
step_results.append(clean_result)
|
||||||
if clean_result.changed:
|
if clean_result.changed:
|
||||||
any_changed = True
|
any_changed = True
|
||||||
# Aplanar si caso simple
|
|
||||||
if preview.get("flatten"):
|
if "flatten" in self.steps:
|
||||||
flat_result = flatten_directory(temp_dir)
|
preview = self._compute_preview("flatten", temp_dir, step_results)
|
||||||
step_results.append(flat_result)
|
if preview.get("flatten_files"):
|
||||||
if flat_result.changed:
|
if confirm_fn is None or confirm_fn("flatten", preview):
|
||||||
any_changed = True
|
flat_result = flatten_directory(temp_dir)
|
||||||
|
step_results.append(flat_result)
|
||||||
|
if flat_result.changed:
|
||||||
|
any_changed = True
|
||||||
|
|
||||||
if "normalize_pages" in self.steps:
|
if "normalize_pages" in self.steps:
|
||||||
preview = self._compute_preview("normalize_pages", temp_dir, step_results)
|
preview = self._compute_preview("normalize_pages", temp_dir, step_results)
|
||||||
|
|||||||
+4
-1
@@ -55,6 +55,7 @@ class SummaryCollector:
|
|||||||
)
|
)
|
||||||
|
|
||||||
cleaned = count_step(["clean"])
|
cleaned = count_step(["clean"])
|
||||||
|
flattened = count_step(["flatten"])
|
||||||
pages_normalized = count_step(["normalize_pages"])
|
pages_normalized = count_step(["normalize_pages"])
|
||||||
images_converted = count_step(["normalize_images", "convert_images"])
|
images_converted = count_step(["normalize_images", "convert_images"])
|
||||||
format_converted = count_step(["convert"])
|
format_converted = count_step(["convert"])
|
||||||
@@ -75,6 +76,8 @@ class SummaryCollector:
|
|||||||
if modified:
|
if modified:
|
||||||
if cleaned:
|
if cleaned:
|
||||||
lines.append(f" · {'Limpiados':<{_SL}} : {cleaned:>{w}}")
|
lines.append(f" · {'Limpiados':<{_SL}} : {cleaned:>{w}}")
|
||||||
|
if flattened:
|
||||||
|
lines.append(f" · {'Aplanados':<{_SL}} : {flattened:>{w}}")
|
||||||
if pages_normalized:
|
if pages_normalized:
|
||||||
lines.append(f" · {'Páginas normalizadas':<{_SL}} : {pages_normalized:>{w}}")
|
lines.append(f" · {'Páginas normalizadas':<{_SL}} : {pages_normalized:>{w}}")
|
||||||
if images_converted:
|
if images_converted:
|
||||||
@@ -139,7 +142,7 @@ class SummaryCollector:
|
|||||||
("Extensión incorrecta", "validate", lambda w: "Extensión incorrecta" in w, ["convert"], "convertido"),
|
("Extensión incorrecta", "validate", lambda w: "Extensión incorrecta" in w, ["convert"], "convertido"),
|
||||||
("Basura detectada", "check_trash", lambda w: True, ["clean"], "limpiado"),
|
("Basura detectada", "check_trash", lambda w: True, ["clean"], "limpiado"),
|
||||||
("Ficheros extraños", "check_foreign", 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),
|
("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"),
|
("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"),
|
("Imágenes mezcladas", "check_image_extensions", lambda w: True, ["normalize_images", "convert_images"], "normalizado"),
|
||||||
|
|||||||
@@ -37,24 +37,22 @@ def _print_preview(step: str, preview: dict, formato: str) -> None:
|
|||||||
fmt = formato.upper()
|
fmt = formato.upper()
|
||||||
|
|
||||||
if step == "clean":
|
if step == "clean":
|
||||||
trash_items = [i for i in preview["items"] if not i.startswith("[Aplanar]")]
|
if preview["items"]:
|
||||||
flatten_files = preview.get("flatten_files", [])
|
|
||||||
|
|
||||||
if trash_items:
|
|
||||||
print("Ficheros a eliminar:")
|
print("Ficheros a eliminar:")
|
||||||
for item in trash_items:
|
for item in preview["items"]:
|
||||||
print(f" - {item}")
|
print(f" - {item}")
|
||||||
|
print(f"Formato final del archivo: {fmt}")
|
||||||
|
|
||||||
if flatten_files:
|
elif step == "flatten":
|
||||||
n = len(flatten_files)
|
flatten_files = preview["flatten_files"]
|
||||||
display = flatten_files[:10] if n > 10 else flatten_files
|
n = len(flatten_files)
|
||||||
col_w = max((len(src) for src, _ in display), default=0) + 2
|
display = flatten_files[:10] if n > 10 else flatten_files
|
||||||
print(f"Aplanar estructura ({n} ficheros):")
|
col_w = max((len(src) for src, _ in display), default=0) + 2
|
||||||
for src, dst in display:
|
print(f"Aplanar estructura ({n} ficheros):")
|
||||||
print(f" {src:<{col_w}} → {dst}")
|
for src, dst in display:
|
||||||
if n > 10:
|
print(f" {src:<{col_w}} → {dst}")
|
||||||
print(f" ... y {n - 10} más")
|
if n > 10:
|
||||||
|
print(f" ... y {n - 10} más")
|
||||||
print(f"Formato final del archivo: {fmt}")
|
print(f"Formato final del archivo: {fmt}")
|
||||||
|
|
||||||
elif step == "normalize_pages":
|
elif step == "normalize_pages":
|
||||||
@@ -124,6 +122,7 @@ def parse_args():
|
|||||||
parser.add_argument("--listar", action="store_true")
|
parser.add_argument("--listar", action="store_true")
|
||||||
parser.add_argument("--validar", action="store_true")
|
parser.add_argument("--validar", action="store_true")
|
||||||
parser.add_argument("--limpiar", 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("--convertir", action="store_true")
|
||||||
parser.add_argument("--estandarizar", action="store_true")
|
parser.add_argument("--estandarizar", action="store_true")
|
||||||
parser.add_argument("--formato", choices=["cbz", "cbr"], default="cbz")
|
parser.add_argument("--formato", choices=["cbz", "cbr"], default="cbz")
|
||||||
@@ -153,11 +152,22 @@ def parse_args():
|
|||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
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)
|
comic_files = find_comic_files(args.ruta)
|
||||||
|
|
||||||
if args.listar:
|
if args.listar:
|
||||||
for f in comic_files:
|
for f in comic_files:
|
||||||
print(f)
|
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:
|
if args.validar:
|
||||||
pipeline = Pipeline(steps=[])
|
pipeline = Pipeline(steps=[])
|
||||||
@@ -186,6 +196,8 @@ def main():
|
|||||||
steps = []
|
steps = []
|
||||||
if args.limpiar or args.estandarizar:
|
if args.limpiar or args.estandarizar:
|
||||||
steps.append("clean")
|
steps.append("clean")
|
||||||
|
if args.aplanar or args.estandarizar:
|
||||||
|
steps.append("flatten")
|
||||||
if args.renumerar or args.estandarizar:
|
if args.renumerar or args.estandarizar:
|
||||||
steps.append("normalize_pages")
|
steps.append("normalize_pages")
|
||||||
if args.uniformizar_imagenes or args.estandarizar:
|
if args.uniformizar_imagenes or args.estandarizar:
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ def check_foreign(names: list[str]) -> StepResult:
|
|||||||
if not basename:
|
if not basename:
|
||||||
continue
|
continue
|
||||||
ext = os.path.splitext(basename)[1].lower()
|
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)
|
found.append(name)
|
||||||
warnings = [f"Fichero extraño: {f}" for f in sorted(found)]
|
warnings = [f"Fichero extraño: {f}" for f in sorted(found)]
|
||||||
return StepResult(step="check_foreign", changed=False, warnings=warnings)
|
return StepResult(step="check_foreign", changed=False, warnings=warnings)
|
||||||
|
|||||||
Reference in New Issue
Block a user