paperstack-cli 0.3.2__py3-none-any.whl → 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
paperstack/cli.py CHANGED
@@ -1004,10 +1004,13 @@ def _run_paper(a: argparse.Namespace) -> int:
1004
1004
  from .content import arxiv_pdf
1005
1005
 
1006
1006
  arxiv_pdf.CACHE_DIR = _paper_cache()
1007
- cached_pdf = arxiv_pdf.CACHE_DIR / ref.value / "paper.md"
1008
- if offline and (not cached_pdf.is_file() or cached_pdf.stat().st_size <= 1000):
1009
- die(f"no complete cached PDF conversion for arxiv:{ref.value}")
1010
- if not arxiv_pdf.convert(ref.value):
1007
+ cached_pdf = arxiv_pdf._cached_conversion(
1008
+ arxiv_pdf.CACHE_DIR / ref.value,
1009
+ allow_native_fallback=offline,
1010
+ )
1011
+ if offline and cached_pdf is None:
1012
+ die(f"no usable cached PDF conversion for arxiv:{ref.value}")
1013
+ if not arxiv_pdf.convert(ref.value, allow_native_fallback=offline):
1011
1014
  return 1
1012
1015
  return 0
1013
1016
  raise AssertionError(a.paper_cmd)
@@ -1,4 +1,4 @@
1
- """Convert native-PDF arXiv submissions to Markdown.
1
+ """Convert arXiv PDF submissions to Markdown with selective OCR.
2
2
 
3
3
  Used by `paperstack paper pdf` after the source fetcher reports no TeX.
4
4
  """
@@ -6,9 +6,11 @@ Used by `paperstack paper pdf` after the source fetcher reports no TeX.
6
6
  from __future__ import annotations
7
7
 
8
8
  import hashlib
9
+ import importlib.metadata
9
10
  import json
10
11
  import os
11
12
  import sys
13
+ import tempfile
12
14
  import time
13
15
  import urllib.error
14
16
  import urllib.request
@@ -16,6 +18,9 @@ from pathlib import Path
16
18
 
17
19
  _CACHE_ROOT = Path(os.environ.get("XDG_CACHE_HOME", Path.home() / ".cache"))
18
20
  CACHE_DIR = Path(os.environ.get("PAPERSTACK_PAPERS_DIR", _CACHE_ROOT / "paperstack" / "papers"))
21
+ CONVERTER = "pdf-inspector"
22
+ MIN_MARKDOWN_CHARS = 100
23
+ OCR_RUNTIME_GUIDE = "https://github.com/firecrawl/pdf-inspector/blob/main/docs/ocr-runtime.md"
19
24
 
20
25
 
21
26
  def _fetch(url: str, timeout: int = 60) -> bytes | None:
@@ -38,15 +43,139 @@ def _fetch(url: str, timeout: int = 60) -> bytes | None:
38
43
  return None
39
44
 
40
45
 
41
- def convert(arxiv_id: str) -> bool:
42
- d = CACHE_DIR / arxiv_id
46
+ def _cached_conversion(d: Path, *, allow_native_fallback: bool = False) -> Path | None:
43
47
  md_path = d / "paper.md"
44
- if md_path.exists() and md_path.stat().st_size > 1000:
45
- print(f"{arxiv_id}: cached at {md_path}")
48
+ meta_path = d / "meta.json"
49
+ if not md_path.is_file() or md_path.stat().st_size < MIN_MARKDOWN_CHARS or not meta_path.is_file():
50
+ return None
51
+ try:
52
+ markdown = md_path.read_bytes()
53
+ meta = json.loads(meta_path.read_text(encoding="utf-8"))
54
+ except (OSError, UnicodeDecodeError, json.JSONDecodeError):
55
+ return None
56
+ if not isinstance(meta, dict):
57
+ return None
58
+ valid = (
59
+ meta.get("converter") == CONVERTER
60
+ and meta.get("bytes") == len(markdown)
61
+ and meta.get("sha256") == hashlib.sha256(markdown).hexdigest()
62
+ and (allow_native_fallback or meta.get("conversion_mode") != "native_fallback")
63
+ )
64
+ return md_path if valid else None
65
+
66
+
67
+ def _converter_version() -> str:
68
+ try:
69
+ return importlib.metadata.version(CONVERTER)
70
+ except importlib.metadata.PackageNotFoundError:
71
+ return "unknown"
72
+
73
+
74
+ def _ocr_reasons(items) -> list[dict]:
75
+ return [{"page": item.page, "reasons": list(item.reasons)} for item in items]
76
+
77
+
78
+ def _page_provenance(page) -> dict:
79
+ provenance = page.provenance
80
+ model = provenance.ocr_model
81
+ return {
82
+ "page": page.page_number,
83
+ "source": provenance.source,
84
+ "ocr_model": None if model is None else {"name": model.name, "revision": model.revision},
85
+ "render_dpi": provenance.render_dpi,
86
+ "ocr_confidence": provenance.ocr_confidence,
87
+ "hosted_recommended": provenance.hosted_recommended,
88
+ "warnings": list(provenance.warnings),
89
+ "timings": {
90
+ "render_ms": provenance.timings.render_ms,
91
+ "ocr_ms": provenance.timings.ocr_ms,
92
+ "assembly_ms": provenance.timings.assembly_ms,
93
+ },
94
+ }
95
+
96
+
97
+ def _base_meta(arxiv_id: str, url: str, markdown: str) -> dict:
98
+ encoded = markdown.encode("utf-8")
99
+ return {
100
+ "schema_version": 1,
101
+ "arxiv_id": arxiv_id,
102
+ "bytes": len(encoded),
103
+ "sha256": hashlib.sha256(encoded).hexdigest(),
104
+ "url": url,
105
+ "converter": CONVERTER,
106
+ "converter_version": _converter_version(),
107
+ }
108
+
109
+
110
+ def _ocr_meta(arxiv_id: str, url: str, markdown: str, result) -> dict:
111
+ meta = _base_meta(arxiv_id, url, markdown)
112
+ meta.update(
113
+ {
114
+ "conversion_mode": "selective_ocr",
115
+ "quality": "partial" if result.pages_recommending_hosted else "complete",
116
+ "page_count": result.page_count,
117
+ "pages_recommended_for_ocr": list(result.pages_recommended_for_ocr),
118
+ "pages_routed_to_ocr": list(result.pages_routed_to_ocr),
119
+ "pages_recommending_hosted": list(result.pages_recommending_hosted),
120
+ "ocr_reasons_by_page": _ocr_reasons(result.ocr_reasons_by_page),
121
+ "pages_with_tables": list(result.pages_with_tables),
122
+ "pages_with_columns": list(result.pages_with_columns),
123
+ "pages": [_page_provenance(page) for page in result.pages],
124
+ }
125
+ )
126
+ return meta
127
+
128
+
129
+ def _native_fallback_meta(arxiv_id: str, url: str, markdown: str, result, error: ValueError) -> dict:
130
+ meta = _ocr_meta(arxiv_id, url, markdown, result)
131
+ meta["conversion_mode"] = "native_fallback"
132
+ meta["quality"] = "partial"
133
+ meta["ocr_error"] = str(error)
134
+ return meta
135
+
136
+
137
+ def _usable(markdown: str | None) -> bool:
138
+ return markdown is not None and len(markdown.strip()) >= MIN_MARKDOWN_CHARS
139
+
140
+
141
+ def _is_pdf(path: Path) -> bool:
142
+ try:
143
+ with path.open("rb") as stream:
144
+ return stream.read(4) == b"%PDF"
145
+ except OSError:
146
+ return False
147
+
148
+
149
+ def _download_pdf(url: str, pdf_path: Path) -> bool:
150
+ raw = _fetch(url)
151
+ if raw is None:
152
+ print(f"could not fetch {url}", file=sys.stderr)
153
+ return False
154
+ if not raw.startswith(b"%PDF"):
155
+ print(f"{url} did not return a PDF", file=sys.stderr)
156
+ return False
157
+ descriptor, staged = tempfile.mkstemp(prefix=f".{pdf_path.name}-", suffix=".part", dir=pdf_path.parent)
158
+ try:
159
+ with os.fdopen(descriptor, "wb") as stream:
160
+ stream.write(raw)
161
+ os.replace(staged, pdf_path)
162
+ finally:
163
+ try:
164
+ os.unlink(staged)
165
+ except FileNotFoundError:
166
+ pass
167
+ return True
168
+
169
+
170
+ def convert(arxiv_id: str, *, allow_native_fallback: bool = False) -> bool:
171
+ d = CACHE_DIR / arxiv_id
172
+ cached = _cached_conversion(d, allow_native_fallback=allow_native_fallback)
173
+ if cached is not None:
174
+ print(f"{arxiv_id}: cached at {cached}")
46
175
  return True
47
176
 
48
177
  try:
49
- import pymupdf4llm
178
+ import pdf_inspector
50
179
  except ImportError:
51
180
  print(
52
181
  "PDF conversion needs `paperstack-cli[pdf]`; reinstall with "
@@ -56,36 +185,57 @@ def convert(arxiv_id: str) -> bool:
56
185
  return False
57
186
 
58
187
  url = f"https://arxiv.org/pdf/{arxiv_id}"
59
- raw = _fetch(url)
60
- if raw is None:
61
- print(f"{arxiv_id}: could not fetch {url}", file=sys.stderr)
62
- return False
63
- if not raw.startswith(b"%PDF"):
64
- print(f"{arxiv_id}: {url} did not return a PDF", file=sys.stderr)
65
- return False
66
-
67
188
  d.mkdir(parents=True, exist_ok=True)
68
189
  pdf_path = d / "paper.pdf"
69
- pdf_path.write_bytes(raw)
70
- md = pymupdf4llm.to_markdown(str(pdf_path))
71
- if len(md) < 500:
72
- print(f"{arxiv_id}: converted to only {len(md)} chars, treat as a failure", file=sys.stderr)
190
+ reused_cached_pdf = _is_pdf(pdf_path)
191
+ if not reused_cached_pdf and not _download_pdf(url, pdf_path):
73
192
  return False
74
193
 
75
- md_path.write_text(md, encoding="utf-8")
76
- (d / "meta.json").write_text(
77
- json.dumps(
78
- {
79
- "arxiv_id": arxiv_id,
80
- "bytes": len(md),
81
- "sha256": hashlib.sha256(md.encode("utf-8")).hexdigest(),
82
- "url": url,
83
- "converter": "pymupdf4llm",
84
- },
85
- indent=2,
86
- ),
87
- encoding="utf-8",
88
- )
194
+ while True:
195
+ ocr_error = None
196
+ try:
197
+ result = pdf_inspector.process_pdf_with_ocr(str(pdf_path), mode="auto")
198
+ parsed = result
199
+ md = result.markdown
200
+ meta = _ocr_meta(arxiv_id, url, md, result)
201
+ except ValueError as error:
202
+ ocr_error = error
203
+ try:
204
+ native = pdf_inspector.process_pdf_with_ocr(str(pdf_path), mode="off")
205
+ except ValueError:
206
+ native = None
207
+ parsed = native
208
+ if native is None:
209
+ md = None
210
+ else:
211
+ md = native.markdown
212
+ if _usable(md):
213
+ meta = _native_fallback_meta(arxiv_id, url, md, native, error)
214
+
215
+ if _usable(md):
216
+ if ocr_error is not None:
217
+ print(f"{arxiv_id}: OCR unavailable; cached {meta['quality']} native extraction", file=sys.stderr)
218
+ break
219
+ needs_ocr = parsed is not None and bool(parsed.pages_recommended_for_ocr)
220
+ if reused_cached_pdf and not needs_ocr and _download_pdf(url, pdf_path):
221
+ reused_cached_pdf = False
222
+ continue
223
+ if ocr_error is not None:
224
+ print(f"{arxiv_id}: PDF conversion failed: {ocr_error}", file=sys.stderr)
225
+ if needs_ocr:
226
+ print(
227
+ "OCR needs PDFium and ONNX Runtime; set PDFIUM_LIB_PATH and ORT_DYLIB_PATH.",
228
+ file=sys.stderr,
229
+ )
230
+ print(f"OCR runtime setup: {OCR_RUNTIME_GUIDE}", file=sys.stderr)
231
+ return False
232
+ chars = len(md.strip()) if md else 0
233
+ print(f"{arxiv_id}: converted to only {chars} chars, treat as a failure", file=sys.stderr)
234
+ return False
235
+
236
+ md_path = d / "paper.md"
237
+ md_path.write_bytes(md.encode("utf-8"))
238
+ (d / "meta.json").write_text(json.dumps(meta, indent=2), encoding="utf-8")
89
239
  print(f"{arxiv_id}: {len(md)} chars -> {md_path}")
90
240
  return True
91
241
 
paperstack/entrypoint.py CHANGED
@@ -9,6 +9,8 @@ from dotenv import find_dotenv, load_dotenv
9
9
 
10
10
  from . import credentials
11
11
 
12
+ _EXPORTED_ONLY = ("PDFIUM_LIB_PATH", "ORT_DYLIB_PATH")
13
+
12
14
 
13
15
  def load_environment() -> None:
14
16
  """Load the nearest .env without replacing exported variables."""
@@ -16,6 +18,9 @@ def load_environment() -> None:
16
18
  path = find_dotenv(usecwd=True)
17
19
  if path:
18
20
  load_dotenv(path, override=False)
21
+ for name in _EXPORTED_ONLY:
22
+ if name not in exported_keys:
23
+ os.environ.pop(name, None)
19
24
  credentials.set_environment_context(exported_keys, Path(path) if path else None)
20
25
 
21
26
 
@@ -1,22 +1,29 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: paperstack-cli
3
- Version: 0.3.2
3
+ Version: 0.4.0
4
4
  Summary: Review, inspect, and retrieve research sources from one CLI
5
5
  Project-URL: Repository, https://github.com/MilkClouds/paperstack
6
6
  Project-URL: Issues, https://github.com/MilkClouds/paperstack/issues
7
7
  License-Expression: Apache-2.0
8
8
  License-File: LICENSE
9
+ Classifier: Programming Language :: Python :: 3 :: Only
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
9
14
  Requires-Python: >=3.11
10
15
  Requires-Dist: filelock>=3.20
11
16
  Requires-Dist: polars>=1.43
12
17
  Requires-Dist: python-dotenv>=1.2.2
13
18
  Requires-Dist: pyyaml>=6
14
19
  Provides-Extra: pdf
15
- Requires-Dist: pymupdf4llm>=0.0.17; extra == 'pdf'
20
+ Requires-Dist: pdf-inspector<2,>=1.17; extra == 'pdf'
16
21
  Description-Content-Type: text/markdown
17
22
 
18
23
  # Paperstack
19
24
 
25
+ [![CI](https://github.com/MilkClouds/paperstack/actions/workflows/ci.yml/badge.svg)](https://github.com/MilkClouds/paperstack/actions/workflows/ci.yml) [![PyPI version](https://img.shields.io/pypi/v/paperstack-cli.svg)](https://pypi.org/project/paperstack-cli/) [![Python versions](https://img.shields.io/pypi/pyversions/paperstack-cli.svg)](https://pypi.org/project/paperstack-cli/) [![License](https://img.shields.io/pypi/l/paperstack-cli.svg)](https://github.com/MilkClouds/paperstack/blob/main/LICENSE)
26
+
20
27
  Paperstack is an open-source CLI for maintaining, querying, and publishing an independently authored research corpus.
21
28
  The software and corpus are separate: Paperstack can be public while each corpus and its editorial judgments remain
22
29
  under its maintainer's control.
@@ -38,6 +45,18 @@ PDF conversion is optional:
38
45
  uv tool install 'paperstack-cli[pdf]'
39
46
  ```
40
47
 
48
+ The PDF extra uses `pdf-inspector` for native extraction and selective OCR.
49
+ Clean text PDFs need no external runtime, while routed OCR pages require the pinned PDFium and ONNX Runtime libraries described in the [OCR runtime guide](https://github.com/firecrawl/pdf-inspector/blob/main/docs/ocr-runtime.md).
50
+ The first routed page downloads and verifies the pinned OCR model set unless an offline model directory is configured.
51
+ When the shared libraries are not on the platform search path, point `pdf-inspector` at the extracted files:
52
+
53
+ ```bash
54
+ export PDFIUM_LIB_PATH=/absolute/path/to/libpdfium.so
55
+ export ORT_DYLIB_PATH=/absolute/path/to/libonnxruntime.so
56
+ ```
57
+
58
+ The runtime guide lists the matching downloads and filenames for Linux, macOS, and Windows.
59
+
41
60
  ## AI agent skill
42
61
 
43
62
  Paperstack ships an [Agent Skills](https://agentskills.io)-compatible skill for Codex, Claude Code, and other
@@ -1,19 +1,19 @@
1
1
  paperstack/__init__.py,sha256=Cnu12PizQdtp2EOJIPMV8LN-2JXdtSiPodSD3iISedg,39
2
2
  paperstack/arxiv.py,sha256=I3nNuzCTDy7QSd0nvt0J5skH6Y_qThQM5DjVh82sz3w,4514
3
3
  paperstack/citations.py,sha256=lnYmGEDiGJeAa8XUQ7P-Rzf5y3u3jmIp-R5ZoFCYrJ0,4237
4
- paperstack/cli.py,sha256=yrFkAdO9r7K_b-z9inNafW6-XuIGBvEu2RL2afSIe-E,47526
4
+ paperstack/cli.py,sha256=mjQ-NfwjqT5sWt5cb_8Kkqg9fYxX2ZGiOInlb6TK0Wk,47593
5
5
  paperstack/corpora.py,sha256=14oQaQHLEFfWeX15DB-j9cnRl-1gGSYL6z-r32qGjQU,5367
6
6
  paperstack/credentials.py,sha256=-OvxwOfQwGM-SBkX8IN6J8El0vuG0_YIkTMCe5g8lYI,4848
7
7
  paperstack/dblp_build.py,sha256=VMNQv78-5jGr7-ZU2VVxKus2qtLimZHVhN-npEGe2Jw,6938
8
8
  paperstack/dblp_catalog.py,sha256=BZNV_DpHiiJKveV9m-2o5Ha6Dt9hR1knClvnn70mg0A,3311
9
9
  paperstack/dblp_index.py,sha256=FRDjMAjRxaUdsKB3FXX2mJCyW6ce-sBgUb4Pk3dSSXA,20340
10
10
  paperstack/entry_types.py,sha256=Iw9_K2_2w2Has4dOi_4fxYYFdWr-Kc_3i4Qj40-Fgjw,12540
11
- paperstack/entrypoint.py,sha256=tz46tgq2Ahy7FhqOKAVIZwRQdCdaIHDVM0xls69tn9c,726
11
+ paperstack/entrypoint.py,sha256=VWHY4qb3ZwlTY6fnZvBSZ5L28IYwxNbUv5uCO0IdQRc,891
12
12
  paperstack/metadata.py,sha256=x3B0UP_yLjJBFFC0Yez-sGS5dLFCT1BS59rMzb37tnw,16345
13
13
  paperstack/semantic_scholar.py,sha256=SGJpmy4zcuwptq1Joowg_ykusyrCxA85lLFIYPvCEb8,6089
14
14
  paperstack/viewer.py,sha256=HHBi7B5fo-vTUU5ZIf1gY2qEYQ9qi76YHzl1xUUlnCs,7265
15
15
  paperstack/content/__init__.py,sha256=rPAfhJSpEpOFyKlPNbbfEZ9-oDTbfJDEqxJHnCgHVp0,54
16
- paperstack/content/arxiv_pdf.py,sha256=TjqpPVIVVU--T9qTJsiRSqt8xOZ_3YDcEulZEX7D6DU,3073
16
+ paperstack/content/arxiv_pdf.py,sha256=m18-8CL0FAenJ0jo77QYbjpp5mKAbQ9hhyGOnNgPeUc,8778
17
17
  paperstack/content/arxiv_source.py,sha256=6LPMGGfi7UzKBUJglMBQWhiHVjXSrIzKRxDqjC1O_zA,15122
18
18
  paperstack/content/vendor/latexpand,sha256=Z-ylXQmz-5gt7iNE5jF_fhDp8Vg5EC5qHbOOUY-DMDw,23338
19
19
  paperstack/content/vendor/latexpand.LICENSE,sha256=hpipH1rQQwKFd1DYN_FtpxSBbYqCBBjsgCKr8YuYJhQ,1529
@@ -24,8 +24,8 @@ paperstack/viewer_assets/site/index.html,sha256=L-RUqIQODuEYxCfky-KjdYnT8VlHuo4v
24
24
  paperstack/viewer_assets/site/style.css,sha256=MdQthEKaslRDPvEVIqB6PVkS6N9030AHGMITuSaUU24,9378
25
25
  paperstack/viewer_assets/vendor/marked.LICENSE,sha256=0XnNx76Kb3YqkDWVF6gJiKaROm5OA3MhIvCeF74B9-Y,2679
26
26
  paperstack/viewer_assets/vendor/marked.min.js,sha256=k04-Nuni2gr7Gm51B1uw8JrwUpOoROhKdHfvQJEcNJo,39589
27
- paperstack_cli-0.3.2.dist-info/METADATA,sha256=Ajjnod2MBKxFJ0J71GipPqhK63nDoKcM1iUe4l6bVdA,7359
28
- paperstack_cli-0.3.2.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
29
- paperstack_cli-0.3.2.dist-info/entry_points.txt,sha256=0716S7tM1TuT4_aj291u3335bww-f0K0wQ1u8pS7VxU,58
30
- paperstack_cli-0.3.2.dist-info/licenses/LICENSE,sha256=_i01vK4EN87TL8E1Fo6RsNIkMi9-_t-wzHTFiqfg9xk,11357
31
- paperstack_cli-0.3.2.dist-info/RECORD,,
27
+ paperstack_cli-0.4.0.dist-info/METADATA,sha256=JvrE3kXgPf8j0_qbZmusx54-a-XdKdjVMlk7_INi0CY,8883
28
+ paperstack_cli-0.4.0.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
29
+ paperstack_cli-0.4.0.dist-info/entry_points.txt,sha256=0716S7tM1TuT4_aj291u3335bww-f0K0wQ1u8pS7VxU,58
30
+ paperstack_cli-0.4.0.dist-info/licenses/LICENSE,sha256=_i01vK4EN87TL8E1Fo6RsNIkMi9-_t-wzHTFiqfg9xk,11357
31
+ paperstack_cli-0.4.0.dist-info/RECORD,,