refactor(pipeline): remove hardcodes and replace static geometry with layout anchors

This commit is contained in:
omkarande14
2026-09-28 17:13:23 +05:30
parent 6f3831ecdb
commit 3729100817
44 changed files with 9315 additions and 1 deletions
View File
+103
View File
@@ -0,0 +1,103 @@
# app/input/file_validator.py
from pathlib import Path
import pymupdf as fitz # PyMuPDF - for reading PDF files
from PIL import Image # Pillow - for reading image files
# The file types Milestone 1 supports.
SUPPORTED_EXTENSIONS = {".pdf", ".jpg", ".jpeg", ".png"}
def validate_file(file_path: str) -> dict:
"""
Checks a single file and reports whether it's a usable invoice input.
Returns a dictionary like:
{"status": "valid", "filename": "...", "format": "pdf", "page_count": 3}
or
{"status": "invalid", "filename": "...", "reason": "..."}
"""
path = Path(file_path)
filename = path.name
# Step 1: Does the file even exist?
if not path.exists():
return {
"status": "invalid",
"filename": filename,
"reason": "File does not exist",
}
# Step 2: Is the extension one we support?
extension = path.suffix.lower()
if extension not in SUPPORTED_EXTENSIONS:
return {
"status": "invalid",
"filename": filename,
"reason": f"Unsupported file format: {extension or 'no extension'}",
}
# Step 3: Handle PDFs
if extension == ".pdf":
return _validate_pdf(path, filename)
# Step 4: Handle images (.jpg, .jpeg, .png)
return _validate_image(path, filename, extension)
def _validate_pdf(path: Path, filename: str) -> dict:
try:
doc = fitz.open(path)
except Exception as e:
return {
"status": "invalid",
"filename": filename,
"reason": f"Could not open PDF (possibly corrupted): {e}",
}
if doc.is_encrypted:
# Some encrypted PDFs can still be opened with an empty password.
# If PyMuPDF couldn't authenticate, we treat it as unreadable.
if not doc.authenticate(""):
doc.close()
return {
"status": "invalid",
"filename": filename,
"reason": "PDF is password-protected/encrypted",
}
page_count = len(doc)
doc.close()
if page_count == 0:
return {
"status": "invalid",
"filename": filename,
"reason": "PDF has zero pages",
}
return {
"status": "valid",
"filename": filename,
"format": "pdf",
"page_count": page_count,
}
def _validate_image(path: Path, filename: str, extension: str) -> dict:
try:
with Image.open(path) as img:
img.verify() # checks the image isn't corrupted, without fully loading it
except Exception as e:
return {
"status": "invalid",
"filename": filename,
"reason": f"Could not open image (possibly corrupted): {e}",
}
return {
"status": "valid",
"filename": filename,
"format": extension.lstrip("."),
"page_count": 1,
}
+51
View File
@@ -0,0 +1,51 @@
# app/input/input_handler.py
from pathlib import Path
from app.input.file_validator import validate_file
def process_folder(folder_path: str) -> dict:
"""
Runs validate_file() on every file inside folder_path.
"""
folder = Path(folder_path)
if not folder.exists() or not folder.is_dir():
return {
"total_files": 0,
"valid_count": 0,
"invalid_count": 0,
"results": [],
"error": f"Folder not found: {folder_path}",
}
results = []
for item in sorted(folder.iterdir()):
if item.is_file() and not item.name.startswith("."):
result = validate_file(str(item))
results.append(result)
valid_count = sum(1 for r in results if r["status"] == "valid")
invalid_count = sum(1 for r in results if r["status"] == "invalid")
# New: breakdown by format, only counting valid files
format_counts = {}
total_pdf_pages = 0
for r in results:
if r["status"] == "valid":
fmt = r["format"]
format_counts[fmt] = format_counts.get(fmt, 0) + 1
if fmt == "pdf":
total_pdf_pages += r["page_count"]
return {
"total_files": len(results),
"valid_count": valid_count,
"invalid_count": invalid_count,
"format_breakdown": format_counts,
"total_pdf_pages": total_pdf_pages,
"results": results,
}