129 lines
4.2 KiB
Python
129 lines
4.2 KiB
Python
# main.py
|
|
|
|
import argparse
|
|
from core.scanner import find_comic_files
|
|
from core.pipeline import Pipeline
|
|
from core.summary import SummaryCollector
|
|
|
|
_COL_W = 30
|
|
_SEP = "─" * 44
|
|
|
|
|
|
def _print_preview(step: str, preview: dict, formato: str) -> None:
|
|
fmt = formato.upper()
|
|
|
|
if step == "clean":
|
|
print("Ficheros basura a eliminar:")
|
|
for item in preview["items"]:
|
|
print(f" - {item}")
|
|
print(f"Formato final del archivo: {fmt}")
|
|
|
|
elif step == "normalize_pages":
|
|
renames = preview["renames"]
|
|
n = len(renames)
|
|
print(f"Renombrado de páginas ({n} páginas):")
|
|
print(f" {'Nombre actual':<{_COL_W}} → Nombre final")
|
|
print(f" {_SEP}")
|
|
display = renames[:10] if n > 20 else renames
|
|
for orig, final in display:
|
|
print(f" {orig:<{_COL_W}} {final}")
|
|
if n > 20:
|
|
print(f" ... y {n - 10} más")
|
|
print(f"Formato final del archivo: {fmt}")
|
|
|
|
elif step in ("normalize_images", "convert_images"):
|
|
conversions = preview["conversions"]
|
|
target_ext = preview["target_ext"].lstrip(".")
|
|
lossless = " (sin pérdida)" if target_ext.lower() == "png" else ""
|
|
print(f"Conversión de imágenes a {target_ext.upper()}{lossless}:")
|
|
print(f" {'Imagen actual':<{_COL_W}} → Imagen final")
|
|
print(f" {_SEP}")
|
|
for orig, final in conversions:
|
|
print(f" {orig:<{_COL_W}} {final}")
|
|
print(f"Formato final del archivo: {fmt}")
|
|
|
|
|
|
def make_confirm_fn(args):
|
|
def confirm_fn(step: str, preview: dict) -> bool:
|
|
if step == "convert":
|
|
return True # No destructivo, no necesita confirmación
|
|
print()
|
|
_print_preview(step, preview, args.formato)
|
|
answer = input("¿Aplicar? [s/N] ").strip().lower()
|
|
return answer in ("s", "si", "sí", "y", "yes")
|
|
return confirm_fn
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser(description="Gestor de cómics CBR/CBZ")
|
|
|
|
parser.add_argument("--ruta", required=True)
|
|
parser.add_argument("--listar", action="store_true")
|
|
parser.add_argument("--validar", action="store_true")
|
|
parser.add_argument("--limpiar", 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")
|
|
parser.add_argument("--renumerar", action="store_true")
|
|
parser.add_argument("--uniformizar-imagenes", action="store_true")
|
|
parser.add_argument("--convertir-imagenes", action="store_true")
|
|
parser.add_argument("--formato-imagen", choices=["jpg", "png", "webp"], default="png")
|
|
|
|
parser.add_argument("--no-preguntar", action="store_true")
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
def main():
|
|
args = parse_args()
|
|
|
|
comic_files = find_comic_files(args.ruta)
|
|
|
|
if args.listar:
|
|
for f in comic_files:
|
|
print(f)
|
|
|
|
if args.validar:
|
|
pipeline = Pipeline(steps=[])
|
|
collector = SummaryCollector()
|
|
for f in comic_files:
|
|
result = pipeline.run(f)
|
|
collector.add(result)
|
|
if result.has_issues():
|
|
print(result.full_report())
|
|
print()
|
|
print(collector.render())
|
|
return
|
|
|
|
# --- Construir steps ---
|
|
steps = []
|
|
if args.limpiar or args.estandarizar:
|
|
steps.append("clean")
|
|
if args.renumerar or args.estandarizar:
|
|
steps.append("normalize_pages")
|
|
if args.uniformizar_imagenes or args.estandarizar:
|
|
steps.append("normalize_images")
|
|
if args.convertir_imagenes:
|
|
steps.append("convert_images")
|
|
if args.convertir or args.estandarizar:
|
|
steps.append("convert")
|
|
|
|
if steps:
|
|
confirm_fn = None if args.no_preguntar else make_confirm_fn(args)
|
|
pipeline = Pipeline(
|
|
steps=steps,
|
|
desired_format=args.formato,
|
|
desired_image_format="." + args.formato_imagen,
|
|
)
|
|
collector = SummaryCollector()
|
|
for f in comic_files:
|
|
print(f"\n=== {f} ===")
|
|
result = pipeline.run(f, confirm_fn=confirm_fn)
|
|
print(result.summary())
|
|
collector.add(result)
|
|
print(f"\n{collector.render()}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|