refactor
This commit is contained in:
+25
-20
@@ -1,8 +1,8 @@
|
||||
# processors/validator.py
|
||||
|
||||
import os
|
||||
import zipfile
|
||||
import rarfile
|
||||
from core.archive import detect_real_format
|
||||
from core.result import StepResult
|
||||
|
||||
|
||||
class ValidationResult:
|
||||
@@ -13,7 +13,16 @@ class ValidationResult:
|
||||
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"
|
||||
@@ -28,24 +37,8 @@ class ValidationResult:
|
||||
return msg
|
||||
|
||||
|
||||
def detect_real_format(path):
|
||||
"""Devuelve 'zip', 'rar' o None."""
|
||||
try:
|
||||
zipfile.ZipFile(path).close()
|
||||
return "zip"
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
rarfile.RarFile(path).close()
|
||||
return "rar"
|
||||
except:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def validate_comic(path):
|
||||
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
|
||||
@@ -64,3 +57,15 @@ def validate_comic(path):
|
||||
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),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user