First public release, published as one snapshot. What changed between versions is in CHANGELOG.md. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
"""Reader stub for the file-type gate: always answers the first place in the file, and never hidden.
|
|
|
|
First page (pdf), first paragraph (docx), cell A1 on the first sheet in workbook order (xlsx), first slide outside
|
|
the notes (pptx), first line (md, txt, html). It shows how much of the gate a reader passes without reading.
|
|
|
|
Usage: python3 gates/stubs/first_locator.py {file} {queries}
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
import zipfile
|
|
from pathlib import Path
|
|
from typing import Any
|
|
from xml.etree import ElementTree
|
|
|
|
MAIN_NS = "{http://schemas.openxmlformats.org/spreadsheetml/2006/main}"
|
|
|
|
|
|
def first_sheet(path: Path) -> str:
|
|
with zipfile.ZipFile(path) as z:
|
|
root = ElementTree.fromstring(z.read("xl/workbook.xml"))
|
|
sheet = root.find(f"{MAIN_NS}sheets/{MAIN_NS}sheet")
|
|
return "" if sheet is None else sheet.get("name", "")
|
|
|
|
|
|
def first_locator(path: Path) -> dict[str, Any]:
|
|
ext = path.suffix.lower()
|
|
if ext == ".pdf":
|
|
return {"page": 1}
|
|
if ext == ".docx":
|
|
return {"paragraph": 1}
|
|
if ext == ".xlsx":
|
|
return {"sheet": first_sheet(path), "cell": "A1"}
|
|
if ext == ".pptx":
|
|
return {"slide": 1, "notes": False}
|
|
return {"line": 1}
|
|
|
|
|
|
def main() -> int:
|
|
path, queries = Path(sys.argv[1]), json.loads(Path(sys.argv[2]).read_text(encoding="utf-8"))
|
|
loc = first_locator(path)
|
|
answers = [{"id": q["id"], "found": [{"locator": loc, "hidden": False}]} for q in queries["queries"]]
|
|
print(json.dumps({"answers": answers}))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|