67 lines
1.6 KiB
Python
67 lines
1.6 KiB
Python
# processors/validator.py
|
|
|
|
import os
|
|
import zipfile
|
|
import rarfile
|
|
|
|
|
|
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 __str__(self):
|
|
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 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):
|
|
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
|