72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
# processors/validator.py
|
|
|
|
import os
|
|
from core.archive import detect_real_format
|
|
from core.result import StepResult
|
|
|
|
|
|
class ValidationResult:
|
|
def __init__(self, path):
|
|
self.path = path
|
|
self.real_format = None # "zip", "rar", None
|
|
self.extension = None # ".cbz" o ".cbr"
|
|
self.errors = []
|
|
self.warnings = []
|
|
|
|
def summary(self):
|
|
"""Resumen compacto para mostrar en consola."""
|
|
if self.errors:
|
|
return f"ERROR: {', '.join(self.errors)}"
|
|
if self.warnings:
|
|
return f"AVISO: {', '.join(self.warnings)}"
|
|
return "OK"
|
|
|
|
def __str__(self):
|
|
"""Salida detallada (solo si el usuario la pide)."""
|
|
msg = f"Validación de: {self.path}\n"
|
|
msg += f" Formato real: {self.real_format}\n"
|
|
msg += f" Extensión: {self.extension}\n"
|
|
if self.errors:
|
|
msg += " Errores:\n"
|
|
for e in self.errors:
|
|
msg += f" - {e}\n"
|
|
if self.warnings:
|
|
msg += " Avisos:\n"
|
|
for w in self.warnings:
|
|
msg += f" - {w}\n"
|
|
return msg
|
|
|
|
|
|
def validate_comic(path) -> ValidationResult:
|
|
"""Modo standalone: comprueba formato e integridad. Para --validar en CLI."""
|
|
result = ValidationResult(path)
|
|
ext = os.path.splitext(path)[1].lower()
|
|
result.extension = ext
|
|
|
|
real = detect_real_format(path)
|
|
result.real_format = real
|
|
|
|
if real is None:
|
|
result.errors.append("Archivo corrupto o ilegible")
|
|
return result
|
|
|
|
if ext == ".cbz" and real == "rar":
|
|
result.warnings.append("Extensión incorrecta: debería ser .cbr")
|
|
|
|
if ext == ".cbr" and real == "zip":
|
|
result.warnings.append("Extensión incorrecta: debería ser .cbz")
|
|
|
|
return result
|
|
|
|
|
|
def validate_archive(path: str) -> StepResult:
|
|
"""Para el pipeline: devuelve StepResult con errores/avisos de validación."""
|
|
vr = validate_comic(path)
|
|
return StepResult(
|
|
step="validate",
|
|
changed=False,
|
|
details=[f"Formato real: {vr.real_format}, extensión: {vr.extension}"],
|
|
warnings=list(vr.warnings),
|
|
errors=list(vr.errors),
|
|
)
|