project_alpha / odt_evaluator.py
ayesha-21's picture
Upload odt_evaluator.py
ac83dbe verified
import os
import subprocess
import sys
ODT = "/home/user/Proposals/Project_Alpha_final.odt"
TMP_XML = "/tmp/content.xml"
def emit(msg: str):
print(msg)
# =================================================
# 1. File existence
# =================================================
if not os.path.isfile(ODT):
emit("FILE_MISSING")
sys.exit(0)
emit("FILE_OK")
# =================================================
# 2. Extract content.xml
# =================================================
try:
with open(TMP_XML, "w", encoding="utf-8") as out:
subprocess.run(
["unzip", "-p", ODT, "content.xml"],
stdout=out,
stderr=subprocess.DEVNULL,
check=True
)
except Exception:
emit("CONTENT_MISSING")
sys.exit(0)
emit("CONTENT_OK")
# =================================================
# 3. Load XML safely
# =================================================
with open(TMP_XML, "r", encoding="utf-8", errors="ignore") as f:
xml = f.read()
# Anti-reward-hacking: ensure document is not empty
if len(xml.strip()) < 800:
emit("CONTENT_MISSING")
sys.exit(0)
# =================================================
# 4. Manual page break (page-2 proxy)
# =================================================
page_break_pos = xml.find("<text:soft-page-break")
if page_break_pos != -1:
emit("PAGE_BREAK_AFTER_P1_OK")
else:
emit("PAGE_BREAK_MISSING")
# =================================================
# 5. Table of Contents checks
# =================================================
toc_pos = xml.find("<text:table-of-content")
# Presence
if toc_pos != -1:
emit("TOC_PRESENT_OK")
else:
emit("TOC_MISSING")
sys.exit(0) # no point checking further TOC properties
# Placement: must be after page break (proxy for page 2)
if not (page_break_pos != -1 and toc_pos > page_break_pos):
emit("TOC_NOT_ON_PAGE2")
# TOC title
if "Proposal Outline" in xml:
emit("TOC_TITLE_OK")
else:
emit("TOC_TITLE_BAD")
# TOC level restriction
if 'text:outline-level="1"' in xml:
emit("TOC_LEVEL_OK")
else:
emit("TOC_LEVEL_BAD")
# =================================================
# 6. Image checks
# =================================================
img_pos = xml.find("<draw:image")
if img_pos != -1:
emit("IMG_PRESENT_OK")
else:
emit("IMG_MISSING")
# Image must be below TOC
if img_pos != -1 and img_pos > toc_pos:
emit("IMG_BELOW_TOC_OK")
else:
emit("IMG_ABOVE_TOC")
# Anchor type
if 'text:anchor-type="as-char"' in xml:
emit("ANCHOR_OK")
else:
emit("ANCHOR_BAD")
# Width exactly 15 cm
if 'svg:width="15cm"' in xml or 'svg:width="150mm"' in xml:
emit("WIDTH_OK")
else:
emit("WIDTH_BAD")